git_rename_branch.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. # Copyright 2014 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Rename the current branch while maintaining correct dependencies."""
  6. import argparse
  7. import sys
  8. import subprocess2
  9. from git_common import current_branch, run, set_branch_config, branch_config
  10. from git_common import branch_config_map
  11. def main(args):
  12. current = current_branch()
  13. if current == 'HEAD':
  14. current = None
  15. old_name_help = 'The old branch to rename.'
  16. if current:
  17. old_name_help += ' (default %(default)r)'
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument('old_name', nargs=('?' if current else 1),
  20. help=old_name_help, default=current)
  21. parser.add_argument('new_name', help='The new branch name.')
  22. opts = parser.parse_args(args)
  23. # when nargs=1, we get a list :(
  24. if isinstance(opts.old_name, list):
  25. opts.old_name = opts.old_name[0]
  26. try:
  27. run('branch', '-m', opts.old_name, opts.new_name)
  28. # update the downstreams
  29. for branch, merge in branch_config_map('merge').iteritems():
  30. if merge == 'refs/heads/' + opts.old_name:
  31. # Only care about local branches
  32. if branch_config(branch, 'remote') == '.':
  33. set_branch_config(branch, 'merge', 'refs/heads/' + opts.new_name)
  34. except subprocess2.CalledProcessError as cpe:
  35. sys.stderr.write(cpe.stderr)
  36. return 1
  37. if __name__ == '__main__': # pragma: no cover
  38. sys.exit(main(sys.argv[1:]))