download.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (C) 2008 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. import sys
  16. from command import Command
  17. from error import GitError
  18. CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
  19. class Download(Command):
  20. common = True
  21. helpSummary = "Download and checkout a change"
  22. helpUsage = """
  23. %prog {[project] change[/patchset]}...
  24. """
  25. helpDescription = """
  26. The '%prog' command downloads a change from the review system and
  27. makes it available in your project's local working directory.
  28. If no project is specified try to use current directory as a project.
  29. """
  30. def _Options(self, p):
  31. p.add_option('-b', '--branch',
  32. help='create a new branch first')
  33. p.add_option('-c', '--cherry-pick',
  34. dest='cherrypick', action='store_true',
  35. help="cherry-pick instead of checkout")
  36. p.add_option('-x', '--record-origin', action='store_true',
  37. help='pass -x when cherry-picking')
  38. p.add_option('-r', '--revert',
  39. dest='revert', action='store_true',
  40. help="revert instead of checkout")
  41. p.add_option('-f', '--ff-only',
  42. dest='ffonly', action='store_true',
  43. help="force fast-forward merge")
  44. def _ParseChangeIds(self, args):
  45. if not args:
  46. self.Usage()
  47. to_get = []
  48. project = None
  49. for a in args:
  50. m = CHANGE_RE.match(a)
  51. if m:
  52. if not project:
  53. project = self.GetProjects(".")[0]
  54. chg_id = int(m.group(1))
  55. if m.group(2):
  56. ps_id = int(m.group(2))
  57. else:
  58. ps_id = 1
  59. refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id)
  60. output = project._LsRemote(refs + '*')
  61. if output:
  62. regex = refs + r'(\d+)'
  63. rcomp = re.compile(regex, re.I)
  64. for line in output.splitlines():
  65. match = rcomp.search(line)
  66. if match:
  67. ps_id = max(int(match.group(1)), ps_id)
  68. to_get.append((project, chg_id, ps_id))
  69. else:
  70. project = self.GetProjects([a])[0]
  71. return to_get
  72. def ValidateOptions(self, opt, args):
  73. if opt.record_origin:
  74. if not opt.cherrypick:
  75. self.OptionParser.error('-x only makes sense with --cherry-pick')
  76. if opt.ffonly:
  77. self.OptionParser.error('-x and --ff are mutually exclusive options')
  78. def Execute(self, opt, args):
  79. for project, change_id, ps_id in self._ParseChangeIds(args):
  80. dl = project.DownloadPatchSet(change_id, ps_id)
  81. if not dl:
  82. print('[%s] change %d/%d not found'
  83. % (project.name, change_id, ps_id),
  84. file=sys.stderr)
  85. sys.exit(1)
  86. if not opt.revert and not dl.commits:
  87. print('[%s] change %d/%d has already been merged'
  88. % (project.name, change_id, ps_id),
  89. file=sys.stderr)
  90. continue
  91. if len(dl.commits) > 1:
  92. print('[%s] %d/%d depends on %d unmerged changes:'
  93. % (project.name, change_id, ps_id, len(dl.commits)),
  94. file=sys.stderr)
  95. for c in dl.commits:
  96. print(' %s' % (c), file=sys.stderr)
  97. if opt.cherrypick:
  98. mode = 'cherry-pick'
  99. elif opt.revert:
  100. mode = 'revert'
  101. elif opt.ffonly:
  102. mode = 'fast-forward merge'
  103. else:
  104. mode = 'checkout'
  105. # We'll combine the branch+checkout operation, but all the rest need a
  106. # dedicated branch start.
  107. if opt.branch and mode != 'checkout':
  108. project.StartBranch(opt.branch)
  109. try:
  110. if opt.cherrypick:
  111. project._CherryPick(dl.commit, ffonly=opt.ffonly,
  112. record_origin=opt.record_origin)
  113. elif opt.revert:
  114. project._Revert(dl.commit)
  115. elif opt.ffonly:
  116. project._FastForward(dl.commit, ffonly=True)
  117. else:
  118. if opt.branch:
  119. project.StartBranch(opt.branch, revision=dl.commit)
  120. else:
  121. project._Checkout(dl.commit)
  122. except GitError:
  123. print('[%s] Could not complete the %s of %s'
  124. % (project.name, mode, dl.commit), file=sys.stderr)
  125. sys.exit(1)