git_new_branch.py 3.6 KB

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