download.py 2.9 KB

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