Edward Lesmes | 98eda3f | 2019-08-12 21:09:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 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. |
techtonik@gmail.com | b14fccd | 2016-04-20 04:10:09 +0000 | [diff] [blame] | 5 | """ |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame^] | 6 | Create new branch tracking origin HEAD by default. |
techtonik@gmail.com | b14fccd | 2016-04-20 04:10:09 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 9 | import argparse |
| 10 | import sys |
| 11 | |
Edward Lemur | 8410164 | 2020-02-21 21:40:34 +0000 | [diff] [blame] | 12 | import git_common |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 13 | import subprocess2 |
| 14 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 15 | |
Edward Lemur | 8410164 | 2020-02-21 21:40:34 +0000 | [diff] [blame] | 16 | def create_new_branch( |
| 17 | branch_name, upstream_current=False, upstream=None, inject_current=False): |
| 18 | upstream = upstream or git_common.root() |
| 19 | try: |
| 20 | if inject_current: |
| 21 | below = git_common.current_branch() |
| 22 | if below is None: |
| 23 | raise Exception('no current branch') |
| 24 | above = git_common.upstream(below) |
| 25 | if above is None: |
| 26 | raise Exception('branch %s has no upstream' % (below)) |
| 27 | git_common.run('checkout', '--track', above, '-b', branch_name) |
| 28 | git_common.run('branch', '--set-upstream-to', branch_name, below) |
| 29 | elif upstream_current: |
| 30 | git_common.run('checkout', '--track', '-b', branch_name) |
| 31 | else: |
| 32 | if upstream in git_common.tags(): |
| 33 | # TODO(iannucci): ensure that basis_ref is an ancestor of HEAD? |
| 34 | git_common.run( |
| 35 | 'checkout', '--no-track', '-b', branch_name, |
| 36 | git_common.hash_one(upstream)) |
| 37 | git_common.set_config('branch.%s.remote' % branch_name, '.') |
| 38 | git_common.set_config('branch.%s.merge' % branch_name, upstream) |
| 39 | else: |
| 40 | # TODO(iannucci): Detect unclean workdir then stash+pop if we need to |
| 41 | # teleport to a conflicting portion of history? |
| 42 | git_common.run('checkout', '--track', upstream, '-b', branch_name) |
| 43 | git_common.get_or_create_merge_base(branch_name) |
| 44 | except subprocess2.CalledProcessError as cpe: |
| 45 | sys.stdout.write(cpe.stdout.decode('utf-8', 'replace')) |
| 46 | sys.stderr.write(cpe.stderr.decode('utf-8', 'replace')) |
| 47 | return 1 |
| 48 | sys.stderr.write('Switched to branch %s.\n' % branch_name) |
| 49 | return 0 |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 50 | |
| 51 | def main(args): |
| 52 | parser = argparse.ArgumentParser( |
techtonik@gmail.com | b14fccd | 2016-04-20 04:10:09 +0000 | [diff] [blame] | 53 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 54 | description=__doc__, |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 55 | ) |
| 56 | parser.add_argument('branch_name') |
| 57 | g = parser.add_mutually_exclusive_group() |
pgervais@chromium.org | b9f2751 | 2014-08-08 15:52:33 +0000 | [diff] [blame] | 58 | g.add_argument('--upstream-current', '--upstream_current', |
| 59 | action='store_true', |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 60 | help='set upstream branch to current branch.') |
Edward Lemur | 8410164 | 2020-02-21 21:40:34 +0000 | [diff] [blame] | 61 | g.add_argument('--upstream', metavar='REF', |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 62 | help='upstream branch (or tag) to track.') |
Asanka Herath | 53a115e | 2018-04-12 19:07:37 -0400 | [diff] [blame] | 63 | g.add_argument('--inject-current', '--inject_current', |
| 64 | action='store_true', |
| 65 | help='new branch adopts current branch\'s upstream,' + |
| 66 | ' and new branch becomes current branch\'s upstream.') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 67 | g.add_argument('--lkgr', action='store_const', const='lkgr', dest='upstream', |
| 68 | help='set basis ref for new branch to lkgr.') |
| 69 | |
| 70 | opts = parser.parse_args(args) |
| 71 | |
Edward Lemur | 8410164 | 2020-02-21 21:40:34 +0000 | [diff] [blame] | 72 | return create_new_branch( |
| 73 | opts.branch_name, opts.upstream_current, opts.upstream, |
| 74 | opts.inject_current) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | if __name__ == '__main__': # pragma: no cover |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 78 | try: |
| 79 | sys.exit(main(sys.argv[1:])) |
| 80 | except KeyboardInterrupt: |
| 81 | sys.stderr.write('interrupted\n') |
| 82 | sys.exit(1) |