2008-11-03 19:24:59 +00:00
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-02-17 19:58:37 +00:00
|
|
|
from collections import defaultdict
|
2021-02-19 04:37:33 +00:00
|
|
|
import functools
|
|
|
|
import itertools
|
2008-11-03 19:24:59 +00:00
|
|
|
import sys
|
2020-02-17 19:58:37 +00:00
|
|
|
|
2021-03-01 05:56:38 +00:00
|
|
|
from command import Command, DEFAULT_LOCAL_JOBS
|
2008-11-03 19:24:59 +00:00
|
|
|
from git_command import git
|
2009-04-18 22:15:24 +00:00
|
|
|
from progress import Progress
|
2008-11-03 19:24:59 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-11-03 19:24:59 +00:00
|
|
|
class Abandon(Command):
|
2021-06-14 20:05:19 +00:00
|
|
|
COMMON = True
|
2008-11-03 19:24:59 +00:00
|
|
|
helpSummary = "Permanently abandon a development branch"
|
|
|
|
helpUsage = """
|
2016-10-12 07:33:19 +00:00
|
|
|
%prog [--all | <branchname>] [<project>...]
|
2008-11-03 19:24:59 +00:00
|
|
|
|
|
|
|
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 <branchname>".
|
|
|
|
"""
|
2021-02-19 04:37:33 +00:00
|
|
|
PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2016-10-12 07:33:19 +00:00
|
|
|
def _Options(self, p):
|
|
|
|
p.add_option('--all',
|
|
|
|
dest='all', action='store_true',
|
|
|
|
help='delete all branches in all projects')
|
2008-11-03 19:24:59 +00:00
|
|
|
|
2019-08-27 05:10:59 +00:00
|
|
|
def ValidateOptions(self, opt, args):
|
2016-10-12 07:33:19 +00:00
|
|
|
if not opt.all and not args:
|
2008-11-03 19:24:59 +00:00
|
|
|
self.Usage()
|
|
|
|
|
2016-10-12 07:33:19 +00:00
|
|
|
if not opt.all:
|
|
|
|
nb = args[0]
|
|
|
|
if not git.check_ref_format('heads/%s' % nb):
|
2019-08-27 05:10:59 +00:00
|
|
|
self.OptionParser.error("'%s' is not a valid branch name" % nb)
|
2016-10-12 07:33:19 +00:00
|
|
|
else:
|
2019-08-27 05:10:59 +00:00
|
|
|
args.insert(0, "'All local branches'")
|
2008-11-03 19:24:59 +00:00
|
|
|
|
2021-03-01 05:56:38 +00:00
|
|
|
def _ExecuteOne(self, all_branches, nb, project):
|
2021-02-19 04:37:33 +00:00
|
|
|
"""Abandon one project."""
|
2021-03-01 05:56:38 +00:00
|
|
|
if all_branches:
|
2021-02-19 04:37:33 +00:00
|
|
|
branches = project.GetBranches()
|
|
|
|
else:
|
|
|
|
branches = [nb]
|
|
|
|
|
|
|
|
ret = {}
|
|
|
|
for name in branches:
|
|
|
|
status = project.AbandonBranch(name)
|
|
|
|
if status is not None:
|
|
|
|
ret[name] = status
|
|
|
|
return (ret, project)
|
|
|
|
|
2019-08-27 05:10:59 +00:00
|
|
|
def Execute(self, opt, args):
|
|
|
|
nb = args[0]
|
2016-10-12 07:33:19 +00:00
|
|
|
err = defaultdict(list)
|
|
|
|
success = defaultdict(list)
|
2021-11-18 22:40:18 +00:00
|
|
|
all_projects = self.GetProjects(args[1:], all_manifests=not opt.this_manifest_only)
|
|
|
|
_RelPath = lambda p: p.RelPath(local=opt.this_manifest_only)
|
2009-04-18 22:15:24 +00:00
|
|
|
|
2021-03-01 05:56:38 +00:00
|
|
|
def _ProcessResults(_pool, pm, states):
|
2021-02-19 04:37:33 +00:00
|
|
|
for (results, project) in states:
|
|
|
|
for branch, status in results.items():
|
2016-10-12 07:33:19 +00:00
|
|
|
if status:
|
2021-02-19 04:37:33 +00:00
|
|
|
success[branch].append(project)
|
2016-10-12 07:33:19 +00:00
|
|
|
else:
|
2021-02-19 04:37:33 +00:00
|
|
|
err[branch].append(project)
|
|
|
|
pm.update()
|
2009-04-18 22:15:24 +00:00
|
|
|
|
2021-03-01 05:56:38 +00:00
|
|
|
self.ExecuteInParallel(
|
|
|
|
opt.jobs,
|
|
|
|
functools.partial(self._ExecuteOne, opt.all, nb),
|
|
|
|
all_projects,
|
|
|
|
callback=_ProcessResults,
|
|
|
|
output=Progress('Abandon %s' % (nb,), len(all_projects), quiet=opt.quiet))
|
2016-10-12 07:33:19 +00:00
|
|
|
|
2021-02-19 04:37:33 +00:00
|
|
|
width = max(itertools.chain(
|
|
|
|
[25], (len(x) for x in itertools.chain(success, err))))
|
2009-04-18 22:15:24 +00:00
|
|
|
if err:
|
2016-10-12 07:33:19 +00:00
|
|
|
for br in err.keys():
|
2020-02-12 05:31:05 +00:00
|
|
|
err_msg = "error: cannot abandon %s" % br
|
2016-10-12 07:33:19 +00:00
|
|
|
print(err_msg, file=sys.stderr)
|
|
|
|
for proj in err[br]:
|
2021-11-18 22:40:18 +00:00
|
|
|
print(' ' * len(err_msg) + " | %s" % _RelPath(proj), file=sys.stderr)
|
2011-04-07 18:46:59 +00:00
|
|
|
sys.exit(1)
|
|
|
|
elif not success:
|
2016-10-12 07:33:19 +00:00
|
|
|
print('error: no project has local branch(es) : %s' % nb,
|
|
|
|
file=sys.stderr)
|
2009-04-18 22:15:24 +00:00
|
|
|
sys.exit(1)
|
2011-04-07 18:46:59 +00:00
|
|
|
else:
|
2020-02-20 05:48:39 +00:00
|
|
|
# Everything below here is displaying status.
|
|
|
|
if opt.quiet:
|
|
|
|
return
|
|
|
|
print('Abandoned branches:')
|
2016-10-12 07:33:19 +00:00
|
|
|
for br in success.keys():
|
|
|
|
if len(all_projects) > 1 and len(all_projects) == len(success[br]):
|
|
|
|
result = "all project"
|
|
|
|
else:
|
|
|
|
result = "%s" % (
|
2021-11-18 22:40:18 +00:00
|
|
|
('\n' + ' ' * width + '| ').join(_RelPath(p) for p in success[br]))
|
2020-02-20 05:48:39 +00:00
|
|
|
print("%s%s| %s\n" % (br, ' ' * (width - len(br)), result))
|