download.py 3.6 KB

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