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