rebase.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. import sys
  16. from command import Command
  17. from git_command import GitCommand
  18. from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
  19. from error import GitError
  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. def Execute(self, opt, args):
  54. all = self.GetProjects(args)
  55. one_project = len(all) == 1
  56. if opt.interactive and not one_project:
  57. print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
  58. return -1
  59. for project in all:
  60. cb = project.CurrentBranch
  61. if not cb:
  62. if one_project:
  63. print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath
  64. return -1
  65. # ignore branches with detatched HEADs
  66. continue
  67. upbranch = project.GetBranch(cb)
  68. if not upbranch.LocalMerge:
  69. if one_project:
  70. print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath
  71. return -1
  72. # ignore branches without remotes
  73. continue
  74. args = ["rebase"]
  75. if opt.whitespace:
  76. args.append('--whitespace=%s' % opt.whitespace)
  77. if opt.quiet:
  78. args.append('--quiet')
  79. if opt.force_rebase:
  80. args.append('--force-rebase')
  81. if opt.no_ff:
  82. args.append('--no-ff')
  83. if opt.autosquash:
  84. args.append('--autosquash')
  85. if opt.interactive:
  86. args.append("-i")
  87. args.append(upbranch.LocalMerge)
  88. print >>sys.stderr, '# %s: rebasing %s -> %s' % \
  89. (project.relpath, cb, upbranch.LocalMerge)
  90. needs_stash = False
  91. if opt.auto_stash:
  92. stash_args = ["update-index", "--refresh", "-q"]
  93. if GitCommand(project, stash_args).Wait() != 0:
  94. needs_stash = True
  95. # Dirty index, requires stash...
  96. stash_args = ["stash"]
  97. if GitCommand(project, stash_args).Wait() != 0:
  98. return -1
  99. if GitCommand(project, args).Wait() != 0:
  100. return -1
  101. if needs_stash:
  102. stash_args.append('pop')
  103. stash_args.append('--quiet')
  104. if GitCommand(project, stash_args).Wait() != 0:
  105. return -1