rebase.py 5.5 KB

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