rebase.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. def Execute(self, opt, args):
  51. all = self.GetProjects(args)
  52. one_project = len(all) == 1
  53. if opt.interactive and not one_project:
  54. print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
  55. return -1
  56. for project in all:
  57. cb = project.CurrentBranch
  58. if not cb:
  59. if one_project:
  60. print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath
  61. return -1
  62. # ignore branches with detatched HEADs
  63. continue
  64. upbranch = project.GetBranch(cb)
  65. if not upbranch.LocalMerge:
  66. if one_project:
  67. print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath
  68. return -1
  69. # ignore branches without remotes
  70. continue
  71. args = ["rebase"]
  72. if opt.whitespace:
  73. args.append('--whitespace=%s' % opt.whitespace)
  74. if opt.quiet:
  75. args.append('--quiet')
  76. if opt.force_rebase:
  77. args.append('--force-rebase')
  78. if opt.no_ff:
  79. args.append('--no-ff')
  80. if opt.autosquash:
  81. args.append('--autosquash')
  82. if opt.interactive:
  83. args.append("-i")
  84. args.append(upbranch.LocalMerge)
  85. print >>sys.stderr, '# %s: rebasing %s -> %s' % \
  86. (project.relpath, cb, upbranch.LocalMerge)
  87. if GitCommand(project, args).Wait() != 0:
  88. return -1