git_reparent_branch.py 3.4 KB

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