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.
|
|
|
|
|
2023-08-22 01:26:51 +00:00
|
|
|
import collections
|
2021-02-19 04:37:33 +00:00
|
|
|
import functools
|
|
|
|
import itertools
|
2020-02-17 19:58:37 +00:00
|
|
|
|
2023-08-22 01:20:32 +00:00
|
|
|
from command import Command
|
|
|
|
from command import DEFAULT_LOCAL_JOBS
|
|
|
|
from error import RepoError
|
|
|
|
from error import RepoExitError
|
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
|
2023-09-14 08:46:44 +00:00
|
|
|
from repo_logging import RepoLogger
|
|
|
|
|
|
|
|
|
|
|
|
logger = RepoLogger(__file__)
|
2023-08-03 21:38:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AbandonError(RepoExitError):
|
|
|
|
"""Exit error when abandon command fails."""
|
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):
|
2023-03-11 06:46:20 +00:00
|
|
|
COMMON = True
|
|
|
|
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>".
|
|
|
|
"""
|
2023-03-11 06:46:20 +00:00
|
|
|
PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2023-03-11 06:46:20 +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
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def ValidateOptions(self, opt, args):
|
|
|
|
if not opt.all and not args:
|
|
|
|
self.Usage()
|
2008-11-03 19:24:59 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
if not opt.all:
|
2023-03-24 04:16:35 +00:00
|
|
|
branches = args[0].split()
|
|
|
|
invalid_branches = [
|
|
|
|
x for x in branches if not git.check_ref_format(f"heads/{x}")
|
|
|
|
]
|
|
|
|
|
|
|
|
if invalid_branches:
|
|
|
|
self.OptionParser.error(
|
|
|
|
f"{invalid_branches} are not valid branch names"
|
|
|
|
)
|
2023-03-11 06:46:20 +00:00
|
|
|
else:
|
|
|
|
args.insert(0, "'All local branches'")
|
2008-11-03 19:24:59 +00:00
|
|
|
|
2024-10-22 13:04:41 +00:00
|
|
|
@classmethod
|
|
|
|
def _ExecuteOne(cls, all_branches, nb, project_idx):
|
2023-03-11 06:46:20 +00:00
|
|
|
"""Abandon one project."""
|
2024-10-22 13:04:41 +00:00
|
|
|
project = cls.get_parallel_context()["projects"][project_idx]
|
2023-03-11 06:46:20 +00:00
|
|
|
if all_branches:
|
|
|
|
branches = project.GetBranches()
|
|
|
|
else:
|
2023-03-24 04:16:35 +00:00
|
|
|
branches = nb
|
2021-02-19 04:37:33 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
ret = {}
|
2023-08-03 21:38:00 +00:00
|
|
|
errors = []
|
2023-03-11 06:46:20 +00:00
|
|
|
for name in branches:
|
2023-08-03 21:38:00 +00:00
|
|
|
status = None
|
|
|
|
try:
|
|
|
|
status = project.AbandonBranch(name)
|
|
|
|
except RepoError as e:
|
|
|
|
status = False
|
|
|
|
errors.append(e)
|
2023-03-11 06:46:20 +00:00
|
|
|
if status is not None:
|
|
|
|
ret[name] = status
|
2023-08-03 21:38:00 +00:00
|
|
|
|
2024-10-22 13:04:41 +00:00
|
|
|
return (ret, project_idx, errors)
|
2021-02-19 04:37:33 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def Execute(self, opt, args):
|
2023-03-24 04:16:35 +00:00
|
|
|
nb = args[0].split()
|
2023-08-22 01:26:51 +00:00
|
|
|
err = collections.defaultdict(list)
|
|
|
|
success = collections.defaultdict(list)
|
2023-08-03 21:38:00 +00:00
|
|
|
aggregate_errors = []
|
2023-03-11 06:46:20 +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
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
def _ProcessResults(_pool, pm, states):
|
2024-10-22 13:04:41 +00:00
|
|
|
for results, project_idx, errors in states:
|
|
|
|
project = all_projects[project_idx]
|
2023-03-11 06:46:20 +00:00
|
|
|
for branch, status in results.items():
|
|
|
|
if status:
|
|
|
|
success[branch].append(project)
|
|
|
|
else:
|
|
|
|
err[branch].append(project)
|
2023-08-03 21:38:00 +00:00
|
|
|
aggregate_errors.extend(errors)
|
2023-05-04 04:48:43 +00:00
|
|
|
pm.update(msg="")
|
2009-04-18 22:15:24 +00:00
|
|
|
|
2024-10-22 13:04:41 +00:00
|
|
|
with self.ParallelContext():
|
|
|
|
self.get_parallel_context()["projects"] = all_projects
|
|
|
|
self.ExecuteInParallel(
|
|
|
|
opt.jobs,
|
|
|
|
functools.partial(self._ExecuteOne, opt.all, nb),
|
|
|
|
range(len(all_projects)),
|
|
|
|
callback=_ProcessResults,
|
|
|
|
output=Progress(
|
|
|
|
f"Abandon {nb}", len(all_projects), quiet=opt.quiet
|
|
|
|
),
|
|
|
|
chunksize=1,
|
|
|
|
)
|
2016-10-12 07:33:19 +00:00
|
|
|
|
2023-03-11 06:46:20 +00:00
|
|
|
width = max(
|
|
|
|
itertools.chain(
|
|
|
|
[25], (len(x) for x in itertools.chain(success, err))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if err:
|
|
|
|
for br in err.keys():
|
|
|
|
err_msg = "error: cannot abandon %s" % br
|
2023-09-14 08:46:44 +00:00
|
|
|
logger.error(err_msg)
|
2023-03-11 06:46:20 +00:00
|
|
|
for proj in err[br]:
|
2023-09-14 08:46:44 +00:00
|
|
|
logger.error(" " * len(err_msg) + " | %s", _RelPath(proj))
|
2023-08-03 21:38:00 +00:00
|
|
|
raise AbandonError(aggregate_errors=aggregate_errors)
|
2023-03-11 06:46:20 +00:00
|
|
|
elif not success:
|
2023-09-14 08:46:44 +00:00
|
|
|
logger.error("error: no project has local branch(es) : %s", nb)
|
2023-08-03 21:38:00 +00:00
|
|
|
raise AbandonError(aggregate_errors=aggregate_errors)
|
2016-10-12 07:33:19 +00:00
|
|
|
else:
|
2023-03-11 06:46:20 +00:00
|
|
|
# Everything below here is displaying status.
|
|
|
|
if opt.quiet:
|
|
|
|
return
|
|
|
|
print("Abandoned branches:")
|
|
|
|
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(
|
|
|
|
_RelPath(p) for p in success[br]
|
|
|
|
)
|
|
|
|
)
|
2023-09-29 15:04:49 +00:00
|
|
|
print(f"{br}{' ' * (width - len(br))}| {result}\n")
|