abandon.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. nb = args[0]
  37. err = []
  38. success = []
  39. all_projects = self.GetProjects(args[1:])
  40. pm = Progress('Abandon %s' % nb, len(all_projects))
  41. for project in all_projects:
  42. pm.update()
  43. status = project.AbandonBranch(nb)
  44. if status is not None:
  45. if status:
  46. success.append(project)
  47. else:
  48. err.append(project)
  49. pm.end()
  50. if err:
  51. for p in err:
  52. print("error: %s/: cannot abandon %s" % (p.relpath, nb),
  53. file=sys.stderr)
  54. sys.exit(1)
  55. elif not success:
  56. print('error: no project has branch %s' % nb, file=sys.stderr)
  57. sys.exit(1)
  58. else:
  59. print('Abandoned in %d project(s):\n %s'
  60. % (len(success), '\n '.join(p.relpath for p in success)),
  61. file=sys.stderr)