git_new_branch.py 3.4 KB

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