abandon.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import print_function
  16. import sys
  17. from command import Command
  18. from git_command import git
  19. from progress import Progress
  20. class Abandon(Command):
  21. common = True
  22. helpSummary = "Permanently abandon a development branch"
  23. helpUsage = """
  24. %prog <branchname> [<project>...]
  25. This subcommand permanently abandons a development branch by
  26. deleting it (and all its history) from your local repository.
  27. It is equivalent to "git branch -D <branchname>".
  28. """
  29. def Execute(self, opt, args):
  30. if not args:
  31. self.Usage()
  32. nb = args[0]
  33. if not git.check_ref_format('heads/%s' % nb):
  34. print("error: '%s' is not a valid name" % nb, file=sys.stderr)
  35. sys.exit(1)
  36. err = []
  37. success = []
  38. all_projects = self.GetProjects(args[1:])
  39. pm = Progress('Abandon %s' % nb, len(all_projects))
  40. for project in all_projects:
  41. pm.update()
  42. status = project.AbandonBranch(nb)
  43. if status is not None:
  44. if status:
  45. success.append(project)
  46. else:
  47. err.append(project)
  48. pm.end()
  49. if err:
  50. for p in err:
  51. print("error: %s/: cannot abandon %s" % (p.relpath, nb),
  52. file=sys.stderr)
  53. sys.exit(1)
  54. elif not success:
  55. print('error: no project has branch %s' % nb, file=sys.stderr)
  56. sys.exit(1)
  57. else:
  58. print('Abandoned in %d project(s):\n %s'
  59. % (len(success), '\n '.join(p.relpath for p in success)),
  60. file=sys.stderr)