download.py 4.7 KB

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