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 [<project>...]

Change-Id: I4d391f37fb9d89b8095488c585468eafc1a35f31
This commit is contained in:
Kyunam.jo 2016-10-12 16:33:19 +09:00 committed by Kyunam Jo
parent 699bcd40be
commit 2e14792a94

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()
if not opt.all:
nb = args[0] nb = args[0]
if not git.check_ref_format('heads/%s' % nb): if not git.check_ref_format('heads/%s' % nb):
print("error: '%s' is not a valid name" % nb, file=sys.stderr) print("error: '%s' is not a valid name" % nb, file=sys.stderr)
sys.exit(1) 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:
branches = project.GetBranches().keys()
else:
branches = [nb]
for name in branches:
status = project.AbandonBranch(name)
if status is not None: if status is not None:
if status: if status:
success.append(project) success[name].append(project)
else: else:
err.append(project) 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)