rebase.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2010 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import sys
  18. from command import Command
  19. from git_command import GitCommand
  20. class Rebase(Command):
  21. common = True
  22. helpSummary = "Rebase local branches on upstream branch"
  23. helpUsage = """
  24. %prog {[<project>...] | -i <project>...}
  25. """
  26. helpDescription = """
  27. '%prog' uses git rebase to move local changes in the current topic branch to
  28. the HEAD of the upstream history, useful when you have made commits in a topic
  29. branch but need to incorporate new upstream changes "underneath" them.
  30. """
  31. def _Options(self, p):
  32. p.add_option('-i', '--interactive',
  33. dest="interactive", action="store_true",
  34. help="interactive rebase (single project only)")
  35. p.add_option('--fail-fast',
  36. dest='fail_fast', action='store_true',
  37. help='Stop rebasing after first error is hit')
  38. p.add_option('-f', '--force-rebase',
  39. dest='force_rebase', action='store_true',
  40. help='Pass --force-rebase to git rebase')
  41. p.add_option('--no-ff',
  42. dest='no_ff', action='store_true',
  43. help='Pass --no-ff to git rebase')
  44. p.add_option('-q', '--quiet',
  45. dest='quiet', action='store_true',
  46. help='Pass --quiet to git rebase')
  47. p.add_option('--autosquash',
  48. dest='autosquash', action='store_true',
  49. help='Pass --autosquash to git rebase')
  50. p.add_option('--whitespace',
  51. dest='whitespace', action='store', metavar='WS',
  52. help='Pass --whitespace to git rebase')
  53. p.add_option('--auto-stash',
  54. dest='auto_stash', action='store_true',
  55. help='Stash local modifications before starting')
  56. p.add_option('-m', '--onto-manifest',
  57. dest='onto_manifest', action='store_true',
  58. help='Rebase onto the manifest version instead of upstream '
  59. 'HEAD. This helps to make sure the local tree stays '
  60. 'consistent if you previously synced to a manifest.')
  61. def Execute(self, opt, args):
  62. all_projects = self.GetProjects(args)
  63. one_project = len(all_projects) == 1
  64. if opt.interactive and not one_project:
  65. print('error: interactive rebase not supported with multiple projects',
  66. file=sys.stderr)
  67. if len(args) == 1:
  68. print('note: project %s is mapped to more than one path' % (args[0],),
  69. file=sys.stderr)
  70. return 1
  71. # Setup the common git rebase args that we use for all projects.
  72. common_args = ['rebase']
  73. if opt.whitespace:
  74. common_args.append('--whitespace=%s' % opt.whitespace)
  75. if opt.quiet:
  76. common_args.append('--quiet')
  77. if opt.force_rebase:
  78. common_args.append('--force-rebase')
  79. if opt.no_ff:
  80. common_args.append('--no-ff')
  81. if opt.autosquash:
  82. common_args.append('--autosquash')
  83. if opt.interactive:
  84. common_args.append('-i')
  85. ret = 0
  86. for project in all_projects:
  87. if ret and opt.fail_fast:
  88. break
  89. cb = project.CurrentBranch
  90. if not cb:
  91. if one_project:
  92. print("error: project %s has a detached HEAD" % project.relpath,
  93. file=sys.stderr)
  94. return 1
  95. # ignore branches with detatched HEADs
  96. continue
  97. upbranch = project.GetBranch(cb)
  98. if not upbranch.LocalMerge:
  99. if one_project:
  100. print("error: project %s does not track any remote branches"
  101. % project.relpath, file=sys.stderr)
  102. return 1
  103. # ignore branches without remotes
  104. continue
  105. args = common_args[:]
  106. if opt.onto_manifest:
  107. args.append('--onto')
  108. args.append(project.revisionExpr)
  109. args.append(upbranch.LocalMerge)
  110. print('# %s: rebasing %s -> %s'
  111. % (project.relpath, cb, upbranch.LocalMerge), file=sys.stderr)
  112. needs_stash = False
  113. if opt.auto_stash:
  114. stash_args = ["update-index", "--refresh", "-q"]
  115. if GitCommand(project, stash_args).Wait() != 0:
  116. needs_stash = True
  117. # Dirty index, requires stash...
  118. stash_args = ["stash"]
  119. if GitCommand(project, stash_args).Wait() != 0:
  120. ret += 1
  121. continue
  122. if GitCommand(project, args).Wait() != 0:
  123. ret += 1
  124. continue
  125. if needs_stash:
  126. stash_args.append('pop')
  127. stash_args.append('--quiet')
  128. if GitCommand(project, stash_args).Wait() != 0:
  129. ret += 1
  130. if ret:
  131. print('error: %i projects had errors' % (ret,), file=sys.stderr)
  132. return ret