download.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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('-c', '--cherry-pick',
  35. dest='cherrypick', action='store_true',
  36. help="cherry-pick instead of checkout")
  37. p.add_option('-x', '--record-origin', action='store_true',
  38. help='pass -x when cherry-picking')
  39. p.add_option('-r', '--revert',
  40. dest='revert', action='store_true',
  41. help="revert instead of checkout")
  42. p.add_option('-f', '--ff-only',
  43. dest='ffonly', action='store_true',
  44. help="force fast-forward merge")
  45. def _ParseChangeIds(self, args):
  46. if not args:
  47. self.Usage()
  48. to_get = []
  49. project = None
  50. for a in args:
  51. m = CHANGE_RE.match(a)
  52. if m:
  53. if not project:
  54. project = self.GetProjects(".")[0]
  55. chg_id = int(m.group(1))
  56. if m.group(2):
  57. ps_id = int(m.group(2))
  58. else:
  59. ps_id = 1
  60. refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id)
  61. output = project._LsRemote(refs + '*')
  62. if output:
  63. regex = refs + r'(\d+)'
  64. rcomp = re.compile(regex, re.I)
  65. for line in output.splitlines():
  66. match = rcomp.search(line)
  67. if match:
  68. ps_id = max(int(match.group(1)), ps_id)
  69. to_get.append((project, chg_id, ps_id))
  70. else:
  71. project = self.GetProjects([a])[0]
  72. return to_get
  73. def ValidateOptions(self, opt, args):
  74. if opt.record_origin:
  75. if not opt.cherrypick:
  76. self.OptionParser.error('-x only makes sense with --cherry-pick')
  77. if opt.ffonly:
  78. self.OptionParser.error('-x and --ff are mutually exclusive options')
  79. def Execute(self, opt, args):
  80. for project, change_id, ps_id in self._ParseChangeIds(args):
  81. dl = project.DownloadPatchSet(change_id, ps_id)
  82. if not dl:
  83. print('[%s] change %d/%d not found'
  84. % (project.name, change_id, ps_id),
  85. file=sys.stderr)
  86. sys.exit(1)
  87. if not opt.revert and not dl.commits:
  88. print('[%s] change %d/%d has already been merged'
  89. % (project.name, change_id, ps_id),
  90. file=sys.stderr)
  91. continue
  92. if len(dl.commits) > 1:
  93. print('[%s] %d/%d depends on %d unmerged changes:'
  94. % (project.name, change_id, ps_id, len(dl.commits)),
  95. file=sys.stderr)
  96. for c in dl.commits:
  97. print(' %s' % (c), file=sys.stderr)
  98. if opt.cherrypick:
  99. mode = 'cherry-pick'
  100. elif opt.revert:
  101. mode = 'revert'
  102. elif opt.ffonly:
  103. mode = 'fast-forward merge'
  104. else:
  105. mode = 'checkout'
  106. try:
  107. if opt.cherrypick:
  108. project._CherryPick(dl.commit, ffonly=opt.ffonly,
  109. record_origin=opt.record_origin)
  110. elif opt.revert:
  111. project._Revert(dl.commit)
  112. elif opt.ffonly:
  113. project._FastForward(dl.commit, ffonly=True)
  114. else:
  115. project._Checkout(dl.commit)
  116. except GitError:
  117. print('[%s] Could not complete the %s of %s'
  118. % (project.name, mode, dl.commit), file=sys.stderr)
  119. sys.exit(1)