git_reparent_branch.py 3.3 KB

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