abandon.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. import sys
  18. from command import Command
  19. from collections import defaultdict
  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('--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. print('Abandoned branches:', file=sys.stderr)
  81. for br in success.keys():
  82. if len(all_projects) > 1 and len(all_projects) == len(success[br]):
  83. result = "all project"
  84. else:
  85. result = "%s" % (
  86. ('\n'+' '*width + '| ').join(p.relpath for p in success[br]))
  87. print("%s%s| %s\n" % (br,' '*(width-len(br)), result),file=sys.stderr)