upload.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 InteractiveCommand
  18. from editor import Editor
  19. from error import UploadError
  20. def _die(fmt, *args):
  21. msg = fmt % args
  22. print >>sys.stderr, 'error: %s' % msg
  23. sys.exit(1)
  24. class Upload(InteractiveCommand):
  25. common = True
  26. helpSummary = "Upload changes for code review"
  27. helpUsage="""
  28. %prog {[<project>]... | --replace <project>}
  29. """
  30. helpDescription = """
  31. The '%prog' command is used to send changes to the Gerrit code
  32. review system. It searches for changes in local projects that do
  33. not yet exist in the corresponding remote repository. If multiple
  34. changes are found, '%prog' opens an editor to allow the
  35. user to choose which change to upload. After a successful upload,
  36. repo prints the URL for the change in the Gerrit code review system.
  37. '%prog' searches for uploadable changes in all projects listed
  38. at the command line. Projects can be specified either by name, or
  39. by a relative or absolute path to the project's local directory. If
  40. no projects are specified, '%prog' will search for uploadable
  41. changes in all projects listed in the manifest.
  42. """
  43. def _Options(self, p):
  44. p.add_option('--replace',
  45. dest='replace', action='store_true',
  46. help='Upload replacement patchesets from this branch')
  47. def _SingleBranch(self, branch):
  48. project = branch.project
  49. name = branch.name
  50. date = branch.date
  51. list = branch.commits
  52. print 'Upload project %s/:' % project.relpath
  53. print ' branch %s (%2d commit%s, %s):' % (
  54. name,
  55. len(list),
  56. len(list) != 1 and 's' or '',
  57. date)
  58. for commit in list:
  59. print ' %s' % commit
  60. sys.stdout.write('(y/n)? ')
  61. answer = sys.stdin.readline().strip()
  62. if answer in ('y', 'Y', 'yes', '1', 'true', 't'):
  63. self._UploadAndReport([branch])
  64. else:
  65. _die("upload aborted by user")
  66. def _MultipleBranches(self, pending):
  67. projects = {}
  68. branches = {}
  69. script = []
  70. script.append('# Uncomment the branches to upload:')
  71. for project, avail in pending:
  72. script.append('#')
  73. script.append('# project %s/:' % project.relpath)
  74. b = {}
  75. for branch in avail:
  76. name = branch.name
  77. date = branch.date
  78. list = branch.commits
  79. if b:
  80. script.append('#')
  81. script.append('# branch %s (%2d commit%s, %s):' % (
  82. name,
  83. len(list),
  84. len(list) != 1 and 's' or '',
  85. date))
  86. for commit in list:
  87. script.append('# %s' % commit)
  88. b[name] = branch
  89. projects[project.relpath] = project
  90. branches[project.name] = b
  91. script.append('')
  92. script = Editor.EditString("\n".join(script)).split("\n")
  93. project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
  94. branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*')
  95. project = None
  96. todo = []
  97. for line in script:
  98. m = project_re.match(line)
  99. if m:
  100. name = m.group(1)
  101. project = projects.get(name)
  102. if not project:
  103. _die('project %s not available for upload', name)
  104. continue
  105. m = branch_re.match(line)
  106. if m:
  107. name = m.group(1)
  108. if not project:
  109. _die('project for branch %s not in script', name)
  110. branch = branches[project.name].get(name)
  111. if not branch:
  112. _die('branch %s not in %s', name, project.relpath)
  113. todo.append(branch)
  114. if not todo:
  115. _die("nothing uncommented for upload")
  116. self._UploadAndReport(todo)
  117. def _ReplaceBranch(self, project):
  118. branch = project.CurrentBranch
  119. if not branch:
  120. print >>sys.stdout, "no branches ready for upload"
  121. return
  122. branch = project.GetUploadableBranch(branch)
  123. if not branch:
  124. print >>sys.stdout, "no branches ready for upload"
  125. return
  126. script = []
  127. script.append('# Replacing from branch %s' % branch.name)
  128. for commit in branch.commits:
  129. script.append('[ ] %s' % commit)
  130. script.append('')
  131. script.append('# Insert change numbers in the brackets to add a new patch set.')
  132. script.append('# To create a new change record, leave the brackets empty.')
  133. script = Editor.EditString("\n".join(script)).split("\n")
  134. change_re = re.compile(r'^\[\s*(\d{1,})\s*\]\s*([0-9a-f]{1,}) .*$')
  135. to_replace = dict()
  136. full_hashes = branch.unabbrev_commits
  137. for line in script:
  138. m = change_re.match(line)
  139. if m:
  140. f = m.group(2)
  141. try:
  142. f = full_hashes[f]
  143. except KeyError:
  144. print 'fh = %s' % full_hashes
  145. print >>sys.stderr, "error: commit %s not found" % f
  146. sys.exit(1)
  147. to_replace[m.group(1)] = f
  148. if not to_replace:
  149. print >>sys.stderr, "error: no replacements specified"
  150. print >>sys.stderr, " use 'repo upload' without --replace"
  151. sys.exit(1)
  152. branch.replace_changes = to_replace
  153. self._UploadAndReport([branch])
  154. def _UploadAndReport(self, todo):
  155. have_errors = False
  156. for branch in todo:
  157. try:
  158. branch.UploadForReview()
  159. branch.uploaded = True
  160. except UploadError, e:
  161. branch.error = e
  162. branch.uploaded = False
  163. have_errors = True
  164. print >>sys.stderr, ''
  165. print >>sys.stderr, '--------------------------------------------'
  166. if have_errors:
  167. for branch in todo:
  168. if not branch.uploaded:
  169. print >>sys.stderr, '[FAILED] %-15s %-15s (%s)' % (
  170. branch.project.relpath + '/', \
  171. branch.name, \
  172. branch.error)
  173. print >>sys.stderr, ''
  174. for branch in todo:
  175. if branch.uploaded:
  176. print >>sys.stderr, '[OK ] %-15s %s' % (
  177. branch.project.relpath + '/',
  178. branch.name)
  179. print >>sys.stderr, '%s' % branch.tip_url
  180. print >>sys.stderr, '(as %s)' % branch.owner_email
  181. print >>sys.stderr, ''
  182. if have_errors:
  183. sys.exit(1)
  184. def Execute(self, opt, args):
  185. project_list = self.GetProjects(args)
  186. pending = []
  187. if opt.replace:
  188. if len(project_list) != 1:
  189. print >>sys.stderr, \
  190. 'error: --replace requires exactly one project'
  191. sys.exit(1)
  192. self._ReplaceBranch(project_list[0])
  193. return
  194. for project in project_list:
  195. avail = project.GetUploadableBranches()
  196. if avail:
  197. pending.append((project, avail))
  198. if not pending:
  199. print >>sys.stdout, "no branches ready for upload"
  200. elif len(pending) == 1 and len(pending[0][1]) == 1:
  201. self._SingleBranch(pending[0][1][0])
  202. else:
  203. self._MultipleBranches(pending)