upload.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. If the --replace option is passed the user can designate which
  52. existing change(s) in Gerrit match up to the commits in the branch
  53. being uploaded. For each matched pair of change,commit the commit
  54. will be added as a new patch set, completely replacing the set of
  55. files and description associated with the change in Gerrit.
  56. Configuration
  57. -------------
  58. review.URL.autoupload:
  59. To disable the "Upload ... (y/n)?" prompt, you can set a per-project
  60. or global Git configuration option. If review.URL.autoupload is set
  61. to "true" then repo will assume you always answer "y" at the prompt,
  62. and will not prompt you further. If it is set to "false" then repo
  63. will assume you always answer "n", and will abort.
  64. The URL must match the review URL listed in the manifest XML file,
  65. or in the .git/config within the project. For example:
  66. [remote "origin"]
  67. url = git://git.example.com/project.git
  68. review = http://review.example.com/
  69. [review "http://review.example.com/"]
  70. autoupload = true
  71. """
  72. def _Options(self, p):
  73. p.add_option('--replace',
  74. dest='replace', action='store_true',
  75. help='Upload replacement patchesets from this branch')
  76. p.add_option('--re', '--reviewers',
  77. type='string', action='append', dest='reviewers',
  78. help='Request reviews from these people.')
  79. p.add_option('--cc',
  80. type='string', action='append', dest='cc',
  81. help='Also send email to these email addresses.')
  82. def _SingleBranch(self, branch, people):
  83. project = branch.project
  84. name = branch.name
  85. remote = project.GetBranch(name).remote
  86. key = 'review.%s.autoupload' % remote.review
  87. answer = project.config.GetBoolean(key)
  88. if answer is False:
  89. _die("upload blocked by %s = false" % key)
  90. if answer is None:
  91. date = branch.date
  92. list = branch.commits
  93. print 'Upload project %s/:' % project.relpath
  94. print ' branch %s (%2d commit%s, %s):' % (
  95. name,
  96. len(list),
  97. len(list) != 1 and 's' or '',
  98. date)
  99. for commit in list:
  100. print ' %s' % commit
  101. sys.stdout.write('(y/n)? ')
  102. answer = sys.stdin.readline().strip()
  103. answer = answer in ('y', 'Y', 'yes', '1', 'true', 't')
  104. if answer:
  105. self._UploadAndReport([branch], people)
  106. else:
  107. _die("upload aborted by user")
  108. def _MultipleBranches(self, pending, people):
  109. projects = {}
  110. branches = {}
  111. script = []
  112. script.append('# Uncomment the branches to upload:')
  113. for project, avail in pending:
  114. script.append('#')
  115. script.append('# project %s/:' % project.relpath)
  116. b = {}
  117. for branch in avail:
  118. name = branch.name
  119. date = branch.date
  120. list = branch.commits
  121. if b:
  122. script.append('#')
  123. script.append('# branch %s (%2d commit%s, %s):' % (
  124. name,
  125. len(list),
  126. len(list) != 1 and 's' or '',
  127. date))
  128. for commit in list:
  129. script.append('# %s' % commit)
  130. b[name] = branch
  131. projects[project.relpath] = project
  132. branches[project.name] = b
  133. script.append('')
  134. script = Editor.EditString("\n".join(script)).split("\n")
  135. project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
  136. branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*')
  137. project = None
  138. todo = []
  139. for line in script:
  140. m = project_re.match(line)
  141. if m:
  142. name = m.group(1)
  143. project = projects.get(name)
  144. if not project:
  145. _die('project %s not available for upload', name)
  146. continue
  147. m = branch_re.match(line)
  148. if m:
  149. name = m.group(1)
  150. if not project:
  151. _die('project for branch %s not in script', name)
  152. branch = branches[project.name].get(name)
  153. if not branch:
  154. _die('branch %s not in %s', name, project.relpath)
  155. todo.append(branch)
  156. if not todo:
  157. _die("nothing uncommented for upload")
  158. self._UploadAndReport(todo, people)
  159. def _ReplaceBranch(self, project, people):
  160. branch = project.CurrentBranch
  161. if not branch:
  162. print >>sys.stdout, "no branches ready for upload"
  163. return
  164. branch = project.GetUploadableBranch(branch)
  165. if not branch:
  166. print >>sys.stdout, "no branches ready for upload"
  167. return
  168. script = []
  169. script.append('# Replacing from branch %s' % branch.name)
  170. for commit in branch.commits:
  171. script.append('[ ] %s' % commit)
  172. script.append('')
  173. script.append('# Insert change numbers in the brackets to add a new patch set.')
  174. script.append('# To create a new change record, leave the brackets empty.')
  175. script = Editor.EditString("\n".join(script)).split("\n")
  176. change_re = re.compile(r'^\[\s*(\d{1,})\s*\]\s*([0-9a-f]{1,}) .*$')
  177. to_replace = dict()
  178. full_hashes = branch.unabbrev_commits
  179. for line in script:
  180. m = change_re.match(line)
  181. if m:
  182. c = m.group(1)
  183. f = m.group(2)
  184. try:
  185. f = full_hashes[f]
  186. except KeyError:
  187. print 'fh = %s' % full_hashes
  188. print >>sys.stderr, "error: commit %s not found" % f
  189. sys.exit(1)
  190. if c in to_replace:
  191. print >>sys.stderr,\
  192. "error: change %s cannot accept multiple commits" % c
  193. sys.exit(1)
  194. to_replace[c] = f
  195. if not to_replace:
  196. print >>sys.stderr, "error: no replacements specified"
  197. print >>sys.stderr, " use 'repo upload' without --replace"
  198. sys.exit(1)
  199. branch.replace_changes = to_replace
  200. self._UploadAndReport([branch], people)
  201. def _UploadAndReport(self, todo, people):
  202. have_errors = False
  203. for branch in todo:
  204. try:
  205. branch.UploadForReview(people)
  206. branch.uploaded = True
  207. except UploadError, e:
  208. branch.error = e
  209. branch.uploaded = False
  210. have_errors = True
  211. print >>sys.stderr, ''
  212. print >>sys.stderr, '--------------------------------------------'
  213. if have_errors:
  214. for branch in todo:
  215. if not branch.uploaded:
  216. print >>sys.stderr, '[FAILED] %-15s %-15s (%s)' % (
  217. branch.project.relpath + '/', \
  218. branch.name, \
  219. branch.error)
  220. print >>sys.stderr, ''
  221. for branch in todo:
  222. if branch.uploaded:
  223. print >>sys.stderr, '[OK ] %-15s %s' % (
  224. branch.project.relpath + '/',
  225. branch.name)
  226. if have_errors:
  227. sys.exit(1)
  228. def Execute(self, opt, args):
  229. project_list = self.GetProjects(args)
  230. pending = []
  231. reviewers = []
  232. cc = []
  233. if opt.reviewers:
  234. reviewers = _SplitEmails(opt.reviewers)
  235. if opt.cc:
  236. cc = _SplitEmails(opt.cc)
  237. people = (reviewers,cc)
  238. if opt.replace:
  239. if len(project_list) != 1:
  240. print >>sys.stderr, \
  241. 'error: --replace requires exactly one project'
  242. sys.exit(1)
  243. self._ReplaceBranch(project_list[0], people)
  244. return
  245. for project in project_list:
  246. avail = project.GetUploadableBranches()
  247. if avail:
  248. pending.append((project, avail))
  249. if not pending:
  250. print >>sys.stdout, "no branches ready for upload"
  251. elif len(pending) == 1 and len(pending[0][1]) == 1:
  252. self._SingleBranch(pending[0][1][0], people)
  253. else:
  254. self._MultipleBranches(pending, people)