download.py 3.2 KB

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