blob: be44bc6cbfe516b8a02cbc5621be50694e292260 [file] [log] [blame]
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00001#!/usr/bin/env python
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"""Change the upstream of the current branch."""
7
8import argparse
9import sys
10
11import subprocess2
12
13from git_common import upstream, current_branch, run, tags, set_branch_config
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000014from git_common import get_or_create_merge_base, root, manual_merge_base
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000015
16import git_rebase_update
17
18def main(args):
19 root_ref = root()
20
21 parser = argparse.ArgumentParser()
22 g = parser.add_mutually_exclusive_group()
23 g.add_argument('new_parent', nargs='?',
24 help='New parent branch (or tag) to reparent to.')
25 g.add_argument('--root', action='store_true',
26 help='Reparent to the configured root branch (%s).' % root_ref)
27 g.add_argument('--lkgr', action='store_true',
28 help='Reparent to the lkgr tag.')
29 opts = parser.parse_args(args)
30
31 # TODO(iannucci): Allow specification of the branch-to-reparent
32
33 branch = current_branch()
34 if opts.root:
35 new_parent = root_ref
36 elif opts.lkgr:
37 new_parent = 'lkgr'
38 else:
tandrii@chromium.org41a9ce42015-08-06 17:57:12 +000039 if not opts.new_parent:
40 parser.error('Must specify new parent somehow')
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000041 new_parent = opts.new_parent
42 cur_parent = upstream(branch)
43
44 if branch == 'HEAD' or not branch:
45 parser.error('Must be on the branch you want to reparent')
46 if new_parent == cur_parent:
47 parser.error('Cannot reparent a branch to its existing parent')
48
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000049 mbase = get_or_create_merge_base(branch, cur_parent)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000050
51 all_tags = tags()
52 if cur_parent in all_tags:
53 cur_parent += ' [tag]'
54
55 try:
56 run('show-ref', new_parent)
57 except subprocess2.CalledProcessError:
58 print >> sys.stderr, 'fatal: invalid reference: %s' % new_parent
59 return 1
60
61 if new_parent in all_tags:
62 print ("Reparenting %s to track %s [tag] (was %s)"
63 % (branch, new_parent, cur_parent))
64 set_branch_config(branch, 'remote', '.')
65 set_branch_config(branch, 'merge', new_parent)
66 else:
67 print ("Reparenting %s to track %s (was %s)"
68 % (branch, new_parent, cur_parent))
69 run('branch', '--set-upstream-to', new_parent, branch)
70
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000071 manual_merge_base(branch, mbase, new_parent)
72
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000073 # TODO(iannucci): ONLY rebase-update the branch which moved (and dependants)
pgervais@chromium.orgb9f27512014-08-08 15:52:33 +000074 return git_rebase_update.main(['--no-fetch'])
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000075
76
77if __name__ == '__main__': # pragma: no cover
sbc@chromium.org013731e2015-02-26 18:28:43 +000078 try:
79 sys.exit(main(sys.argv[1:]))
80 except KeyboardInterrupt:
81 sys.stderr.write('interrupted\n')
82 sys.exit(1)