blob: ed1225dc408839d69624df29e720837832fd89c0 [file] [log] [blame]
[email protected]97231b52014-03-26 06:54:551#!/usr/bin/env python
[email protected]c050a5b2014-03-26 06:18:502# 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]b14fccd2016-04-20 04:10:096"""
7Create new branch tracking origin/master by default.
8"""
9
[email protected]c050a5b2014-03-26 06:18:5010import argparse
11import sys
12
13import subprocess2
14
15from git_common import run, root, set_config, get_or_create_merge_base, tags
16from git_common import hash_one
17
18
19def main(args):
20 parser = argparse.ArgumentParser(
[email protected]b14fccd2016-04-20 04:10:0921 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
22 description=__doc__,
[email protected]c050a5b2014-03-26 06:18:5023 )
24 parser.add_argument('branch_name')
25 g = parser.add_mutually_exclusive_group()
[email protected]b9f27512014-08-08 15:52:3326 g.add_argument('--upstream-current', '--upstream_current',
27 action='store_true',
[email protected]c050a5b2014-03-26 06:18:5028 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]c050a5b2014-03-26 06:18:5050 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]9c0f8512014-05-01 20:23:3055 sys.stderr.write('Switched to branch %s.\n' % opts.branch_name)
[email protected]013731e2015-02-26 18:28:4356 return 0
[email protected]c050a5b2014-03-26 06:18:5057
58
59if __name__ == '__main__': # pragma: no cover
[email protected]013731e2015-02-26 18:28:4360 try:
61 sys.exit(main(sys.argv[1:]))
62 except KeyboardInterrupt:
63 sys.stderr.write('interrupted\n')
64 sys.exit(1)