def check_branches(self): """Checks if the branch can be renamed. We require that the source branch exists, is not the current branch and is actually a review branch. We also require that the target branch does not exist. If successful, sets state to RENAME, otherwise to FINISHED. """ rietveld_info = None if utils.branch_exists(self.__target_branch): print 'Target branch %r already exists.' % (self.__target_branch, ) self.state = self.FINISHED elif not utils.branch_exists(self.__source_branch): print 'Branch %r doesn\'t exist.' % (self.__source_branch, ) self.state = self.FINISHED elif self.__source_branch == utils.get_current_branch(): print 'Can\'t rename branch you\'re currently in.' self.state = self.FINISHED else: rietveld_info = utils.RietveldInfo.from_branch( branch_name=self.__source_branch) if rietveld_info is None: print('Branch %r has no review in progress.' % (self.__source_branch, )) print 'Instead, use the git command:' print '\tgit branch -m %s %s' % (self.__source_branch, self.__target_branch) self.state = self.FINISHED else: self.state = self.RENAME self.advance(rietveld_info)
def check_branches(self): """Checks if the branch can be renamed. We require that the source branch exists, is not the current branch and is actually a review branch. We also require that the target branch does not exist. If successful, sets state to RENAME, otherwise to FINISHED. """ rietveld_info = None if utils.branch_exists(self.__target_branch): print 'Target branch %r already exists.' % (self.__target_branch,) self.state = self.FINISHED elif not utils.branch_exists(self.__source_branch): print 'Branch %r doesn\'t exist.' % (self.__source_branch,) self.state = self.FINISHED elif self.__source_branch == utils.get_current_branch(): print 'Can\'t rename branch you\'re currently in.' self.state = self.FINISHED else: rietveld_info = utils.RietveldInfo.from_branch( branch_name=self.__source_branch) if rietveld_info is None: print ('Branch %r has no review in progress.' % (self.__source_branch,)) print 'Instead, use the git command:' print '\tgit branch -m %s %s' % (self.__source_branch, self.__target_branch) self.state = self.FINISHED else: self.state = self.RENAME self.advance(rietveld_info)
def create_branch(self): """Creates dummy branch with contents from detached HEAD. - Finds a dummy name by using BRANCH_NAME_TEMPLATE and the current issue and then adding '_0' until it finds a branch name which doesn't already exist. - Creates and checks out (via checkout -b) the contents using the dummy name. Thanks to http://stackoverflow.com/a/4481621/1068170 for the merge strategy. If successful, sets state to COMMIT; if not, saves the error message and state to NOTIFY_FAILURE. In either case, advances the state machine. """ # Find dummy branch name review_branch = BRANCH_NAME_TEMPLATE % self.__issue while utils.branch_exists(review_branch): review_branch += '_0' # Dictionary to pass along state to advance() next_state_kwargs = {} # Create and checkout review branch print 'Checking out %s at %s.' % (review_branch, self.__last_synced) result, _, stderr = utils.capture_command('git', 'checkout', '-b', review_branch, expect_success=False) if result != 0: next_state_kwargs['error_message'] = stderr self.state = self.NOTIFY_FAILURE else: # Only set the review branch if it is created. self.__review_branch = review_branch self.state = self.COMMIT self.advance(**next_state_kwargs)
def create_branch(self): """Creates dummy branch with contents from detached HEAD. - Finds a dummy name by using BRANCH_NAME_TEMPLATE and the current issue and then adding '_0' until it finds a branch name which doesn't already exist. - Creates and checks out (via checkout -b) the contents using the dummy name. Thanks to http://stackoverflow.com/a/4481621/1068170 for the merge strategy. If successful, sets state to COMMIT; if not, saves the error message and state to NOTIFY_FAILURE. In either case, advances the state machine. """ # Find dummy branch name review_branch = BRANCH_NAME_TEMPLATE % self.__issue while utils.branch_exists(review_branch): review_branch += '_0' # Dictionary to pass along state to advance() next_state_kwargs = {} # Create and checkout review branch print 'Checking out %s at %s.' % (review_branch, self.__last_synced) result, _, stderr = utils.capture_command( 'git', 'checkout', '-b', review_branch, expect_success=False) if result != 0: next_state_kwargs['error_message'] = stderr self.state = self.NOTIFY_FAILURE else: # Only set the review branch if it is created. self.__review_branch = review_branch self.state = self.COMMIT self.advance(**next_state_kwargs)
def check_branch(self): """Checks if the branch can be deleted. We require that the branch exists, is not the current branch and is actually a review branch. If successful, sets state to DELETE, otherwise to FINISHED. """ if not utils.branch_exists(self.__branch): print 'Branch %r doesn\'t exist.' % (self.__branch,) self.state = self.FINISHED elif self.__branch == utils.get_current_branch(): print 'Can\'t delete current branch.' % (self.__branch,) self.state = self.FINISHED else: if not utils.in_review(current_branch=self.__branch): print 'Branch %r has no review in progress.' % (self.__branch,) print 'Instead, use the git command:' print '\tgit branch -D %s' % (self.__branch,) self.state = self.FINISHED else: self.state = self.DELETE self.advance()
def check_branch(self): """Checks if the branch can be deleted. We require that the branch exists, is not the current branch and is actually a review branch. If successful, sets state to DELETE, otherwise to FINISHED. """ if not utils.branch_exists(self.__branch): print 'Branch %r doesn\'t exist.' % (self.__branch, ) self.state = self.FINISHED elif self.__branch == utils.get_current_branch(): print 'Can\'t delete current branch.' % (self.__branch, ) self.state = self.FINISHED else: if not utils.in_review(current_branch=self.__branch): print 'Branch %r has no review in progress.' % ( self.__branch, ) print 'Instead, use the git command:' print '\tgit branch -D %s' % (self.__branch, ) self.state = self.FINISHED else: self.state = self.DELETE self.advance()