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