[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 1 | #!/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 | |
| 8 | import argparse |
| 9 | import sys |
| 10 | |
| 11 | import subprocess2 |
| 12 | |
| 13 | from git_common import upstream, current_branch, run, tags, set_branch_config |
[email protected] | 10fbe87 | 2014-05-16 22:31:13 | [diff] [blame] | 14 | from git_common import get_or_create_merge_base, root, manual_merge_base |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 15 | |
| 16 | import git_rebase_update |
| 17 | |
| 18 | def 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() |
[email protected] | f3e37a0 | 2015-12-04 19:03:23 | [diff] [blame] | 34 | |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 35 | if opts.root: |
| 36 | new_parent = root_ref |
| 37 | elif opts.lkgr: |
| 38 | new_parent = 'lkgr' |
| 39 | else: |
[email protected] | 41a9ce4 | 2015-08-06 17:57:12 | [diff] [blame] | 40 | if not opts.new_parent: |
| 41 | parser.error('Must specify new parent somehow') |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 42 | new_parent = opts.new_parent |
| 43 | cur_parent = upstream(branch) |
| 44 | |
| 45 | if branch == 'HEAD' or not branch: |
| 46 | parser.error('Must be on the branch you want to reparent') |
| 47 | if new_parent == cur_parent: |
| 48 | parser.error('Cannot reparent a branch to its existing parent') |
| 49 | |
[email protected] | f3e37a0 | 2015-12-04 19:03:23 | [diff] [blame] | 50 | if not cur_parent: |
| 51 | msg = ( |
| 52 | "Unable to determine %s@{upstream}.\n\nThis can happen if you didn't use " |
| 53 | "`git new-branch` to create the branch and haven't used " |
| 54 | "`git branch --set-upstream-to` to assign it one.\n\nPlease assign an " |
| 55 | "upstream branch and then run this command again." |
| 56 | ) |
| 57 | print >> sys.stderr, msg % branch |
| 58 | return 1 |
| 59 | |
[email protected] | 10fbe87 | 2014-05-16 22:31:13 | [diff] [blame] | 60 | mbase = get_or_create_merge_base(branch, cur_parent) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 61 | |
| 62 | all_tags = tags() |
| 63 | if cur_parent in all_tags: |
| 64 | cur_parent += ' [tag]' |
| 65 | |
| 66 | try: |
| 67 | run('show-ref', new_parent) |
| 68 | except subprocess2.CalledProcessError: |
| 69 | print >> sys.stderr, 'fatal: invalid reference: %s' % new_parent |
| 70 | return 1 |
| 71 | |
| 72 | if new_parent in all_tags: |
| 73 | print ("Reparenting %s to track %s [tag] (was %s)" |
| 74 | % (branch, new_parent, cur_parent)) |
| 75 | set_branch_config(branch, 'remote', '.') |
| 76 | set_branch_config(branch, 'merge', new_parent) |
| 77 | else: |
| 78 | print ("Reparenting %s to track %s (was %s)" |
| 79 | % (branch, new_parent, cur_parent)) |
| 80 | run('branch', '--set-upstream-to', new_parent, branch) |
| 81 | |
[email protected] | 10fbe87 | 2014-05-16 22:31:13 | [diff] [blame] | 82 | manual_merge_base(branch, mbase, new_parent) |
| 83 | |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 84 | # TODO(iannucci): ONLY rebase-update the branch which moved (and dependants) |
[email protected] | b9f2751 | 2014-08-08 15:52:33 | [diff] [blame] | 85 | return git_rebase_update.main(['--no-fetch']) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == '__main__': # pragma: no cover |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 89 | try: |
| 90 | sys.exit(main(sys.argv[1:])) |
| 91 | except KeyboardInterrupt: |
| 92 | sys.stderr.write('interrupted\n') |
| 93 | sys.exit(1) |