git_new_branch.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. """
  6. Create new branch tracking origin HEAD by default.
  7. """
  8. import argparse
  9. import sys
  10. import git_common
  11. import subprocess2
  12. def create_new_branch(
  13. branch_name, upstream_current=False, upstream=None, inject_current=False):
  14. upstream = upstream or git_common.root()
  15. try:
  16. if inject_current:
  17. below = git_common.current_branch()
  18. if below is None:
  19. raise Exception('no current branch')
  20. above = git_common.upstream(below)
  21. if above is None:
  22. raise Exception('branch %s has no upstream' % (below))
  23. git_common.run('checkout', '--track', above, '-b', branch_name)
  24. git_common.run('branch', '--set-upstream-to', branch_name, below)
  25. elif upstream_current:
  26. git_common.run('checkout', '--track', '-b', branch_name)
  27. else:
  28. if upstream in git_common.tags():
  29. # TODO(iannucci): ensure that basis_ref is an ancestor of HEAD?
  30. git_common.run(
  31. 'checkout', '--no-track', '-b', branch_name,
  32. git_common.hash_one(upstream))
  33. git_common.set_config('branch.%s.remote' % branch_name, '.')
  34. git_common.set_config('branch.%s.merge' % branch_name, upstream)
  35. else:
  36. # TODO(iannucci): Detect unclean workdir then stash+pop if we need to
  37. # teleport to a conflicting portion of history?
  38. git_common.run('checkout', '--track', upstream, '-b', branch_name)
  39. git_common.get_or_create_merge_base(branch_name)
  40. except subprocess2.CalledProcessError as cpe:
  41. sys.stdout.write(cpe.stdout.decode('utf-8', 'replace'))
  42. sys.stderr.write(cpe.stderr.decode('utf-8', 'replace'))
  43. return 1
  44. sys.stderr.write('Switched to branch %s.\n' % branch_name)
  45. return 0
  46. def main(args):
  47. parser = argparse.ArgumentParser(
  48. formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  49. description=__doc__,
  50. )
  51. parser.add_argument('branch_name')
  52. g = parser.add_mutually_exclusive_group()
  53. g.add_argument('--upstream-current', '--upstream_current',
  54. action='store_true',
  55. help='set upstream branch to current branch.')
  56. g.add_argument('--upstream', metavar='REF',
  57. help='upstream branch (or tag) to track.')
  58. g.add_argument('--inject-current', '--inject_current',
  59. action='store_true',
  60. help='new branch adopts current branch\'s upstream,' +
  61. ' and new branch becomes current branch\'s upstream.')
  62. g.add_argument('--lkgr', action='store_const', const='lkgr', dest='upstream',
  63. help='set basis ref for new branch to lkgr.')
  64. opts = parser.parse_args(args)
  65. return create_new_branch(
  66. opts.branch_name, opts.upstream_current, opts.upstream,
  67. opts.inject_current)
  68. if __name__ == '__main__': # pragma: no cover
  69. try:
  70. sys.exit(main(sys.argv[1:]))
  71. except KeyboardInterrupt:
  72. sys.stderr.write('interrupted\n')
  73. sys.exit(1)