def run(self): args = self.get_args() branch = args['branch'] # Confirmation prompt if args['confirm']: confirmation = cmd.prompt('Delete Branch? (y/n)', f'Delete branch {branch}?', default_val='n', validate_function=cmd.validate_yn) if not confirmation: return # Unset template unset_template_parsed_args = self.parser.parse_args( [UnsetTemplate.command, branch, '--force']) unset_template = UnsetTemplate(self.repo, self.parser, parsed_args=unset_template_parsed_args, verbosity=self.verbosity) unset_template.run() # Checkout base_branch base_branch = self.configs.BASE_BRANCH base_head = checkout_branch(self.repo, base_branch) # Finish branch self.print(f'Attempting to delete {branch}...') try: self.repo.git.branch('-d', branch) self.print_success('Deletion successful.') except GitCommandError: self.print_warning( f'Unable to delete {branch}. You will need to delete it manually.' )
def run(self): args = self.get_args() branch = args['branch'] # Get branch config path, print and exit if non-existent branch_config_file = self.configs.get_config( f'includeif.onbranch:{branch}.path', local=True, includes=True) if branch_config_file is None: self.print( f'Branch {branch} does not have an associated config file.') return # Confirmation prompt if args['confirm']: confirmation = cmd.prompt('Unset Template? (y/n)', f'Unset commit template for {branch}?', default_val='n', validate_function=cmd.validate_yn) if not confirmation: return # Verify config file exists branch_config_path = os.path.join(self.repo.git_dir, branch_config_file) if not os.path.exists(branch_config_path): self.print( f'Config file {branch_config_file} not found, unsetting include...' ) self.unset_includeif_onbranch_path(branch) self.print_success('Config include removed.', '') return # Unset commit.template in branch config file self.print(f'Unsetting commit.template config for {branch}...') commit_template_file = self.configs.get_config('commit.template', file=branch_config_path) if commit_template_file is None: self.print(f'commit.template not configured for branch {branch}.', '') return self.configs.unset_config('commit.template', file=branch_config_path) self.print_success('commit.template config unset.', '') # Delete commit template repo_root_dir = os.path.dirname(self.repo.git_dir) commit_template_path = os.path.join(repo_root_dir, commit_template_file) if os.path.exists(commit_template_path): self.print( f'Deleting commit template file {commit_template_file}...') os.remove(commit_template_path) self.print_success('Commit template file removed.', '') else: self.print('Commit template file already removed.') # If branch config is now empty, delete the file and unset includeIf if os.stat(branch_config_path).st_size == 0: self.print( f'Removing empty branch config file and unsetting include...') self.unset_includeif_onbranch_path(branch) os.remove(branch_config_path) self.print_success(f'Empty branch config removed.', '')
def get_args(self): """Parse command line arguments and prompt for any missing values. :return: A dictionary with the following keys: ticket """ args = {} validate_ticket_number = cmd.generate_validate_regex_function( self.configs.TICKET_INPUT_FORMAT_REGEX) ticket = cmd.prompt( 'Ticket Number', 'Enter ticket number to use in commit messages.', invalid_msg='Invalid ticket number formatting.', initial_input=self.parsed_args.ticket, validate_function=validate_ticket_number, format_function=self.format_ticket_number, ) args['ticket'] = ticket return args
def run(self): args = self.get_args() targets = self.find_cleanup_targets() orphans = targets.pop('orphans', []) # Pop current branch's configs if we shouldn't include it if not args['include_current_branch']: current_branch = targets.pop(self.repo.active_branch.name, None) # Otherwise keep track of it for messaging else: current_branch = targets.get(self.repo.active_branch.name, None) # Output: Configured Templates (unless --orphans-only) if not args['orphans_only']: # Case 1: We have branches to clean if targets: self.print( 'Commit templates will be unset for the following branches:', '', *targets.keys(), '') if args['include_current_branch'] and current_branch: self.print_warning( 'This includes the branch you are currently on!', '') # Case 2: Nothing to clean but current branch has a template elif current_branch: self.print( 'Only branch with a template configured is the current branch.' ) self.print( 'To include the current branch, use --include-current-branch argument.', '') # Output: Orphans if orphans: self.print( 'The following commit templates do not have an associated branch and will be deleted:', '', *orphans, '') # Output: Nothing to clean up if (not targets or args['orphans_only']) and (not orphans): self.print('Nothing to clean up.') return # Confirmation if args['confirm']: confirmation = cmd.prompt('Confirm (y/n)', 'Would you like to continue?', default_val='n', validate_function=cmd.validate_yn) if not confirmation: return # Unset Configured Templates if not args['orphans_only'] and targets: self.print('Unsetting configured templates...') for branch_name in targets.keys(): unset_template_parsed_args = self.parser.parse_args( [UnsetTemplate.command, branch_name, '--force']) unset_template = UnsetTemplate( self.repo, self.parser, parsed_args=unset_template_parsed_args, # TODO match verbosity if > 1? verbosity=0) unset_template.run() self.print_success(f'{branch_name} template unset.') self.print('') # Delete Orphans if orphans: self.print('Removing orphan templates...') repo_root_dir = os.path.dirname(self.repo.git_dir) for orphan in orphans: orphan_path = os.path.join(repo_root_dir, orphan) # NOTE: Probably don't need this exists() check since find_cleanup_targets() # builds this list based on the files it finds, but doing it just to be safe if os.path.exists(orphan_path): os.remove(orphan_path) if not os.path.exists(orphan_path): self.print_success(f'Deleted {orphan}.') else: self.print_warning(f'Unable to delete {orphan}.') self.print('')
def get_args(self): """Parse command line arguments and prompt for any missing values. :return: A dictionary with the following keys: client, description, initials, ticket, timestamp, base_branch, skip_bad_name_check """ args = {} client = None if not self.parsed_args.no_client: client = cmd.prompt( 'Client', '(Optional) Enter the name of the affected client.', initial_input=self.parsed_args.client, validate_function=cmd.validate_optional_prompt, format_function=self.format_branch_name, ) # Append hyphen if client is not empty if client: client += '-' else: client = '' args['client'] = client description = cmd.prompt( 'Description', 'Enter a brief description for the branch.', invalid_msg='Description must not be blank.', initial_input=self.parsed_args.description, format_function=self.format_branch_name, ) description += '-' args['description'] = description initials = cmd.prompt( 'Initials', 'Enter your initials.', invalid_msg='Must enter initials.', initial_input=self.parsed_args.initials or self.configs.INITIALS, format_function=self.format_branch_name, ) args['initials'] = initials ticket = None if not self.parsed_args.no_ticket: ticket = cmd.prompt( 'Ticket Number', '(Optional) Enter ticket number to use in commit messages.', "Leave blank if you don't want to use a commit template.", initial_input=self.parsed_args.ticket, validate_function=cmd.validate_optional_prompt, ) args['ticket'] = ticket timestamp = datetime.datetime.now().strftime('%Y%m%d-') args['timestamp'] = timestamp base_branch = self.parsed_args.base_branch or self.configs.BASE_BRANCH if self.parsed_args.branch_from_current: base_branch = self.repo.active_branch.name args['base_branch'] = base_branch args['base_release'] = self.parsed_args.base_release args['no_pull'] = self.parsed_args.no_pull args['skip_bad_name_check'] = self.parsed_args.skip_bad_name_check return args