diff options
Diffstat (limited to 'subcmds/abandon.py')
-rw-r--r-- | subcmds/abandon.py | 70 |
1 files changed, 49 insertions, 21 deletions
diff --git a/subcmds/abandon.py b/subcmds/abandon.py index 84517b91..6f78da74 100644 --- a/subcmds/abandon.py +++ b/subcmds/abandon.py | |||
@@ -16,6 +16,7 @@ | |||
16 | from __future__ import print_function | 16 | from __future__ import print_function |
17 | import sys | 17 | import sys |
18 | from command import Command | 18 | from command import Command |
19 | from collections import defaultdict | ||
19 | from git_command import git | 20 | from git_command import git |
20 | from progress import Progress | 21 | from progress import Progress |
21 | 22 | ||
@@ -23,48 +24,75 @@ class Abandon(Command): | |||
23 | common = True | 24 | common = True |
24 | helpSummary = "Permanently abandon a development branch" | 25 | helpSummary = "Permanently abandon a development branch" |
25 | helpUsage = """ | 26 | helpUsage = """ |
26 | %prog <branchname> [<project>...] | 27 | %prog [--all | <branchname>] [<project>...] |
27 | 28 | ||
28 | This subcommand permanently abandons a development branch by | 29 | This subcommand permanently abandons a development branch by |
29 | deleting it (and all its history) from your local repository. | 30 | deleting it (and all its history) from your local repository. |
30 | 31 | ||
31 | It is equivalent to "git branch -D <branchname>". | 32 | It is equivalent to "git branch -D <branchname>". |
32 | """ | 33 | """ |
34 | def _Options(self, p): | ||
35 | p.add_option('--all', | ||
36 | dest='all', action='store_true', | ||
37 | help='delete all branches in all projects') | ||
33 | 38 | ||
34 | def Execute(self, opt, args): | 39 | def Execute(self, opt, args): |
35 | if not args: | 40 | if not opt.all and not args: |
36 | self.Usage() | 41 | self.Usage() |
37 | 42 | ||
38 | nb = args[0] | 43 | if not opt.all: |
39 | if not git.check_ref_format('heads/%s' % nb): | 44 | nb = args[0] |
40 | print("error: '%s' is not a valid name" % nb, file=sys.stderr) | 45 | if not git.check_ref_format('heads/%s' % nb): |
41 | sys.exit(1) | 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'" | ||
42 | 51 | ||
43 | err = [] | 52 | err = defaultdict(list) |
44 | success = [] | 53 | success = defaultdict(list) |
45 | all_projects = self.GetProjects(args[1:]) | 54 | all_projects = self.GetProjects(args[1:]) |
46 | 55 | ||
47 | pm = Progress('Abandon %s' % nb, len(all_projects)) | 56 | pm = Progress('Abandon %s' % nb, len(all_projects)) |
48 | for project in all_projects: | 57 | for project in all_projects: |
49 | pm.update() | 58 | pm.update() |
50 | 59 | ||
51 | status = project.AbandonBranch(nb) | 60 | if opt.all: |
52 | if status is not None: | 61 | branches = project.GetBranches().keys() |
53 | if status: | 62 | else: |
54 | success.append(project) | 63 | branches = [nb] |
55 | else: | 64 | |
56 | err.append(project) | 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) | ||
57 | pm.end() | 72 | pm.end() |
58 | 73 | ||
74 | width = 25 | ||
75 | for name in branches: | ||
76 | if width < len(name): | ||
77 | width = len(name) | ||
78 | |||
59 | if err: | 79 | if err: |
60 | for p in err: | 80 | for br in err.keys(): |
61 | print("error: %s/: cannot abandon %s" % (p.relpath, nb), | 81 | err_msg = "error: cannot abandon %s" %br |
62 | file=sys.stderr) | 82 | print(err_msg, file=sys.stderr) |
83 | for proj in err[br]: | ||
84 | print(' '*len(err_msg) + " | %s" % p.relpath, file=sys.stderr) | ||
63 | sys.exit(1) | 85 | sys.exit(1) |
64 | elif not success: | 86 | elif not success: |
65 | print('error: no project has branch %s' % nb, file=sys.stderr) | 87 | print('error: no project has local branch(es) : %s' % nb, |
88 | file=sys.stderr) | ||
66 | sys.exit(1) | 89 | sys.exit(1) |
67 | else: | 90 | else: |
68 | print('Abandoned in %d project(s):\n %s' | 91 | print('Abandoned branches:', file=sys.stderr) |
69 | % (len(success), '\n '.join(p.relpath for p in success)), | 92 | for br in success.keys(): |
70 | file=sys.stderr) | 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) | ||