Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 | """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 |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 14 | from git_common import get_or_create_merge_base, root, manual_merge_base |
Henrique Ferreiro | 9f273d0 | 2019-02-22 10:10:27 +0000 | [diff] [blame] | 15 | from git_common import get_branch_tree, topo_iter |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 16 | |
| 17 | import git_rebase_update |
Anthony Polito | 87e7389 | 2021-07-09 19:15:11 +0000 | [diff] [blame] | 18 | import metrics |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 19 | |
Anthony Polito | 87e7389 | 2021-07-09 19:15:11 +0000 | [diff] [blame] | 20 | @metrics.collector.collect_metrics('git reparent-branch') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 21 | def main(args): |
| 22 | root_ref = root() |
| 23 | |
| 24 | parser = argparse.ArgumentParser() |
| 25 | g = parser.add_mutually_exclusive_group() |
| 26 | g.add_argument('new_parent', nargs='?', |
| 27 | help='New parent branch (or tag) to reparent to.') |
| 28 | g.add_argument('--root', action='store_true', |
| 29 | help='Reparent to the configured root branch (%s).' % root_ref) |
| 30 | g.add_argument('--lkgr', action='store_true', |
| 31 | help='Reparent to the lkgr tag.') |
| 32 | opts = parser.parse_args(args) |
| 33 | |
| 34 | # TODO(iannucci): Allow specification of the branch-to-reparent |
| 35 | |
| 36 | branch = current_branch() |
iannucci@chromium.org | f3e37a0 | 2015-12-04 19:03:23 +0000 | [diff] [blame] | 37 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 38 | if opts.root: |
| 39 | new_parent = root_ref |
| 40 | elif opts.lkgr: |
| 41 | new_parent = 'lkgr' |
| 42 | else: |
tandrii@chromium.org | 41a9ce4 | 2015-08-06 17:57:12 +0000 | [diff] [blame] | 43 | if not opts.new_parent: |
| 44 | parser.error('Must specify new parent somehow') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 45 | new_parent = opts.new_parent |
| 46 | cur_parent = upstream(branch) |
| 47 | |
| 48 | if branch == 'HEAD' or not branch: |
| 49 | parser.error('Must be on the branch you want to reparent') |
| 50 | if new_parent == cur_parent: |
| 51 | parser.error('Cannot reparent a branch to its existing parent') |
| 52 | |
iannucci@chromium.org | f3e37a0 | 2015-12-04 19:03:23 +0000 | [diff] [blame] | 53 | if not cur_parent: |
| 54 | msg = ( |
| 55 | "Unable to determine %s@{upstream}.\n\nThis can happen if you didn't use " |
| 56 | "`git new-branch` to create the branch and haven't used " |
| 57 | "`git branch --set-upstream-to` to assign it one.\n\nPlease assign an " |
| 58 | "upstream branch and then run this command again." |
| 59 | ) |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 60 | print(msg % branch, file=sys.stderr) |
iannucci@chromium.org | f3e37a0 | 2015-12-04 19:03:23 +0000 | [diff] [blame] | 61 | return 1 |
| 62 | |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 63 | mbase = get_or_create_merge_base(branch, cur_parent) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 64 | |
| 65 | all_tags = tags() |
| 66 | if cur_parent in all_tags: |
| 67 | cur_parent += ' [tag]' |
| 68 | |
| 69 | try: |
| 70 | run('show-ref', new_parent) |
| 71 | except subprocess2.CalledProcessError: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 72 | print('fatal: invalid reference: %s' % new_parent, file=sys.stderr) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 73 | return 1 |
| 74 | |
| 75 | if new_parent in all_tags: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 76 | print("Reparenting %s to track %s [tag] (was %s)" % (branch, new_parent, |
| 77 | cur_parent)) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 78 | set_branch_config(branch, 'remote', '.') |
| 79 | set_branch_config(branch, 'merge', new_parent) |
| 80 | else: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 81 | print("Reparenting %s to track %s (was %s)" % (branch, new_parent, |
| 82 | cur_parent)) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 83 | run('branch', '--set-upstream-to', new_parent, branch) |
| 84 | |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 85 | manual_merge_base(branch, mbase, new_parent) |
| 86 | |
Henrique Ferreiro | 9f273d0 | 2019-02-22 10:10:27 +0000 | [diff] [blame] | 87 | # ONLY rebase-update the branch which moved (and dependants) |
| 88 | _, branch_tree = get_branch_tree() |
| 89 | branches = [branch] |
| 90 | for branch, parent in topo_iter(branch_tree): |
| 91 | if parent in branches: |
| 92 | branches.append(branch) |
Robert Iannucci | 7c59d27 | 2023-07-12 17:49:54 +0000 | [diff] [blame] | 93 | return git_rebase_update.main(['--no-fetch', '--keep-empty'] + branches) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | if __name__ == '__main__': # pragma: no cover |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 97 | try: |
Anthony Polito | 87e7389 | 2021-07-09 19:15:11 +0000 | [diff] [blame] | 98 | with metrics.collector.print_notice_and_exit(): |
| 99 | sys.exit(main(sys.argv[1:])) |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 100 | except KeyboardInterrupt: |
| 101 | sys.stderr.write('interrupted\n') |
| 102 | sys.exit(1) |