rebase.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 color import Coloring
  19. from command import Command
  20. from git_command import GitCommand
  21. class RebaseColoring(Coloring):
  22. def __init__(self, config):
  23. Coloring.__init__(self, config, 'rebase')
  24. self.project = self.printer('project', attr='bold')
  25. self.fail = self.printer('fail', fg='red')
  26. class Rebase(Command):
  27. common = True
  28. helpSummary = "Rebase local branches on upstream branch"
  29. helpUsage = """
  30. %prog {[<project>...] | -i <project>...}
  31. """
  32. helpDescription = """
  33. '%prog' uses git rebase to move local changes in the current topic branch to
  34. the HEAD of the upstream history, useful when you have made commits in a topic
  35. branch but need to incorporate new upstream changes "underneath" them.
  36. """
  37. def _Options(self, p):
  38. p.add_option('-i', '--interactive',
  39. dest="interactive", action="store_true",
  40. help="interactive rebase (single project only)")
  41. p.add_option('--fail-fast',
  42. dest='fail_fast', action='store_true',
  43. help='Stop rebasing after first error is hit')
  44. p.add_option('-f', '--force-rebase',
  45. dest='force_rebase', action='store_true',
  46. help='Pass --force-rebase to git rebase')
  47. p.add_option('--no-ff',
  48. dest='ff', default=True, action='store_false',
  49. help='Pass --no-ff to git rebase')
  50. p.add_option('-q', '--quiet',
  51. dest='quiet', action='store_true',
  52. help='Pass --quiet to git rebase')
  53. p.add_option('--autosquash',
  54. dest='autosquash', action='store_true',
  55. help='Pass --autosquash to git rebase')
  56. p.add_option('--whitespace',
  57. dest='whitespace', action='store', metavar='WS',
  58. help='Pass --whitespace to git rebase')
  59. p.add_option('--auto-stash',
  60. dest='auto_stash', action='store_true',
  61. help='Stash local modifications before starting')
  62. p.add_option('-m', '--onto-manifest',
  63. dest='onto_manifest', action='store_true',
  64. help='Rebase onto the manifest version instead of upstream '
  65. 'HEAD. This helps to make sure the local tree stays '
  66. 'consistent if you previously synced to a manifest.')
  67. def Execute(self, opt, args):
  68. all_projects = self.GetProjects(args)
  69. one_project = len(all_projects) == 1
  70. if opt.interactive and not one_project:
  71. print('error: interactive rebase not supported with multiple projects',
  72. file=sys.stderr)
  73. if len(args) == 1:
  74. print('note: project %s is mapped to more than one path' % (args[0],),
  75. file=sys.stderr)
  76. return 1
  77. # Setup the common git rebase args that we use for all projects.
  78. common_args = ['rebase']
  79. if opt.whitespace:
  80. common_args.append('--whitespace=%s' % opt.whitespace)
  81. if opt.quiet:
  82. common_args.append('--quiet')
  83. if opt.force_rebase:
  84. common_args.append('--force-rebase')
  85. if not opt.ff:
  86. common_args.append('--no-ff')
  87. if opt.autosquash:
  88. common_args.append('--autosquash')
  89. if opt.interactive:
  90. common_args.append('-i')
  91. config = self.manifest.manifestProject.config
  92. out = RebaseColoring(config)
  93. out.redirect(sys.stdout)
  94. ret = 0
  95. for project in all_projects:
  96. if ret and opt.fail_fast:
  97. break
  98. cb = project.CurrentBranch
  99. if not cb:
  100. if one_project:
  101. print("error: project %s has a detached HEAD" % project.relpath,
  102. file=sys.stderr)
  103. return 1
  104. # ignore branches with detatched HEADs
  105. continue
  106. upbranch = project.GetBranch(cb)
  107. if not upbranch.LocalMerge:
  108. if one_project:
  109. print("error: project %s does not track any remote branches"
  110. % project.relpath, file=sys.stderr)
  111. return 1
  112. # ignore branches without remotes
  113. continue
  114. args = common_args[:]
  115. if opt.onto_manifest:
  116. args.append('--onto')
  117. args.append(project.revisionExpr)
  118. args.append(upbranch.LocalMerge)
  119. out.project('project %s: rebasing %s -> %s',
  120. project.relpath, cb, upbranch.LocalMerge)
  121. out.nl()
  122. out.flush()
  123. needs_stash = False
  124. if opt.auto_stash:
  125. stash_args = ["update-index", "--refresh", "-q"]
  126. if GitCommand(project, stash_args).Wait() != 0:
  127. needs_stash = True
  128. # Dirty index, requires stash...
  129. stash_args = ["stash"]
  130. if GitCommand(project, stash_args).Wait() != 0:
  131. ret += 1
  132. continue
  133. if GitCommand(project, args).Wait() != 0:
  134. ret += 1
  135. continue
  136. if needs_stash:
  137. stash_args.append('pop')
  138. stash_args.append('--quiet')
  139. if GitCommand(project, stash_args).Wait() != 0:
  140. ret += 1
  141. if ret:
  142. out.fail('%i projects had errors', ret)
  143. out.nl()
  144. return ret