blob: 105da17d004a68c8106a1bb431d9624ee2e01c5c [file] [log] [blame]
iannucci@chromium.org97231b52014-03-26 06:54:55 +00001#!/usr/bin/env python
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00002# 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.comb14fccd2016-04-20 04:10:09 +00005"""
6Create new branch tracking origin/master by default.
7"""
8
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00009import argparse
10import sys
11
12import subprocess2
13
14from git_common import run, root, set_config, get_or_create_merge_base, tags
Asanka Herath53a115e2018-04-12 19:07:37 -040015from git_common import hash_one, upstream, set_branch_config, current_branch
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000016
17
18def main(args):
19 parser = argparse.ArgumentParser(
techtonik@gmail.comb14fccd2016-04-20 04:10:09 +000020 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
21 description=__doc__,
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000022 )
23 parser.add_argument('branch_name')
24 g = parser.add_mutually_exclusive_group()
pgervais@chromium.orgb9f27512014-08-08 15:52:33 +000025 g.add_argument('--upstream-current', '--upstream_current',
26 action='store_true',
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000027 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 Herath53a115e2018-04-12 19:07:37 -040030 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.orgc050a5b2014-03-26 06:18:50 +000034 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 Herath53a115e2018-04-12 19:07:37 -040040 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.orgc050a5b2014-03-26 06:18:50 +000050 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 Hammacherba832292018-12-04 09:41:17 +000061 run('checkout', '-b', opts.branch_name)
62 run('branch', '--set-upstream-to', opts.upstream)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000063 get_or_create_merge_base(opts.branch_name)
64 except subprocess2.CalledProcessError as cpe:
65 sys.stdout.write(cpe.stdout)
66 sys.stderr.write(cpe.stderr)
67 return 1
agable@chromium.org9c0f8512014-05-01 20:23:30 +000068 sys.stderr.write('Switched to branch %s.\n' % opts.branch_name)
sbc@chromium.org013731e2015-02-26 18:28:43 +000069 return 0
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000070
71
72if __name__ == '__main__': # pragma: no cover
sbc@chromium.org013731e2015-02-26 18:28:43 +000073 try:
74 sys.exit(main(sys.argv[1:]))
75 except KeyboardInterrupt:
76 sys.stderr.write('interrupted\n')
77 sys.exit(1)