abandon.py 3.2 KB

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