rebase.py 4.1 KB

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