download.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import os
  16. import re
  17. import sys
  18. from command import Command
  19. CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
  20. class Download(Command):
  21. common = True
  22. helpSummary = "Download and checkout a change"
  23. helpUsage = """
  24. %prog {project change[/patchset]}...
  25. """
  26. helpDescription = """
  27. The '%prog' command downloads a change from the review system and
  28. makes it available in your project's local working directory.
  29. """
  30. def _Options(self, p):
  31. p.add_option('-c','--cherry-pick',
  32. dest='cherrypick', action='store_true',
  33. help="cherry-pick instead of checkout")
  34. p.add_option('-r','--revert',
  35. dest='revert', action='store_true',
  36. help="revert instead of checkout")
  37. def _ParseChangeIds(self, args):
  38. if not args:
  39. self.Usage()
  40. to_get = []
  41. project = None
  42. for a in args:
  43. m = CHANGE_RE.match(a)
  44. if m:
  45. if not project:
  46. self.Usage()
  47. chg_id = int(m.group(1))
  48. if m.group(2):
  49. ps_id = int(m.group(2))
  50. else:
  51. ps_id = 1
  52. to_get.append((project, chg_id, ps_id))
  53. else:
  54. project = self.GetProjects([a])[0]
  55. return to_get
  56. def Execute(self, opt, args):
  57. for project, change_id, ps_id in self._ParseChangeIds(args):
  58. dl = project.DownloadPatchSet(change_id, ps_id)
  59. if not dl:
  60. print >>sys.stderr, \
  61. '[%s] change %d/%d not found' \
  62. % (project.name, change_id, ps_id)
  63. sys.exit(1)
  64. if not opt.revert and not dl.commits:
  65. print >>sys.stderr, \
  66. '[%s] change %d/%d has already been merged' \
  67. % (project.name, change_id, ps_id)
  68. continue
  69. if len(dl.commits) > 1:
  70. print >>sys.stderr, \
  71. '[%s] %d/%d depends on %d unmerged changes:' \
  72. % (project.name, change_id, ps_id, len(dl.commits))
  73. for c in dl.commits:
  74. print >>sys.stderr, ' %s' % (c)
  75. if opt.cherrypick:
  76. project._CherryPick(dl.commit)
  77. elif opt.revert:
  78. project._Revert(dl.commit)
  79. else:
  80. project._Checkout(dl.commit)