rebase.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. def Execute(self, opt, args):
  53. all_projects = self.GetProjects(args)
  54. one_project = len(all_projects) == 1
  55. if opt.interactive and not one_project:
  56. print('error: interactive rebase not supported with multiple projects',
  57. file=sys.stderr)
  58. if len(args) == 1:
  59. print('note: project %s is mapped to more than one path' % (args[0],),
  60. file=sys.stderr)
  61. return -1
  62. for project in all_projects:
  63. cb = project.CurrentBranch
  64. if not cb:
  65. if one_project:
  66. print("error: project %s has a detached HEAD" % project.relpath,
  67. file=sys.stderr)
  68. return -1
  69. # ignore branches with detatched HEADs
  70. continue
  71. upbranch = project.GetBranch(cb)
  72. if not upbranch.LocalMerge:
  73. if one_project:
  74. print("error: project %s does not track any remote branches"
  75. % project.relpath, file=sys.stderr)
  76. return -1
  77. # ignore branches without remotes
  78. continue
  79. args = ["rebase"]
  80. if opt.whitespace:
  81. args.append('--whitespace=%s' % opt.whitespace)
  82. if opt.quiet:
  83. args.append('--quiet')
  84. if opt.force_rebase:
  85. args.append('--force-rebase')
  86. if opt.no_ff:
  87. args.append('--no-ff')
  88. if opt.autosquash:
  89. args.append('--autosquash')
  90. if opt.interactive:
  91. args.append("-i")
  92. args.append(upbranch.LocalMerge)
  93. print('# %s: rebasing %s -> %s'
  94. % (project.relpath, cb, upbranch.LocalMerge), file=sys.stderr)
  95. needs_stash = False
  96. if opt.auto_stash:
  97. stash_args = ["update-index", "--refresh", "-q"]
  98. if GitCommand(project, stash_args).Wait() != 0:
  99. needs_stash = True
  100. # Dirty index, requires stash...
  101. stash_args = ["stash"]
  102. if GitCommand(project, stash_args).Wait() != 0:
  103. return -1
  104. if GitCommand(project, args).Wait() != 0:
  105. return -1
  106. if needs_stash:
  107. stash_args.append('pop')
  108. stash_args.append('--quiet')
  109. if GitCommand(project, stash_args).Wait() != 0:
  110. return -1