rebase.py 4.7 KB

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