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