From 2e14792a9481736e4b532da64813a7cb856c48a0 Mon Sep 17 00:00:00 2001 From: "Kyunam.jo" Date: Wed, 12 Oct 2016 16:33:19 +0900 Subject: [PATCH] implement optional '--all' in the abandon command when you want to delete all local branches, you should be find all branches' name, and type them behind 'repo abandon' command. Usage: repo abandon --all [...] Change-Id: I4d391f37fb9d89b8095488c585468eafc1a35f31 --- subcmds/abandon.py | 70 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/subcmds/abandon.py b/subcmds/abandon.py index 84517b91..6f78da74 100644 --- a/subcmds/abandon.py +++ b/subcmds/abandon.py @@ -16,6 +16,7 @@ from __future__ import print_function import sys from command import Command +from collections import defaultdict from git_command import git from progress import Progress @@ -23,48 +24,75 @@ class Abandon(Command): common = True helpSummary = "Permanently abandon a development branch" helpUsage = """ -%prog [...] +%prog [--all | ] [...] This subcommand permanently abandons a development branch by deleting it (and all its history) from your local repository. It is equivalent to "git branch -D ". """ + def _Options(self, p): + p.add_option('--all', + dest='all', action='store_true', + help='delete all branches in all projects') def Execute(self, opt, args): - if not args: + if not opt.all and not args: self.Usage() - nb = args[0] - if not git.check_ref_format('heads/%s' % nb): - print("error: '%s' is not a valid name" % nb, file=sys.stderr) - sys.exit(1) + if not opt.all: + nb = args[0] + if not git.check_ref_format('heads/%s' % nb): + print("error: '%s' is not a valid name" % nb, file=sys.stderr) + sys.exit(1) + else: + args.insert(0,None) + nb = "'All local branches'" - err = [] - success = [] + err = defaultdict(list) + success = defaultdict(list) all_projects = self.GetProjects(args[1:]) pm = Progress('Abandon %s' % nb, len(all_projects)) for project in all_projects: pm.update() - status = project.AbandonBranch(nb) - if status is not None: - if status: - success.append(project) - else: - err.append(project) + if opt.all: + branches = project.GetBranches().keys() + else: + branches = [nb] + + for name in branches: + status = project.AbandonBranch(name) + if status is not None: + if status: + success[name].append(project) + else: + err[name].append(project) pm.end() + width = 25 + for name in branches: + if width < len(name): + width = len(name) + if err: - for p in err: - print("error: %s/: cannot abandon %s" % (p.relpath, nb), - file=sys.stderr) + for br in err.keys(): + err_msg = "error: cannot abandon %s" %br + print(err_msg, file=sys.stderr) + for proj in err[br]: + print(' '*len(err_msg) + " | %s" % p.relpath, file=sys.stderr) sys.exit(1) elif not success: - print('error: no project has branch %s' % nb, file=sys.stderr) + print('error: no project has local branch(es) : %s' % nb, + file=sys.stderr) sys.exit(1) else: - print('Abandoned in %d project(s):\n %s' - % (len(success), '\n '.join(p.relpath for p in success)), - file=sys.stderr) + print('Abandoned branches:', file=sys.stderr) + for br in success.keys(): + if len(all_projects) > 1 and len(all_projects) == len(success[br]): + result = "all project" + else: + result = "%s" % ( + ('\n'+' '*width + '| ').join(p.relpath for p in success[br])) + print("%s%s| %s\n" % (br,' '*(width-len(br)), result),file=sys.stderr)