rebase.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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('-f', '--force-rebase',
  36. dest='force_rebase', action='store_true',
  37. help='Pass --force-rebase to git rebase')
  38. p.add_option('--no-ff',
  39. dest='no_ff', action='store_true',
  40. help='Pass --no-ff to git rebase')
  41. p.add_option('-q', '--quiet',
  42. dest='quiet', action='store_true',
  43. help='Pass --quiet to git rebase')
  44. p.add_option('--autosquash',
  45. dest='autosquash', action='store_true',
  46. help='Pass --autosquash to git rebase')
  47. p.add_option('--whitespace',
  48. dest='whitespace', action='store', metavar='WS',
  49. help='Pass --whitespace to git rebase')
  50. p.add_option('--auto-stash',
  51. dest='auto_stash', action='store_true',
  52. help='Stash local modifications before starting')
  53. p.add_option('-m', '--onto-manifest',
  54. dest='onto_manifest', action='store_true',
  55. help='Rebase onto the manifest version instead of upstream '
  56. 'HEAD. This helps to make sure the local tree stays '
  57. 'consistent if you previously synced to a manifest.')
  58. def Execute(self, opt, args):
  59. all_projects = self.GetProjects(args)
  60. one_project = len(all_projects) == 1
  61. if opt.interactive and not one_project:
  62. print('error: interactive rebase not supported with multiple projects',
  63. file=sys.stderr)
  64. if len(args) == 1:
  65. print('note: project %s is mapped to more than one path' % (args[0],),
  66. file=sys.stderr)
  67. return 1
  68. # Setup the common git rebase args that we use for all projects.
  69. common_args = ['rebase']
  70. if opt.whitespace:
  71. common_args.append('--whitespace=%s' % opt.whitespace)
  72. if opt.quiet:
  73. common_args.append('--quiet')
  74. if opt.force_rebase:
  75. common_args.append('--force-rebase')
  76. if opt.no_ff:
  77. common_args.append('--no-ff')
  78. if opt.autosquash:
  79. common_args.append('--autosquash')
  80. if opt.interactive:
  81. common_args.append('-i')
  82. for project in all_projects:
  83. cb = project.CurrentBranch
  84. if not cb:
  85. if one_project:
  86. print("error: project %s has a detached HEAD" % project.relpath,
  87. file=sys.stderr)
  88. return 1
  89. # ignore branches with detatched HEADs
  90. continue
  91. upbranch = project.GetBranch(cb)
  92. if not upbranch.LocalMerge:
  93. if one_project:
  94. print("error: project %s does not track any remote branches"
  95. % project.relpath, file=sys.stderr)
  96. return 1
  97. # ignore branches without remotes
  98. continue
  99. args = common_args[:]
  100. if opt.onto_manifest:
  101. args.append('--onto')
  102. args.append(project.revisionExpr)
  103. args.append(upbranch.LocalMerge)
  104. print('# %s: rebasing %s -> %s'
  105. % (project.relpath, cb, upbranch.LocalMerge), file=sys.stderr)
  106. needs_stash = False
  107. if opt.auto_stash:
  108. stash_args = ["update-index", "--refresh", "-q"]
  109. if GitCommand(project, stash_args).Wait() != 0:
  110. needs_stash = True
  111. # Dirty index, requires stash...
  112. stash_args = ["stash"]
  113. if GitCommand(project, stash_args).Wait() != 0:
  114. return 1
  115. if GitCommand(project, args).Wait() != 0:
  116. return 1
  117. if needs_stash:
  118. stash_args.append('pop')
  119. stash_args.append('--quiet')
  120. if GitCommand(project, stash_args).Wait() != 0:
  121. return 1