[email protected] | 97231b5 | 2014-03-26 06:54:55 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [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. |
| 5 | |
[email protected] | b14fccd | 2016-04-20 04:10:09 | [diff] [blame] | 6 | """ |
| 7 | Create new branch tracking origin/master by default. |
| 8 | """ |
| 9 | |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 10 | import argparse |
| 11 | import sys |
| 12 | |
| 13 | import subprocess2 |
| 14 | |
| 15 | from git_common import run, root, set_config, get_or_create_merge_base, tags |
| 16 | from git_common import hash_one |
| 17 | |
| 18 | |
| 19 | def main(args): |
| 20 | parser = argparse.ArgumentParser( |
[email protected] | b14fccd | 2016-04-20 04:10:09 | [diff] [blame] | 21 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 22 | description=__doc__, |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 23 | ) |
| 24 | parser.add_argument('branch_name') |
| 25 | g = parser.add_mutually_exclusive_group() |
[email protected] | b9f2751 | 2014-08-08 15:52:33 | [diff] [blame] | 26 | g.add_argument('--upstream-current', '--upstream_current', |
| 27 | action='store_true', |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 28 | help='set upstream branch to current branch.') |
| 29 | g.add_argument('--upstream', metavar='REF', default=root(), |
| 30 | help='upstream branch (or tag) to track.') |
| 31 | g.add_argument('--lkgr', action='store_const', const='lkgr', dest='upstream', |
| 32 | help='set basis ref for new branch to lkgr.') |
| 33 | |
| 34 | opts = parser.parse_args(args) |
| 35 | |
| 36 | try: |
| 37 | if opts.upstream_current: |
| 38 | run('checkout', '--track', '-b', opts.branch_name) |
| 39 | else: |
| 40 | if opts.upstream in tags(): |
| 41 | # TODO(iannucci): ensure that basis_ref is an ancestor of HEAD? |
| 42 | run('checkout', '--no-track', '-b', opts.branch_name, |
| 43 | hash_one(opts.upstream)) |
| 44 | set_config('branch.%s.remote' % opts.branch_name, '.') |
| 45 | set_config('branch.%s.merge' % opts.branch_name, opts.upstream) |
| 46 | else: |
| 47 | # TODO(iannucci): Detect unclean workdir then stash+pop if we need to |
| 48 | # teleport to a conflicting portion of history? |
| 49 | run('checkout', '--track', opts.upstream, '-b', opts.branch_name) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 50 | get_or_create_merge_base(opts.branch_name) |
| 51 | except subprocess2.CalledProcessError as cpe: |
| 52 | sys.stdout.write(cpe.stdout) |
| 53 | sys.stderr.write(cpe.stderr) |
| 54 | return 1 |
[email protected] | 9c0f851 | 2014-05-01 20:23:30 | [diff] [blame] | 55 | sys.stderr.write('Switched to branch %s.\n' % opts.branch_name) |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 56 | return 0 |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 57 | |
| 58 | |
| 59 | if __name__ == '__main__': # pragma: no cover |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 60 | try: |
| 61 | sys.exit(main(sys.argv[1:])) |
| 62 | except KeyboardInterrupt: |
| 63 | sys.stderr.write('interrupted\n') |
| 64 | sys.exit(1) |