upload.py 8.1 KB

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