Merge "implement optional '--all' in the abandon command"

This commit is contained in:
David Pursehouse 2016-10-28 07:52:31 +00:00 committed by Gerrit Code Review
commit ae81c964b6

View File

@ -16,6 +16,7 @@
from __future__ import print_function from __future__ import print_function
import sys import sys
from command import Command from command import Command
from collections import defaultdict
from git_command import git from git_command import git
from progress import Progress from progress import Progress
@ -23,48 +24,75 @@ class Abandon(Command):
common = True common = True
helpSummary = "Permanently abandon a development branch" helpSummary = "Permanently abandon a development branch"
helpUsage = """ helpUsage = """
%prog <branchname> [<project>...] %prog [--all | <branchname>] [<project>...]
This subcommand permanently abandons a development branch by This subcommand permanently abandons a development branch by
deleting it (and all its history) from your local repository. deleting it (and all its history) from your local repository.
It is equivalent to "git branch -D <branchname>". It is equivalent to "git branch -D <branchname>".
""" """
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): def Execute(self, opt, args):
if not args: if not opt.all and not args:
self.Usage() self.Usage()
nb = args[0] if not opt.all:
if not git.check_ref_format('heads/%s' % nb): nb = args[0]
print("error: '%s' is not a valid name" % nb, file=sys.stderr) if not git.check_ref_format('heads/%s' % nb):
sys.exit(1) 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 = [] err = defaultdict(list)
success = [] success = defaultdict(list)
all_projects = self.GetProjects(args[1:]) all_projects = self.GetProjects(args[1:])
pm = Progress('Abandon %s' % nb, len(all_projects)) pm = Progress('Abandon %s' % nb, len(all_projects))
for project in all_projects: for project in all_projects:
pm.update() pm.update()
status = project.AbandonBranch(nb) if opt.all:
if status is not None: branches = project.GetBranches().keys()
if status: else:
success.append(project) branches = [nb]
else:
err.append(project) 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() pm.end()
width = 25
for name in branches:
if width < len(name):
width = len(name)
if err: if err:
for p in err: for br in err.keys():
print("error: %s/: cannot abandon %s" % (p.relpath, nb), err_msg = "error: cannot abandon %s" %br
file=sys.stderr) print(err_msg, file=sys.stderr)
for proj in err[br]:
print(' '*len(err_msg) + " | %s" % p.relpath, file=sys.stderr)
sys.exit(1) sys.exit(1)
elif not success: 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) sys.exit(1)
else: else:
print('Abandoned in %d project(s):\n %s' print('Abandoned branches:', file=sys.stderr)
% (len(success), '\n '.join(p.relpath for p in success)), for br in success.keys():
file=sys.stderr) 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)