rebase.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. class Rebase(Command):
  19. common = True
  20. helpSummary = "Rebase local branches on upstream branch"
  21. helpUsage = """
  22. %prog {[<project>...] | -i <project>...}
  23. """
  24. helpDescription = """
  25. '%prog' uses git rebase to move local changes in the current topic branch to
  26. the HEAD of the upstream history, useful when you have made commits in a topic
  27. branch but need to incorporate new upstream changes "underneath" them.
  28. """
  29. def _Options(self, p):
  30. p.add_option('-i', '--interactive',
  31. dest="interactive", action="store_true",
  32. help="interactive rebase (single project only)")
  33. p.add_option('-f', '--force-rebase',
  34. dest='force_rebase', action='store_true',
  35. help='Pass --force-rebase to git rebase')
  36. p.add_option('--no-ff',
  37. dest='no_ff', action='store_true',
  38. help='Pass --no-ff to git rebase')
  39. p.add_option('-q', '--quiet',
  40. dest='quiet', action='store_true',
  41. help='Pass --quiet to git rebase')
  42. p.add_option('--autosquash',
  43. dest='autosquash', action='store_true',
  44. help='Pass --autosquash to git rebase')
  45. p.add_option('--whitespace',
  46. dest='whitespace', action='store', metavar='WS',
  47. help='Pass --whitespace to git rebase')
  48. p.add_option('--auto-stash',
  49. dest='auto_stash', action='store_true',
  50. help='Stash local modifications before starting')
  51. def Execute(self, opt, args):
  52. all_projects = self.GetProjects(args)
  53. one_project = len(all_projects) == 1
  54. if opt.interactive and not one_project:
  55. print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
  56. return -1
  57. for project in all_projects:
  58. cb = project.CurrentBranch
  59. if not cb:
  60. if one_project:
  61. print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath
  62. return -1
  63. # ignore branches with detatched HEADs
  64. continue
  65. upbranch = project.GetBranch(cb)
  66. if not upbranch.LocalMerge:
  67. if one_project:
  68. print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath
  69. return -1
  70. # ignore branches without remotes
  71. continue
  72. args = ["rebase"]
  73. if opt.whitespace:
  74. args.append('--whitespace=%s' % opt.whitespace)
  75. if opt.quiet:
  76. args.append('--quiet')
  77. if opt.force_rebase:
  78. args.append('--force-rebase')
  79. if opt.no_ff:
  80. args.append('--no-ff')
  81. if opt.autosquash:
  82. args.append('--autosquash')
  83. if opt.interactive:
  84. args.append("-i")
  85. args.append(upbranch.LocalMerge)
  86. print >>sys.stderr, '# %s: rebasing %s -> %s' % \
  87. (project.relpath, cb, upbranch.LocalMerge)
  88. needs_stash = False
  89. if opt.auto_stash:
  90. stash_args = ["update-index", "--refresh", "-q"]
  91. if GitCommand(project, stash_args).Wait() != 0:
  92. needs_stash = True
  93. # Dirty index, requires stash...
  94. stash_args = ["stash"]
  95. if GitCommand(project, stash_args).Wait() != 0:
  96. return -1
  97. if GitCommand(project, args).Wait() != 0:
  98. return -1
  99. if needs_stash:
  100. stash_args.append('pop')
  101. stash_args.append('--quiet')
  102. if GitCommand(project, stash_args).Wait() != 0:
  103. return -1