blob: f34781299e0edeb5003b220f1e405a1c06ecd761 [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
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080018import sys
19from command import Command
Kyunam.jo2e147922016-10-12 16:33:19 +090020from collections import defaultdict
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080021from git_command import git
Shawn O. Pearce552ac892009-04-18 15:15:24 -070022from progress import Progress
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080023
David Pursehouse819827a2020-02-12 15:20:19 +090024
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080025class Abandon(Command):
26 common = True
27 helpSummary = "Permanently abandon a development branch"
28 helpUsage = """
Kyunam.jo2e147922016-10-12 16:33:19 +090029%prog [--all | <branchname>] [<project>...]
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080030
31This subcommand permanently abandons a development branch by
32deleting it (and all its history) from your local repository.
33
34It is equivalent to "git branch -D <branchname>".
35"""
David Pursehouse819827a2020-02-12 15:20:19 +090036
Kyunam.jo2e147922016-10-12 16:33:19 +090037 def _Options(self, p):
38 p.add_option('--all',
39 dest='all', action='store_true',
40 help='delete all branches in all projects')
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080041
Mike Frysingerae6cb082019-08-27 01:10:59 -040042 def ValidateOptions(self, opt, args):
Kyunam.jo2e147922016-10-12 16:33:19 +090043 if not opt.all and not args:
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080044 self.Usage()
45
Kyunam.jo2e147922016-10-12 16:33:19 +090046 if not opt.all:
47 nb = args[0]
48 if not git.check_ref_format('heads/%s' % nb):
Mike Frysingerae6cb082019-08-27 01:10:59 -040049 self.OptionParser.error("'%s' is not a valid branch name" % nb)
Kyunam.jo2e147922016-10-12 16:33:19 +090050 else:
Mike Frysingerae6cb082019-08-27 01:10:59 -040051 args.insert(0, "'All local branches'")
Shawn O. Pearce9fa44db2008-11-03 11:24:59 -080052
Mike Frysingerae6cb082019-08-27 01:10:59 -040053 def Execute(self, opt, args):
54 nb = args[0]
Kyunam.jo2e147922016-10-12 16:33:19 +090055 err = defaultdict(list)
56 success = defaultdict(list)
David Pursehouse5c6eeac2012-10-11 16:44:48 +090057 all_projects = self.GetProjects(args[1:])
Shawn O. Pearce552ac892009-04-18 15:15:24 -070058
David Pursehouse5c6eeac2012-10-11 16:44:48 +090059 pm = Progress('Abandon %s' % nb, len(all_projects))
60 for project in all_projects:
Shawn O. Pearce552ac892009-04-18 15:15:24 -070061 pm.update()
Doug Andersondafb1d62011-04-07 11:46:59 -070062
Kyunam.jo2e147922016-10-12 16:33:19 +090063 if opt.all:
Mike Frysinger31067c02019-06-13 02:13:23 -040064 branches = list(project.GetBranches().keys())
Kyunam.jo2e147922016-10-12 16:33:19 +090065 else:
66 branches = [nb]
67
68 for name in branches:
69 status = project.AbandonBranch(name)
70 if status is not None:
71 if status:
72 success[name].append(project)
73 else:
74 err[name].append(project)
Shawn O. Pearce552ac892009-04-18 15:15:24 -070075 pm.end()
76
Kyunam.jo2e147922016-10-12 16:33:19 +090077 width = 25
78 for name in branches:
79 if width < len(name):
80 width = len(name)
81
Shawn O. Pearce552ac892009-04-18 15:15:24 -070082 if err:
Kyunam.jo2e147922016-10-12 16:33:19 +090083 for br in err.keys():
David Pursehouse54a4e602020-02-12 14:31:05 +090084 err_msg = "error: cannot abandon %s" % br
Kyunam.jo2e147922016-10-12 16:33:19 +090085 print(err_msg, file=sys.stderr)
86 for proj in err[br]:
David Pursehouse54a4e602020-02-12 14:31:05 +090087 print(' ' * len(err_msg) + " | %s" % proj.relpath, file=sys.stderr)
Shawn O. Pearce552ac892009-04-18 15:15:24 -070088 sys.exit(1)
Doug Andersondafb1d62011-04-07 11:46:59 -070089 elif not success:
Kyunam.jo2e147922016-10-12 16:33:19 +090090 print('error: no project has local branch(es) : %s' % nb,
91 file=sys.stderr)
Doug Andersondafb1d62011-04-07 11:46:59 -070092 sys.exit(1)
93 else:
Kyunam.jo2e147922016-10-12 16:33:19 +090094 print('Abandoned branches:', file=sys.stderr)
95 for br in success.keys():
96 if len(all_projects) > 1 and len(all_projects) == len(success[br]):
97 result = "all project"
98 else:
99 result = "%s" % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900100 ('\n' + ' ' * width + '| ').join(p.relpath for p in success[br]))
David Pursehouse54a4e602020-02-12 14:31:05 +0900101 print("%s%s| %s\n" % (br, ' ' * (width - len(br)), result), file=sys.stderr)