download.py 3.2 KB

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