download.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. def _ParseChangeIds(self, args):
  35. if not args:
  36. self.Usage()
  37. to_get = []
  38. project = None
  39. for a in args:
  40. m = CHANGE_RE.match(a)
  41. if m:
  42. if not project:
  43. self.Usage()
  44. chg_id = int(m.group(1))
  45. if m.group(2):
  46. ps_id = int(m.group(2))
  47. else:
  48. ps_id = 1
  49. to_get.append((project, chg_id, ps_id))
  50. else:
  51. project = self.GetProjects([a])[0]
  52. return to_get
  53. def Execute(self, opt, args):
  54. for project, change_id, ps_id in self._ParseChangeIds(args):
  55. dl = project.DownloadPatchSet(change_id, ps_id)
  56. if not dl:
  57. print >>sys.stderr, \
  58. '[%s] change %d/%d not found' \
  59. % (project.name, change_id, ps_id)
  60. sys.exit(1)
  61. if not dl.commits:
  62. print >>sys.stderr, \
  63. '[%s] change %d/%d has already been merged' \
  64. % (project.name, change_id, ps_id)
  65. continue
  66. if len(dl.commits) > 1:
  67. print >>sys.stderr, \
  68. '[%s] %d/%d depends on %d unmerged changes:' \
  69. % (project.name, change_id, ps_id, len(dl.commits))
  70. for c in dl.commits:
  71. print >>sys.stderr, ' %s' % (c)
  72. if opt.cherrypick:
  73. project._CherryPick(dl.commit)
  74. else:
  75. project._Checkout(dl.commit)