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