rebase.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. def Execute(self, opt, args):
  36. all = self.GetProjects(args)
  37. one_project = len(all) == 1
  38. if opt.interactive and not one_project:
  39. print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
  40. return -1
  41. for project in all:
  42. cb = project.CurrentBranch
  43. if not cb:
  44. if one_project:
  45. print >>sys.stderr, "error: project %s has a detatched HEAD" % project.name
  46. return -1
  47. # ignore branches with detatched HEADs
  48. continue
  49. upbranch = project.GetBranch(cb)
  50. if not upbranch.LocalMerge:
  51. if one_project:
  52. print >>sys.stderr, "error: project %s does not track any remote branches" % project.name
  53. return -1
  54. # ignore branches without remotes
  55. continue
  56. upstream = project.GetRevisionId()
  57. args = ["rebase"]
  58. if opt.interactive:
  59. args.append("-i")
  60. args.append(upstream)
  61. print '# project %s: rebasing branch %s -> %s (%s)' % (
  62. project.relpath, cb, upbranch.LocalMerge, upstream[0:7])
  63. if GitCommand(project, args).Wait() != 0:
  64. return -1