blob: 5d56abf7799a4f6e8c74299a15ff2bd5afabeb56 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -08002#
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 Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Mike Frysingeraf1e5de2020-02-17 14:58:37 -050018
Kyunam.jo2e147922016-10-12 16:33:19 +090019from collections import defaultdict
Mike Frysingeraf1e5de2020-02-17 14:58:37 -050020import sys
21
22from command import Command
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080023from git_command import git
Shawn O. Pearce552ac892009-04-18 15:15:24 -070024from progress import Progress
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080025
David Pursehouse819827a2020-02-12 15:20:19 +090026
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080027class Abandon(Command):
28 common = True
29 helpSummary = "Permanently abandon a development branch"
30 helpUsage = """
Kyunam.jo2e147922016-10-12 16:33:19 +090031%prog [--all | <branchname>] [<project>...]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080032
33This subcommand permanently abandons a development branch by
34deleting it (and all its history) from your local repository.
35
36It is equivalent to "git branch -D <branchname>".
37"""
David Pursehouse819827a2020-02-12 15:20:19 +090038
Kyunam.jo2e147922016-10-12 16:33:19 +090039 def _Options(self, p):
40 p.add_option('--all',
41 dest='all', action='store_true',
42 help='delete all branches in all projects')
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080043
Mike Frysingerae6cb082019-08-27 01:10:59 -040044 def ValidateOptions(self, opt, args):
Kyunam.jo2e147922016-10-12 16:33:19 +090045 if not opt.all and not args:
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080046 self.Usage()
47
Kyunam.jo2e147922016-10-12 16:33:19 +090048 if not opt.all:
49 nb = args[0]
50 if not git.check_ref_format('heads/%s' % nb):
Mike Frysingerae6cb082019-08-27 01:10:59 -040051 self.OptionParser.error("'%s' is not a valid branch name" % nb)
Kyunam.jo2e147922016-10-12 16:33:19 +090052 else:
Mike Frysingerae6cb082019-08-27 01:10:59 -040053 args.insert(0, "'All local branches'")
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080054
Mike Frysingerae6cb082019-08-27 01:10:59 -040055 def Execute(self, opt, args):
56 nb = args[0]
Kyunam.jo2e147922016-10-12 16:33:19 +090057 err = defaultdict(list)
58 success = defaultdict(list)
David Pursehouse5c6eeac2012-10-11 16:44:48 +090059 all_projects = self.GetProjects(args[1:])
Shawn O. Pearce552ac892009-04-18 15:15:24 -070060
David Pursehouse5c6eeac2012-10-11 16:44:48 +090061 pm = Progress('Abandon %s' % nb, len(all_projects))
62 for project in all_projects:
Shawn O. Pearce552ac892009-04-18 15:15:24 -070063 pm.update()
Doug Andersondafb1d62011-04-07 11:46:59 -070064
Kyunam.jo2e147922016-10-12 16:33:19 +090065 if opt.all:
Mike Frysinger31067c02019-06-13 02:13:23 -040066 branches = list(project.GetBranches().keys())
Kyunam.jo2e147922016-10-12 16:33:19 +090067 else:
68 branches = [nb]
69
70 for name in branches:
71 status = project.AbandonBranch(name)
72 if status is not None:
73 if status:
74 success[name].append(project)
75 else:
76 err[name].append(project)
Shawn O. Pearce552ac892009-04-18 15:15:24 -070077 pm.end()
78
Kyunam.jo2e147922016-10-12 16:33:19 +090079 width = 25
80 for name in branches:
81 if width < len(name):
82 width = len(name)
83
Shawn O. Pearce552ac892009-04-18 15:15:24 -070084 if err:
Kyunam.jo2e147922016-10-12 16:33:19 +090085 for br in err.keys():
David Pursehouse54a4e602020-02-12 14:31:05 +090086 err_msg = "error: cannot abandon %s" % br
Kyunam.jo2e147922016-10-12 16:33:19 +090087 print(err_msg, file=sys.stderr)
88 for proj in err[br]:
David Pursehouse54a4e602020-02-12 14:31:05 +090089 print(' ' * len(err_msg) + " | %s" % proj.relpath, file=sys.stderr)
Shawn O. Pearce552ac892009-04-18 15:15:24 -070090 sys.exit(1)
Doug Andersondafb1d62011-04-07 11:46:59 -070091 elif not success:
Kyunam.jo2e147922016-10-12 16:33:19 +090092 print('error: no project has local branch(es) : %s' % nb,
93 file=sys.stderr)
Doug Andersondafb1d62011-04-07 11:46:59 -070094 sys.exit(1)
95 else:
Kyunam.jo2e147922016-10-12 16:33:19 +090096 print('Abandoned branches:', file=sys.stderr)
97 for br in success.keys():
98 if len(all_projects) > 1 and len(all_projects) == len(success[br]):
99 result = "all project"
100 else:
101 result = "%s" % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900102 ('\n' + ' ' * width + '| ').join(p.relpath for p in success[br]))
David Pursehouse54a4e602020-02-12 14:31:05 +0900103 print("%s%s| %s\n" % (br, ' ' * (width - len(br)), result), file=sys.stderr)