Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 17 | import sys |
| 18 | from command import Command |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 19 | from collections import defaultdict |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 20 | from git_command import git |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 21 | from progress import Progress |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 22 | |
| 23 | class Abandon(Command): |
| 24 | common = True |
| 25 | helpSummary = "Permanently abandon a development branch" |
| 26 | helpUsage = """ |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 27 | %prog [--all | <branchname>] [<project>...] |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 28 | |
| 29 | This subcommand permanently abandons a development branch by |
| 30 | deleting it (and all its history) from your local repository. |
| 31 | |
| 32 | It is equivalent to "git branch -D <branchname>". |
| 33 | """ |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 34 | def _Options(self, p): |
| 35 | p.add_option('--all', |
| 36 | dest='all', action='store_true', |
| 37 | help='delete all branches in all projects') |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 38 | |
| 39 | def Execute(self, opt, args): |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 40 | if not opt.all and not args: |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 41 | self.Usage() |
| 42 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 43 | if not opt.all: |
| 44 | nb = args[0] |
| 45 | if not git.check_ref_format('heads/%s' % nb): |
| 46 | print("error: '%s' is not a valid name" % nb, file=sys.stderr) |
| 47 | sys.exit(1) |
| 48 | else: |
| 49 | args.insert(0,None) |
| 50 | nb = "'All local branches'" |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 51 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 52 | err = defaultdict(list) |
| 53 | success = defaultdict(list) |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 54 | all_projects = self.GetProjects(args[1:]) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 55 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 56 | pm = Progress('Abandon %s' % nb, len(all_projects)) |
| 57 | for project in all_projects: |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 58 | pm.update() |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 59 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 60 | if opt.all: |
| 61 | branches = project.GetBranches().keys() |
| 62 | else: |
| 63 | branches = [nb] |
| 64 | |
| 65 | for name in branches: |
| 66 | status = project.AbandonBranch(name) |
| 67 | if status is not None: |
| 68 | if status: |
| 69 | success[name].append(project) |
| 70 | else: |
| 71 | err[name].append(project) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 72 | pm.end() |
| 73 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 74 | width = 25 |
| 75 | for name in branches: |
| 76 | if width < len(name): |
| 77 | width = len(name) |
| 78 | |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 79 | if err: |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 80 | for br in err.keys(): |
| 81 | err_msg = "error: cannot abandon %s" %br |
| 82 | print(err_msg, file=sys.stderr) |
| 83 | for proj in err[br]: |
David Pursehouse | c354a9b | 2017-05-26 21:52:12 +0900 | [diff] [blame] | 84 | print(' '*len(err_msg) + " | %s" % proj.relpath, file=sys.stderr) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 85 | sys.exit(1) |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 86 | elif not success: |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 87 | print('error: no project has local branch(es) : %s' % nb, |
| 88 | file=sys.stderr) |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 89 | sys.exit(1) |
| 90 | else: |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 91 | print('Abandoned branches:', file=sys.stderr) |
| 92 | for br in success.keys(): |
| 93 | if len(all_projects) > 1 and len(all_projects) == len(success[br]): |
| 94 | result = "all project" |
| 95 | else: |
| 96 | result = "%s" % ( |
| 97 | ('\n'+' '*width + '| ').join(p.relpath for p in success[br])) |
| 98 | print("%s%s| %s\n" % (br,' '*(width-len(br)), result),file=sys.stderr) |