git_squash_branch.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import argparse
  6. import sys
  7. import gclient_utils
  8. import git_common
  9. # Squash a branch, taking care to rebase the branch on top of the new commit
  10. # position of its upstream branch.
  11. def rebase_branch(branch, initial_hashes):
  12. print('Re-parenting branch %s.' % branch)
  13. assert initial_hashes[branch] == git_common.hash_one(branch)
  14. upstream_branch = git_common.upstream(branch)
  15. old_upstream_branch = initial_hashes[upstream_branch]
  16. # Because the branch's upstream has potentially changed from squashing it,
  17. # the current branch is rebased on top of the new upstream.
  18. git_common.run('rebase', '--onto', upstream_branch, old_upstream_branch,
  19. branch, '--update-refs')
  20. # Squashes all branches that are part of the subtree starting at `branch`.
  21. def rebase_subtree(branch, initial_hashes, downstream_branches):
  22. # Rebase us onto our parent
  23. rebase_branch(branch, initial_hashes)
  24. # Recurse on downstream branches, if any.
  25. for downstream_branch in downstream_branches[branch]:
  26. rebase_subtree(downstream_branch, initial_hashes, downstream_branches)
  27. def children_have_diverged(branch, downstream_branches, diverged_branches):
  28. # If we have no diverged branches, then no children have diverged.
  29. if not diverged_branches:
  30. return False
  31. # If we have diverged, then our children have diverged.
  32. if branch in diverged_branches:
  33. return True
  34. # If any of our children have diverged, then we need to return true.
  35. for downstream_branch in downstream_branches[branch]:
  36. if children_have_diverged(downstream_branch, downstream_branches,
  37. diverged_branches):
  38. return True
  39. return False
  40. def main(args):
  41. if gclient_utils.IsEnvCog():
  42. print('squash-branch command is not supported in non-git environment.',
  43. file=sys.stderr)
  44. return 1
  45. parser = argparse.ArgumentParser()
  46. parser.add_argument(
  47. '-m',
  48. '--message',
  49. metavar='<msg>',
  50. default=None,
  51. help='Use the given <msg> as the first line of the commit message.')
  52. opts = parser.parse_args(args)
  53. if git_common.is_dirty_git_tree('squash-branch'):
  54. return 1
  55. # Save off the current branch so we can return to it at the end.
  56. return_branch = git_common.current_branch()
  57. # Save the hashes before we mutate the tree so that we have all of the
  58. # necessary rebasing information.
  59. _, tree = git_common.get_branch_tree()
  60. initial_hashes = git_common.get_hashes(tree)
  61. downstream_branches = git_common.get_downstream_branches(tree)
  62. diverged_branches = git_common.get_diverged_branches(tree)
  63. # We won't be rebasing our squashed branch, so only check any potential
  64. # children
  65. for branch in downstream_branches[return_branch]:
  66. if children_have_diverged(branch, downstream_branches,
  67. diverged_branches):
  68. print('Cannot use `git squash-branch` since some children have '
  69. 'diverged from their upstream and could cause conflicts.')
  70. return 1
  71. git_common.squash_current_branch(opts.message)
  72. # Fixup our children with our new state.
  73. for branch in downstream_branches[return_branch]:
  74. rebase_subtree(branch, initial_hashes, downstream_branches)
  75. git_common.run('checkout', return_branch)
  76. return 0
  77. if __name__ == '__main__':
  78. try:
  79. sys.exit(main(sys.argv[1:]))
  80. except KeyboardInterrupt:
  81. sys.stderr.write('interrupted\n')
  82. sys.exit(1)