download.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #
  2. # Copyright (C) 2008 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. from __future__ import print_function
  16. import re
  17. import sys
  18. from command import Command
  19. from error import GitError
  20. CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
  21. class Download(Command):
  22. common = True
  23. helpSummary = "Download and checkout a change"
  24. helpUsage = """
  25. %prog {[project] change[/patchset]}...
  26. """
  27. helpDescription = """
  28. The '%prog' command downloads a change from the review system and
  29. makes it available in your project's local working directory.
  30. If no project is specified try to use current directory as a project.
  31. """
  32. def _Options(self, p):
  33. p.add_option('-c', '--cherry-pick',
  34. dest='cherrypick', action='store_true',
  35. help="cherry-pick instead of checkout")
  36. p.add_option('-r', '--revert',
  37. dest='revert', action='store_true',
  38. help="revert instead of checkout")
  39. p.add_option('-f', '--ff-only',
  40. dest='ffonly', action='store_true',
  41. help="force fast-forward merge")
  42. def _ParseChangeIds(self, args):
  43. if not args:
  44. self.Usage()
  45. to_get = []
  46. project = None
  47. for a in args:
  48. m = CHANGE_RE.match(a)
  49. if m:
  50. if not project:
  51. project = self.GetProjects(".")[0]
  52. chg_id = int(m.group(1))
  53. if m.group(2):
  54. ps_id = int(m.group(2))
  55. else:
  56. ps_id = 1
  57. regex = r'refs/changes/%2.2d/%d/(\d+)' % (chg_id % 100, chg_id)
  58. output = project._LsRemote()
  59. if output:
  60. rcomp = re.compile(regex, re.I)
  61. for line in output.splitlines():
  62. match = rcomp.search(line)
  63. if match:
  64. ps_id = max(int(match.group(1)), ps_id)
  65. to_get.append((project, chg_id, ps_id))
  66. else:
  67. project = self.GetProjects([a])[0]
  68. return to_get
  69. def Execute(self, opt, args):
  70. for project, change_id, ps_id in self._ParseChangeIds(args):
  71. dl = project.DownloadPatchSet(change_id, ps_id)
  72. if not dl:
  73. print('[%s] change %d/%d not found'
  74. % (project.name, change_id, ps_id),
  75. file=sys.stderr)
  76. sys.exit(1)
  77. if not opt.revert and not dl.commits:
  78. print('[%s] change %d/%d has already been merged'
  79. % (project.name, change_id, ps_id),
  80. file=sys.stderr)
  81. continue
  82. if len(dl.commits) > 1:
  83. print('[%s] %d/%d depends on %d unmerged changes:' \
  84. % (project.name, change_id, ps_id, len(dl.commits)),
  85. file=sys.stderr)
  86. for c in dl.commits:
  87. print(' %s' % (c), file=sys.stderr)
  88. if opt.cherrypick:
  89. try:
  90. project._CherryPick(dl.commit)
  91. except GitError:
  92. print('[%s] Could not complete the cherry-pick of %s' \
  93. % (project.name, dl.commit), file=sys.stderr)
  94. sys.exit(1)
  95. elif opt.revert:
  96. project._Revert(dl.commit)
  97. elif opt.ffonly:
  98. project._FastForward(dl.commit, ffonly=True)
  99. else:
  100. project._Checkout(dl.commit)