The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 16 | import re |
| 17 | import sys |
| 18 | |
| 19 | from command import InteractiveCommand |
| 20 | from editor import Editor |
| 21 | from error import UploadError |
| 22 | |
Dan Morrill | f0a9a1a | 2010-05-05 08:18:35 -0700 | [diff] [blame^] | 23 | UNUSUAL_COMMIT_THRESHOLD = 5 |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 24 | |
| 25 | def _ConfirmManyUploads(multiple_branches=False): |
| 26 | if multiple_branches: |
| 27 | print "ATTENTION: One or more branches has an unusually high number of commits." |
| 28 | else: |
| 29 | print "ATTENTION: You are uploading an unusually high number of commits." |
| 30 | print "YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across branches?)" |
| 31 | answer = raw_input("If you are sure you intend to do this, type 'yes': ").strip() |
| 32 | return answer == "yes" |
| 33 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | def _die(fmt, *args): |
| 35 | msg = fmt % args |
| 36 | print >>sys.stderr, 'error: %s' % msg |
| 37 | sys.exit(1) |
| 38 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 39 | def _SplitEmails(values): |
| 40 | result = [] |
| 41 | for str in values: |
| 42 | result.extend([s.strip() for s in str.split(',')]) |
| 43 | return result |
| 44 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | class Upload(InteractiveCommand): |
| 46 | common = True |
| 47 | helpSummary = "Upload changes for code review" |
| 48 | helpUsage=""" |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 49 | %prog [--re --cc] {[<project>]... | --replace <project>} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | """ |
| 51 | helpDescription = """ |
Shawn O. Pearce | 337fb9c | 2009-04-18 10:59:33 -0700 | [diff] [blame] | 52 | The '%prog' command is used to send changes to the Gerrit Code |
| 53 | Review system. It searches for topic branches in local projects |
| 54 | that have not yet been published for review. If multiple topic |
| 55 | branches are found, '%prog' opens an editor to allow the user to |
| 56 | select which branches to upload. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
Shawn O. Pearce | 337fb9c | 2009-04-18 10:59:33 -0700 | [diff] [blame] | 58 | '%prog' searches for uploadable changes in all projects listed at |
| 59 | the command line. Projects can be specified either by name, or by |
| 60 | a relative or absolute path to the project's local directory. If no |
| 61 | projects are specified, '%prog' will search for uploadable changes |
| 62 | in all projects listed in the manifest. |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 63 | |
| 64 | If the --reviewers or --cc options are passed, those emails are |
| 65 | added to the respective list of users, and emails are sent to any |
Shawn O. Pearce | 337fb9c | 2009-04-18 10:59:33 -0700 | [diff] [blame] | 66 | new users. Users passed as --reviewers must already be registered |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 67 | with the code review system, or the upload will fail. |
Shawn O. Pearce | a6df7d2 | 2008-12-12 08:04:07 -0800 | [diff] [blame] | 68 | |
| 69 | If the --replace option is passed the user can designate which |
| 70 | existing change(s) in Gerrit match up to the commits in the branch |
| 71 | being uploaded. For each matched pair of change,commit the commit |
| 72 | will be added as a new patch set, completely replacing the set of |
| 73 | files and description associated with the change in Gerrit. |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 74 | |
| 75 | Configuration |
| 76 | ------------- |
| 77 | |
| 78 | review.URL.autoupload: |
| 79 | |
| 80 | To disable the "Upload ... (y/n)?" prompt, you can set a per-project |
| 81 | or global Git configuration option. If review.URL.autoupload is set |
| 82 | to "true" then repo will assume you always answer "y" at the prompt, |
| 83 | and will not prompt you further. If it is set to "false" then repo |
| 84 | will assume you always answer "n", and will abort. |
| 85 | |
| 86 | The URL must match the review URL listed in the manifest XML file, |
| 87 | or in the .git/config within the project. For example: |
| 88 | |
| 89 | [remote "origin"] |
| 90 | url = git://git.example.com/project.git |
| 91 | review = http://review.example.com/ |
| 92 | |
| 93 | [review "http://review.example.com/"] |
| 94 | autoupload = true |
| 95 | |
Shawn O. Pearce | 337fb9c | 2009-04-18 10:59:33 -0700 | [diff] [blame] | 96 | References |
| 97 | ---------- |
| 98 | |
| 99 | Gerrit Code Review: http://code.google.com/p/gerrit/ |
| 100 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | """ |
| 102 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 103 | def _Options(self, p): |
| 104 | p.add_option('--replace', |
| 105 | dest='replace', action='store_true', |
| 106 | help='Upload replacement patchesets from this branch') |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 107 | p.add_option('--re', '--reviewers', |
| 108 | type='string', action='append', dest='reviewers', |
| 109 | help='Request reviews from these people.') |
| 110 | p.add_option('--cc', |
| 111 | type='string', action='append', dest='cc', |
| 112 | help='Also send email to these email addresses.') |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 113 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 114 | def _SingleBranch(self, branch, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | project = branch.project |
| 116 | name = branch.name |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 117 | remote = project.GetBranch(name).remote |
| 118 | |
| 119 | key = 'review.%s.autoupload' % remote.review |
| 120 | answer = project.config.GetBoolean(key) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 122 | if answer is False: |
| 123 | _die("upload blocked by %s = false" % key) |
| 124 | |
| 125 | if answer is None: |
Shawn O. Pearce | 66bdd46 | 2009-04-17 18:47:22 -0700 | [diff] [blame] | 126 | date = branch.date |
| 127 | list = branch.commits |
| 128 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 129 | print 'Upload project %s/:' % project.relpath |
| 130 | print ' branch %s (%2d commit%s, %s):' % ( |
| 131 | name, |
| 132 | len(list), |
| 133 | len(list) != 1 and 's' or '', |
| 134 | date) |
| 135 | for commit in list: |
| 136 | print ' %s' % commit |
| 137 | |
Shawn O. Pearce | 8225cdc | 2009-04-18 11:00:35 -0700 | [diff] [blame] | 138 | sys.stdout.write('to %s (y/n)? ' % remote.review) |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 139 | answer = sys.stdin.readline().strip() |
| 140 | answer = answer in ('y', 'Y', 'yes', '1', 'true', 't') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 142 | if answer: |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 143 | if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD: |
| 144 | answer = _ConfirmManyUploads() |
| 145 | |
| 146 | if answer: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 147 | self._UploadAndReport([branch], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | else: |
| 149 | _die("upload aborted by user") |
| 150 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 151 | def _MultipleBranches(self, pending, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 152 | projects = {} |
| 153 | branches = {} |
| 154 | |
| 155 | script = [] |
| 156 | script.append('# Uncomment the branches to upload:') |
| 157 | for project, avail in pending: |
| 158 | script.append('#') |
| 159 | script.append('# project %s/:' % project.relpath) |
| 160 | |
| 161 | b = {} |
| 162 | for branch in avail: |
| 163 | name = branch.name |
| 164 | date = branch.date |
| 165 | list = branch.commits |
| 166 | |
| 167 | if b: |
| 168 | script.append('#') |
| 169 | script.append('# branch %s (%2d commit%s, %s):' % ( |
| 170 | name, |
| 171 | len(list), |
| 172 | len(list) != 1 and 's' or '', |
| 173 | date)) |
| 174 | for commit in list: |
| 175 | script.append('# %s' % commit) |
| 176 | b[name] = branch |
| 177 | |
| 178 | projects[project.relpath] = project |
| 179 | branches[project.name] = b |
| 180 | script.append('') |
| 181 | |
| 182 | script = Editor.EditString("\n".join(script)).split("\n") |
| 183 | |
| 184 | project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$') |
| 185 | branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*') |
| 186 | |
| 187 | project = None |
| 188 | todo = [] |
| 189 | |
| 190 | for line in script: |
| 191 | m = project_re.match(line) |
| 192 | if m: |
| 193 | name = m.group(1) |
| 194 | project = projects.get(name) |
| 195 | if not project: |
| 196 | _die('project %s not available for upload', name) |
| 197 | continue |
| 198 | |
| 199 | m = branch_re.match(line) |
| 200 | if m: |
| 201 | name = m.group(1) |
| 202 | if not project: |
| 203 | _die('project for branch %s not in script', name) |
| 204 | branch = branches[project.name].get(name) |
| 205 | if not branch: |
| 206 | _die('branch %s not in %s', name, project.relpath) |
| 207 | todo.append(branch) |
| 208 | if not todo: |
| 209 | _die("nothing uncommented for upload") |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 210 | |
| 211 | many_commits = False |
| 212 | for branch in todo: |
| 213 | if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD: |
| 214 | many_commits = True |
| 215 | break |
| 216 | if many_commits: |
| 217 | if not _ConfirmManyUploads(multiple_branches=True): |
| 218 | _die("upload aborted by user") |
| 219 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 220 | self._UploadAndReport(todo, people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 222 | def _FindGerritChange(self, branch): |
| 223 | last_pub = branch.project.WasPublished(branch.name) |
| 224 | if last_pub is None: |
| 225 | return "" |
| 226 | |
| 227 | refs = branch.GetPublishedRefs() |
| 228 | try: |
| 229 | # refs/changes/XYZ/N --> XYZ |
| 230 | return refs.get(last_pub).split('/')[-2] |
| 231 | except: |
| 232 | return "" |
| 233 | |
Shawn O. Pearce | e92ceeb | 2008-11-24 15:51:25 -0800 | [diff] [blame] | 234 | def _ReplaceBranch(self, project, people): |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 235 | branch = project.CurrentBranch |
| 236 | if not branch: |
| 237 | print >>sys.stdout, "no branches ready for upload" |
| 238 | return |
| 239 | branch = project.GetUploadableBranch(branch) |
| 240 | if not branch: |
| 241 | print >>sys.stdout, "no branches ready for upload" |
| 242 | return |
| 243 | |
| 244 | script = [] |
| 245 | script.append('# Replacing from branch %s' % branch.name) |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 246 | |
| 247 | if len(branch.commits) == 1: |
| 248 | change = self._FindGerritChange(branch) |
| 249 | script.append('[%-6s] %s' % (change, branch.commits[0])) |
| 250 | else: |
| 251 | for commit in branch.commits: |
| 252 | script.append('[ ] %s' % commit) |
| 253 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 254 | script.append('') |
| 255 | script.append('# Insert change numbers in the brackets to add a new patch set.') |
| 256 | script.append('# To create a new change record, leave the brackets empty.') |
| 257 | |
| 258 | script = Editor.EditString("\n".join(script)).split("\n") |
| 259 | |
| 260 | change_re = re.compile(r'^\[\s*(\d{1,})\s*\]\s*([0-9a-f]{1,}) .*$') |
| 261 | to_replace = dict() |
| 262 | full_hashes = branch.unabbrev_commits |
| 263 | |
| 264 | for line in script: |
| 265 | m = change_re.match(line) |
| 266 | if m: |
Shawn O. Pearce | 6709244 | 2008-12-12 08:01:12 -0800 | [diff] [blame] | 267 | c = m.group(1) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 268 | f = m.group(2) |
| 269 | try: |
| 270 | f = full_hashes[f] |
| 271 | except KeyError: |
| 272 | print 'fh = %s' % full_hashes |
| 273 | print >>sys.stderr, "error: commit %s not found" % f |
| 274 | sys.exit(1) |
Shawn O. Pearce | 6709244 | 2008-12-12 08:01:12 -0800 | [diff] [blame] | 275 | if c in to_replace: |
| 276 | print >>sys.stderr,\ |
| 277 | "error: change %s cannot accept multiple commits" % c |
| 278 | sys.exit(1) |
| 279 | to_replace[c] = f |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 280 | |
| 281 | if not to_replace: |
| 282 | print >>sys.stderr, "error: no replacements specified" |
| 283 | print >>sys.stderr, " use 'repo upload' without --replace" |
| 284 | sys.exit(1) |
| 285 | |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 286 | if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD: |
| 287 | if not _ConfirmManyUploads(multiple_branches=True): |
| 288 | _die("upload aborted by user") |
| 289 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 290 | branch.replace_changes = to_replace |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 291 | self._UploadAndReport([branch], people) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 292 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 293 | def _UploadAndReport(self, todo, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 294 | have_errors = False |
| 295 | for branch in todo: |
| 296 | try: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 297 | branch.UploadForReview(people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | branch.uploaded = True |
| 299 | except UploadError, e: |
| 300 | branch.error = e |
| 301 | branch.uploaded = False |
| 302 | have_errors = True |
| 303 | |
| 304 | print >>sys.stderr, '' |
| 305 | print >>sys.stderr, '--------------------------------------------' |
| 306 | |
| 307 | if have_errors: |
| 308 | for branch in todo: |
| 309 | if not branch.uploaded: |
| 310 | print >>sys.stderr, '[FAILED] %-15s %-15s (%s)' % ( |
| 311 | branch.project.relpath + '/', \ |
| 312 | branch.name, \ |
| 313 | branch.error) |
| 314 | print >>sys.stderr, '' |
| 315 | |
| 316 | for branch in todo: |
| 317 | if branch.uploaded: |
| 318 | print >>sys.stderr, '[OK ] %-15s %s' % ( |
| 319 | branch.project.relpath + '/', |
| 320 | branch.name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 321 | |
| 322 | if have_errors: |
| 323 | sys.exit(1) |
| 324 | |
| 325 | def Execute(self, opt, args): |
| 326 | project_list = self.GetProjects(args) |
| 327 | pending = [] |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 328 | reviewers = [] |
| 329 | cc = [] |
| 330 | |
| 331 | if opt.reviewers: |
| 332 | reviewers = _SplitEmails(opt.reviewers) |
| 333 | if opt.cc: |
| 334 | cc = _SplitEmails(opt.cc) |
| 335 | people = (reviewers,cc) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 336 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 337 | if opt.replace: |
| 338 | if len(project_list) != 1: |
| 339 | print >>sys.stderr, \ |
| 340 | 'error: --replace requires exactly one project' |
| 341 | sys.exit(1) |
Shawn O. Pearce | e92ceeb | 2008-11-24 15:51:25 -0800 | [diff] [blame] | 342 | self._ReplaceBranch(project_list[0], people) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 343 | return |
| 344 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | for project in project_list: |
| 346 | avail = project.GetUploadableBranches() |
| 347 | if avail: |
| 348 | pending.append((project, avail)) |
| 349 | |
| 350 | if not pending: |
| 351 | print >>sys.stdout, "no branches ready for upload" |
| 352 | elif len(pending) == 1 and len(pending[0][1]) == 1: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 353 | self._SingleBranch(pending[0][1][0], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 354 | else: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 355 | self._MultipleBranches(pending, people) |