git_reparent_branch.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  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. """Change the upstream of the current branch."""
  6. import argparse
  7. import sys
  8. import subprocess2
  9. from git_common import upstream, current_branch, run, tags, set_branch_config
  10. from git_common import get_or_create_merge_base, root, manual_merge_base
  11. from git_common import get_branch_tree, topo_iter
  12. import git_rebase_update
  13. import metrics
  14. @metrics.collector.collect_metrics('git reparent-branch')
  15. def main(args):
  16. root_ref = root()
  17. parser = argparse.ArgumentParser()
  18. g = parser.add_mutually_exclusive_group()
  19. g.add_argument('new_parent', nargs='?',
  20. help='New parent branch (or tag) to reparent to.')
  21. g.add_argument('--root', action='store_true',
  22. help='Reparent to the configured root branch (%s).' % root_ref)
  23. g.add_argument('--lkgr', action='store_true',
  24. help='Reparent to the lkgr tag.')
  25. opts = parser.parse_args(args)
  26. # TODO(iannucci): Allow specification of the branch-to-reparent
  27. branch = current_branch()
  28. if opts.root:
  29. new_parent = root_ref
  30. elif opts.lkgr:
  31. new_parent = 'lkgr'
  32. else:
  33. if not opts.new_parent:
  34. parser.error('Must specify new parent somehow')
  35. new_parent = opts.new_parent
  36. cur_parent = upstream(branch)
  37. if branch == 'HEAD' or not branch:
  38. parser.error('Must be on the branch you want to reparent')
  39. if new_parent == cur_parent:
  40. parser.error('Cannot reparent a branch to its existing parent')
  41. if not cur_parent:
  42. msg = (
  43. "Unable to determine %s@{upstream}.\n\nThis can happen if you didn't use "
  44. "`git new-branch` to create the branch and haven't used "
  45. "`git branch --set-upstream-to` to assign it one.\n\nPlease assign an "
  46. "upstream branch and then run this command again."
  47. )
  48. print(msg % branch, file=sys.stderr)
  49. return 1
  50. mbase = get_or_create_merge_base(branch, cur_parent)
  51. all_tags = tags()
  52. if cur_parent in all_tags:
  53. cur_parent += ' [tag]'
  54. try:
  55. run('show-ref', new_parent)
  56. except subprocess2.CalledProcessError:
  57. print('fatal: invalid reference: %s' % new_parent, file=sys.stderr)
  58. return 1
  59. if new_parent in all_tags:
  60. print("Reparenting %s to track %s [tag] (was %s)" % (branch, new_parent,
  61. cur_parent))
  62. set_branch_config(branch, 'remote', '.')
  63. set_branch_config(branch, 'merge', new_parent)
  64. else:
  65. print("Reparenting %s to track %s (was %s)" % (branch, new_parent,
  66. cur_parent))
  67. run('branch', '--set-upstream-to', new_parent, branch)
  68. manual_merge_base(branch, mbase, new_parent)
  69. # ONLY rebase-update the branch which moved (and dependants)
  70. _, branch_tree = get_branch_tree()
  71. branches = [branch]
  72. for branch, parent in topo_iter(branch_tree):
  73. if parent in branches:
  74. branches.append(branch)
  75. return git_rebase_update.main(['--no-fetch', '--keep-empty'] + branches)
  76. if __name__ == '__main__': # pragma: no cover
  77. try:
  78. with metrics.collector.print_notice_and_exit():
  79. sys.exit(main(sys.argv[1:]))
  80. except KeyboardInterrupt:
  81. sys.stderr.write('interrupted\n')
  82. sys.exit(1)