blob: 359c431b1fdea2463aa83b39e38e6c32acc7bd2f [file] [log] [blame]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Kyunam.jo2e147922016-10-12 16:33:19 +090015from collections import defaultdict
Mike Frysingeraf1e5de2020-02-17 14:58:37 -050016import sys
17
18from command import Command
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080019from git_command import git
Shawn O. Pearce552ac892009-04-18 15:15:24 -070020from progress import Progress
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080021
David Pursehouse819827a2020-02-12 15:20:19 +090022
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080023class Abandon(Command):
24 common = True
25 helpSummary = "Permanently abandon a development branch"
26 helpUsage = """
Kyunam.jo2e147922016-10-12 16:33:19 +090027%prog [--all | <branchname>] [<project>...]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080028
29This subcommand permanently abandons a development branch by
30deleting it (and all its history) from your local repository.
31
32It is equivalent to "git branch -D <branchname>".
33"""
David Pursehouse819827a2020-02-12 15:20:19 +090034
Kyunam.jo2e147922016-10-12 16:33:19 +090035 def _Options(self, p):
Mike Frysingere6e27b32020-02-20 00:48:39 -050036 p.add_option('-q', '--quiet',
37 action='store_true', default=False,
38 help='be quiet')
Kyunam.jo2e147922016-10-12 16:33:19 +090039 p.add_option('--all',
40 dest='all', action='store_true',
41 help='delete all branches in all projects')
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080042
Mike Frysingerae6cb082019-08-27 01:10:59 -040043 def ValidateOptions(self, opt, args):
Kyunam.jo2e147922016-10-12 16:33:19 +090044 if not opt.all and not args:
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080045 self.Usage()
46
Kyunam.jo2e147922016-10-12 16:33:19 +090047 if not opt.all:
48 nb = args[0]
49 if not git.check_ref_format('heads/%s' % nb):
Mike Frysingerae6cb082019-08-27 01:10:59 -040050 self.OptionParser.error("'%s' is not a valid branch name" % nb)
Kyunam.jo2e147922016-10-12 16:33:19 +090051 else:
Mike Frysingerae6cb082019-08-27 01:10:59 -040052 args.insert(0, "'All local branches'")
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080053
Mike Frysingerae6cb082019-08-27 01:10:59 -040054 def Execute(self, opt, args):
55 nb = args[0]
Kyunam.jo2e147922016-10-12 16:33:19 +090056 err = defaultdict(list)
57 success = defaultdict(list)
David Pursehouse5c6eeac2012-10-11 16:44:48 +090058 all_projects = self.GetProjects(args[1:])
Shawn O. Pearce552ac892009-04-18 15:15:24 -070059
David Pursehouse5c6eeac2012-10-11 16:44:48 +090060 pm = Progress('Abandon %s' % nb, len(all_projects))
61 for project in all_projects:
Shawn O. Pearce552ac892009-04-18 15:15:24 -070062 pm.update()
Doug Andersondafb1d62011-04-07 11:46:59 -070063
Kyunam.jo2e147922016-10-12 16:33:19 +090064 if opt.all:
Mike Frysinger31067c02019-06-13 02:13:23 -040065 branches = list(project.GetBranches().keys())
Kyunam.jo2e147922016-10-12 16:33:19 +090066 else:
67 branches = [nb]
68
69 for name in branches:
70 status = project.AbandonBranch(name)
71 if status is not None:
72 if status:
73 success[name].append(project)
74 else:
75 err[name].append(project)
Shawn O. Pearce552ac892009-04-18 15:15:24 -070076 pm.end()
77
Kyunam.jo2e147922016-10-12 16:33:19 +090078 width = 25
79 for name in branches:
80 if width < len(name):
81 width = len(name)
82
Shawn O. Pearce552ac892009-04-18 15:15:24 -070083 if err:
Kyunam.jo2e147922016-10-12 16:33:19 +090084 for br in err.keys():
David Pursehouse54a4e602020-02-12 14:31:05 +090085 err_msg = "error: cannot abandon %s" % br
Kyunam.jo2e147922016-10-12 16:33:19 +090086 print(err_msg, file=sys.stderr)
87 for proj in err[br]:
David Pursehouse54a4e602020-02-12 14:31:05 +090088 print(' ' * len(err_msg) + " | %s" % proj.relpath, file=sys.stderr)
Shawn O. Pearce552ac892009-04-18 15:15:24 -070089 sys.exit(1)
Doug Andersondafb1d62011-04-07 11:46:59 -070090 elif not success:
Kyunam.jo2e147922016-10-12 16:33:19 +090091 print('error: no project has local branch(es) : %s' % nb,
92 file=sys.stderr)
Doug Andersondafb1d62011-04-07 11:46:59 -070093 sys.exit(1)
94 else:
Mike Frysingere6e27b32020-02-20 00:48:39 -050095 # Everything below here is displaying status.
96 if opt.quiet:
97 return
98 print('Abandoned branches:')
Kyunam.jo2e147922016-10-12 16:33:19 +090099 for br in success.keys():
100 if len(all_projects) > 1 and len(all_projects) == len(success[br]):
101 result = "all project"
102 else:
103 result = "%s" % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900104 ('\n' + ' ' * width + '| ').join(p.relpath for p in success[br]))
Mike Frysingere6e27b32020-02-20 00:48:39 -0500105 print("%s%s| %s\n" % (br, ' ' * (width - len(br)), result))