abandon.py 3.0 KB

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