git_reparent_branch.py 3.2 KB

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