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 | |
Shawn O. Pearce | e92ceeb | 2008-11-24 15:51:25 -0800 | [diff] [blame] | 197 | def _ReplaceBranch(self, project, people): |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 198 | branch = project.CurrentBranch |
| 199 | if not branch: |
| 200 | print >>sys.stdout, "no branches ready for upload" |
| 201 | return |
| 202 | branch = project.GetUploadableBranch(branch) |
| 203 | if not branch: |
| 204 | print >>sys.stdout, "no branches ready for upload" |
| 205 | return |
| 206 | |
| 207 | script = [] |
| 208 | script.append('# Replacing from branch %s' % branch.name) |
| 209 | for commit in branch.commits: |
| 210 | script.append('[ ] %s' % commit) |
| 211 | script.append('') |
| 212 | script.append('# Insert change numbers in the brackets to add a new patch set.') |
| 213 | script.append('# To create a new change record, leave the brackets empty.') |
| 214 | |
| 215 | script = Editor.EditString("\n".join(script)).split("\n") |
| 216 | |
| 217 | change_re = re.compile(r'^\[\s*(\d{1,})\s*\]\s*([0-9a-f]{1,}) .*$') |
| 218 | to_replace = dict() |
| 219 | full_hashes = branch.unabbrev_commits |
| 220 | |
| 221 | for line in script: |
| 222 | m = change_re.match(line) |
| 223 | if m: |
Shawn O. Pearce | 6709244 | 2008-12-12 08:01:12 -0800 | [diff] [blame] | 224 | c = m.group(1) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 225 | f = m.group(2) |
| 226 | try: |
| 227 | f = full_hashes[f] |
| 228 | except KeyError: |
| 229 | print 'fh = %s' % full_hashes |
| 230 | print >>sys.stderr, "error: commit %s not found" % f |
| 231 | sys.exit(1) |
Shawn O. Pearce | 6709244 | 2008-12-12 08:01:12 -0800 | [diff] [blame] | 232 | if c in to_replace: |
| 233 | print >>sys.stderr,\ |
| 234 | "error: change %s cannot accept multiple commits" % c |
| 235 | sys.exit(1) |
| 236 | to_replace[c] = f |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 237 | |
| 238 | if not to_replace: |
| 239 | print >>sys.stderr, "error: no replacements specified" |
| 240 | print >>sys.stderr, " use 'repo upload' without --replace" |
| 241 | sys.exit(1) |
| 242 | |
| 243 | branch.replace_changes = to_replace |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 244 | self._UploadAndReport([branch], people) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 245 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 246 | def _UploadAndReport(self, todo, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 247 | have_errors = False |
| 248 | for branch in todo: |
| 249 | try: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 250 | branch.UploadForReview(people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 251 | branch.uploaded = True |
| 252 | except UploadError, e: |
| 253 | branch.error = e |
| 254 | branch.uploaded = False |
| 255 | have_errors = True |
| 256 | |
| 257 | print >>sys.stderr, '' |
| 258 | print >>sys.stderr, '--------------------------------------------' |
| 259 | |
| 260 | if have_errors: |
| 261 | for branch in todo: |
| 262 | if not branch.uploaded: |
| 263 | print >>sys.stderr, '[FAILED] %-15s %-15s (%s)' % ( |
| 264 | branch.project.relpath + '/', \ |
| 265 | branch.name, \ |
| 266 | branch.error) |
| 267 | print >>sys.stderr, '' |
| 268 | |
| 269 | for branch in todo: |
| 270 | if branch.uploaded: |
| 271 | print >>sys.stderr, '[OK ] %-15s %s' % ( |
| 272 | branch.project.relpath + '/', |
| 273 | branch.name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | |
| 275 | if have_errors: |
| 276 | sys.exit(1) |
| 277 | |
| 278 | def Execute(self, opt, args): |
| 279 | project_list = self.GetProjects(args) |
| 280 | pending = [] |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 281 | reviewers = [] |
| 282 | cc = [] |
| 283 | |
| 284 | if opt.reviewers: |
| 285 | reviewers = _SplitEmails(opt.reviewers) |
| 286 | if opt.cc: |
| 287 | cc = _SplitEmails(opt.cc) |
| 288 | people = (reviewers,cc) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 289 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 290 | if opt.replace: |
| 291 | if len(project_list) != 1: |
| 292 | print >>sys.stderr, \ |
| 293 | 'error: --replace requires exactly one project' |
| 294 | sys.exit(1) |
Shawn O. Pearce | e92ceeb | 2008-11-24 15:51:25 -0800 | [diff] [blame] | 295 | self._ReplaceBranch(project_list[0], people) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 296 | return |
| 297 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | for project in project_list: |
| 299 | avail = project.GetUploadableBranches() |
| 300 | if avail: |
| 301 | pending.append((project, avail)) |
| 302 | |
| 303 | if not pending: |
| 304 | print >>sys.stdout, "no branches ready for upload" |
| 305 | elif len(pending) == 1 and len(pending[0][1]) == 1: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 306 | self._SingleBranch(pending[0][1][0], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 307 | else: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 308 | self._MultipleBranches(pending, people) |