iannucci@chromium.org | 97231b5 | 2014-03-26 06:54:55 +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. |
| 5 | |
| 6 | import argparse |
| 7 | import sys |
| 8 | |
| 9 | import subprocess2 |
| 10 | |
| 11 | from git_common import run, root, set_config, get_or_create_merge_base, tags |
| 12 | from git_common import hash_one |
| 13 | |
| 14 | |
| 15 | def main(args): |
| 16 | parser = argparse.ArgumentParser( |
| 17 | formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 18 | ) |
| 19 | parser.add_argument('branch_name') |
| 20 | g = parser.add_mutually_exclusive_group() |
| 21 | g.add_argument('--upstream_current', action='store_true', |
| 22 | help='set upstream branch to current branch.') |
| 23 | g.add_argument('--upstream', metavar='REF', default=root(), |
| 24 | help='upstream branch (or tag) to track.') |
| 25 | g.add_argument('--lkgr', action='store_const', const='lkgr', dest='upstream', |
| 26 | help='set basis ref for new branch to lkgr.') |
| 27 | |
| 28 | opts = parser.parse_args(args) |
| 29 | |
| 30 | try: |
| 31 | if opts.upstream_current: |
| 32 | run('checkout', '--track', '-b', opts.branch_name) |
| 33 | else: |
| 34 | if opts.upstream in tags(): |
| 35 | # TODO(iannucci): ensure that basis_ref is an ancestor of HEAD? |
| 36 | run('checkout', '--no-track', '-b', opts.branch_name, |
| 37 | hash_one(opts.upstream)) |
| 38 | set_config('branch.%s.remote' % opts.branch_name, '.') |
| 39 | set_config('branch.%s.merge' % opts.branch_name, opts.upstream) |
| 40 | else: |
| 41 | # TODO(iannucci): Detect unclean workdir then stash+pop if we need to |
| 42 | # teleport to a conflicting portion of history? |
| 43 | run('checkout', '--track', opts.upstream, '-b', opts.branch_name) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 44 | get_or_create_merge_base(opts.branch_name) |
| 45 | except subprocess2.CalledProcessError as cpe: |
| 46 | sys.stdout.write(cpe.stdout) |
| 47 | sys.stderr.write(cpe.stderr) |
| 48 | return 1 |
pgervais@chromium.org | 022d06e | 2014-04-29 17:08:12 +0000 | [diff] [blame] | 49 | sys.stderr.write('Switched to branch %s.' % opts.branch_name) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | if __name__ == '__main__': # pragma: no cover |
| 53 | sys.exit(main(sys.argv[1:])) |