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 |
Mike Frysinger | af1e5de | 2020-02-17 14:58:37 -0500 | [diff] [blame] | 18 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 19 | from collections import defaultdict |
Mike Frysinger | af1e5de | 2020-02-17 14:58:37 -0500 | [diff] [blame] | 20 | import sys |
| 21 | |
| 22 | from command import Command |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 23 | from git_command import git |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 24 | from progress import Progress |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 25 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 26 | |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 27 | class Abandon(Command): |
| 28 | common = True |
| 29 | helpSummary = "Permanently abandon a development branch" |
| 30 | helpUsage = """ |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 31 | %prog [--all | <branchname>] [<project>...] |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 32 | |
| 33 | This subcommand permanently abandons a development branch by |
| 34 | deleting it (and all its history) from your local repository. |
| 35 | |
| 36 | It is equivalent to "git branch -D <branchname>". |
| 37 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 38 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 39 | def _Options(self, p): |
Mike Frysinger | e6e27b3 | 2020-02-20 00:48:39 -0500 | [diff] [blame] | 40 | p.add_option('-q', '--quiet', |
| 41 | action='store_true', default=False, |
| 42 | help='be quiet') |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 43 | p.add_option('--all', |
| 44 | dest='all', action='store_true', |
| 45 | help='delete all branches in all projects') |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 46 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 47 | def ValidateOptions(self, opt, args): |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 48 | if not opt.all and not args: |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 49 | self.Usage() |
| 50 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 51 | if not opt.all: |
| 52 | nb = args[0] |
| 53 | if not git.check_ref_format('heads/%s' % nb): |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 54 | self.OptionParser.error("'%s' is not a valid branch name" % nb) |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 55 | else: |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 56 | args.insert(0, "'All local branches'") |
Shawn O. Pearce | 9fa44db | 2008-11-03 11:24:59 -0800 | [diff] [blame] | 57 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 58 | def Execute(self, opt, args): |
| 59 | nb = args[0] |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 60 | err = defaultdict(list) |
| 61 | success = defaultdict(list) |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 62 | all_projects = self.GetProjects(args[1:]) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 63 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 64 | pm = Progress('Abandon %s' % nb, len(all_projects)) |
| 65 | for project in all_projects: |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 66 | pm.update() |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 67 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 68 | if opt.all: |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 69 | branches = list(project.GetBranches().keys()) |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 70 | else: |
| 71 | branches = [nb] |
| 72 | |
| 73 | for name in branches: |
| 74 | status = project.AbandonBranch(name) |
| 75 | if status is not None: |
| 76 | if status: |
| 77 | success[name].append(project) |
| 78 | else: |
| 79 | err[name].append(project) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 80 | pm.end() |
| 81 | |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 82 | width = 25 |
| 83 | for name in branches: |
| 84 | if width < len(name): |
| 85 | width = len(name) |
| 86 | |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 87 | if err: |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 88 | for br in err.keys(): |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 89 | err_msg = "error: cannot abandon %s" % br |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 90 | print(err_msg, file=sys.stderr) |
| 91 | for proj in err[br]: |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 92 | print(' ' * len(err_msg) + " | %s" % proj.relpath, file=sys.stderr) |
Shawn O. Pearce | 552ac89 | 2009-04-18 15:15:24 -0700 | [diff] [blame] | 93 | sys.exit(1) |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 94 | elif not success: |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 95 | print('error: no project has local branch(es) : %s' % nb, |
| 96 | file=sys.stderr) |
Doug Anderson | dafb1d6 | 2011-04-07 11:46:59 -0700 | [diff] [blame] | 97 | sys.exit(1) |
| 98 | else: |
Mike Frysinger | e6e27b3 | 2020-02-20 00:48:39 -0500 | [diff] [blame] | 99 | # Everything below here is displaying status. |
| 100 | if opt.quiet: |
| 101 | return |
| 102 | print('Abandoned branches:') |
Kyunam.jo | 2e14792 | 2016-10-12 16:33:19 +0900 | [diff] [blame] | 103 | for br in success.keys(): |
| 104 | if len(all_projects) > 1 and len(all_projects) == len(success[br]): |
| 105 | result = "all project" |
| 106 | else: |
| 107 | result = "%s" % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 108 | ('\n' + ' ' * width + '| ').join(p.relpath for p in success[br])) |
Mike Frysinger | e6e27b3 | 2020-02-20 00:48:39 -0500 | [diff] [blame] | 109 | print("%s%s| %s\n" % (br, ' ' * (width - len(br)), result)) |