abandon.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2008 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections import defaultdict
  15. import sys
  16. from command import Command
  17. from git_command import git
  18. from progress import Progress
  19. class Abandon(Command):
  20. common = True
  21. helpSummary = "Permanently abandon a development branch"
  22. helpUsage = """
  23. %prog [--all | <branchname>] [<project>...]
  24. This subcommand permanently abandons a development branch by
  25. deleting it (and all its history) from your local repository.
  26. It is equivalent to "git branch -D <branchname>".
  27. """
  28. def _Options(self, p):
  29. p.add_option('-q', '--quiet',
  30. action='store_true', default=False,
  31. help='be quiet')
  32. p.add_option('--all',
  33. dest='all', action='store_true',
  34. help='delete all branches in all projects')
  35. def ValidateOptions(self, opt, args):
  36. if not opt.all and not args:
  37. self.Usage()
  38. if not opt.all:
  39. nb = args[0]
  40. if not git.check_ref_format('heads/%s' % nb):
  41. self.OptionParser.error("'%s' is not a valid branch name" % nb)
  42. else:
  43. args.insert(0, "'All local branches'")
  44. def Execute(self, opt, args):
  45. nb = args[0]
  46. err = defaultdict(list)
  47. success = defaultdict(list)
  48. all_projects = self.GetProjects(args[1:])
  49. pm = Progress('Abandon %s' % nb, len(all_projects))
  50. for project in all_projects:
  51. pm.update()
  52. if opt.all:
  53. branches = list(project.GetBranches().keys())
  54. else:
  55. branches = [nb]
  56. for name in branches:
  57. status = project.AbandonBranch(name)
  58. if status is not None:
  59. if status:
  60. success[name].append(project)
  61. else:
  62. err[name].append(project)
  63. pm.end()
  64. width = 25
  65. for name in branches:
  66. if width < len(name):
  67. width = len(name)
  68. if err:
  69. for br in err.keys():
  70. err_msg = "error: cannot abandon %s" % br
  71. print(err_msg, file=sys.stderr)
  72. for proj in err[br]:
  73. print(' ' * len(err_msg) + " | %s" % proj.relpath, file=sys.stderr)
  74. sys.exit(1)
  75. elif not success:
  76. print('error: no project has local branch(es) : %s' % nb,
  77. file=sys.stderr)
  78. sys.exit(1)
  79. else:
  80. # Everything below here is displaying status.
  81. if opt.quiet:
  82. return
  83. print('Abandoned branches:')
  84. for br in success.keys():
  85. if len(all_projects) > 1 and len(all_projects) == len(success[br]):
  86. result = "all project"
  87. else:
  88. result = "%s" % (
  89. ('\n' + ' ' * width + '| ').join(p.relpath for p in success[br]))
  90. print("%s%s| %s\n" % (br, ' ' * (width - len(br)), result))