git_new_branch.py 2.8 KB

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