download.py 2.9 KB

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