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 |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 22 | from error import HookError, UploadError |
| 23 | from project import RepoHook |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | |
Dan Morrill | f0a9a1a | 2010-05-05 08:18:35 -0700 | [diff] [blame] | 25 | UNUSUAL_COMMIT_THRESHOLD = 5 |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 26 | |
| 27 | def _ConfirmManyUploads(multiple_branches=False): |
| 28 | if multiple_branches: |
| 29 | print "ATTENTION: One or more branches has an unusually high number of commits." |
| 30 | else: |
| 31 | print "ATTENTION: You are uploading an unusually high number of commits." |
| 32 | print "YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across branches?)" |
| 33 | answer = raw_input("If you are sure you intend to do this, type 'yes': ").strip() |
| 34 | return answer == "yes" |
| 35 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | def _die(fmt, *args): |
| 37 | msg = fmt % args |
| 38 | print >>sys.stderr, 'error: %s' % msg |
| 39 | sys.exit(1) |
| 40 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 41 | def _SplitEmails(values): |
| 42 | result = [] |
| 43 | for str in values: |
| 44 | result.extend([s.strip() for s in str.split(',')]) |
| 45 | return result |
| 46 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | class Upload(InteractiveCommand): |
| 48 | common = True |
| 49 | helpSummary = "Upload changes for code review" |
| 50 | helpUsage=""" |
Ficus Kirkpatrick | a0de6e8 | 2010-10-22 13:06:47 -0700 | [diff] [blame] | 51 | %prog [--re --cc] [<project>]... |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | """ |
| 53 | helpDescription = """ |
Shawn O. Pearce | 337fb9c | 2009-04-18 10:59:33 -0700 | [diff] [blame] | 54 | The '%prog' command is used to send changes to the Gerrit Code |
| 55 | Review system. It searches for topic branches in local projects |
| 56 | that have not yet been published for review. If multiple topic |
| 57 | branches are found, '%prog' opens an editor to allow the user to |
| 58 | select which branches to upload. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | |
Shawn O. Pearce | 337fb9c | 2009-04-18 10:59:33 -0700 | [diff] [blame] | 60 | '%prog' searches for uploadable changes in all projects listed at |
| 61 | the command line. Projects can be specified either by name, or by |
| 62 | a relative or absolute path to the project's local directory. If no |
| 63 | projects are specified, '%prog' will search for uploadable changes |
| 64 | in all projects listed in the manifest. |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 65 | |
| 66 | If the --reviewers or --cc options are passed, those emails are |
| 67 | 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] | 68 | new users. Users passed as --reviewers must already be registered |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 69 | with the code review system, or the upload will fail. |
Shawn O. Pearce | a6df7d2 | 2008-12-12 08:04:07 -0800 | [diff] [blame] | 70 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 71 | Configuration |
| 72 | ------------- |
| 73 | |
| 74 | review.URL.autoupload: |
| 75 | |
| 76 | To disable the "Upload ... (y/n)?" prompt, you can set a per-project |
| 77 | or global Git configuration option. If review.URL.autoupload is set |
| 78 | to "true" then repo will assume you always answer "y" at the prompt, |
| 79 | and will not prompt you further. If it is set to "false" then repo |
| 80 | will assume you always answer "n", and will abort. |
| 81 | |
Ben Komalo | 08a3f68 | 2010-07-15 16:03:02 -0700 | [diff] [blame] | 82 | review.URL.autocopy: |
| 83 | |
| 84 | To automatically copy a user or mailing list to all uploaded reviews, |
| 85 | you can set a per-project or global Git option to do so. Specifically, |
| 86 | review.URL.autocopy can be set to a comma separated list of reviewers |
| 87 | who you always want copied on all uploads with a non-empty --re |
| 88 | argument. |
| 89 | |
Shawn O. Pearce | 3575b8f | 2010-07-15 17:00:14 -0700 | [diff] [blame] | 90 | review.URL.username: |
| 91 | |
| 92 | Override the username used to connect to Gerrit Code Review. |
| 93 | By default the local part of the email address is used. |
| 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): |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 114 | p.add_option('-t', |
| 115 | dest='auto_topic', action='store_true', |
| 116 | help='Send local branch name to Gerrit Code Review') |
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 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 124 | # Options relating to upload hook. Note that verify and no-verify are NOT |
| 125 | # opposites of each other, which is why they store to different locations. |
| 126 | # We are using them to match 'git commit' syntax. |
| 127 | # |
| 128 | # Combinations: |
| 129 | # - no-verify=False, verify=False (DEFAULT): |
| 130 | # If stdout is a tty, can prompt about running upload hooks if needed. |
| 131 | # If user denies running hooks, the upload is cancelled. If stdout is |
| 132 | # not a tty and we would need to prompt about upload hooks, upload is |
| 133 | # cancelled. |
| 134 | # - no-verify=False, verify=True: |
| 135 | # Always run upload hooks with no prompt. |
| 136 | # - no-verify=True, verify=False: |
| 137 | # Never run upload hooks, but upload anyway (AKA bypass hooks). |
| 138 | # - no-verify=True, verify=True: |
| 139 | # Invalid |
| 140 | p.add_option('--no-verify', |
| 141 | dest='bypass_hooks', action='store_true', |
| 142 | help='Do not run the upload hook.') |
| 143 | p.add_option('--verify', |
| 144 | dest='allow_all_hooks', action='store_true', |
| 145 | help='Run the upload hook without prompting.') |
| 146 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 147 | def _SingleBranch(self, opt, branch, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | project = branch.project |
| 149 | name = branch.name |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 150 | remote = project.GetBranch(name).remote |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 152 | key = 'review.%s.autoupload' % remote.review |
| 153 | answer = project.config.GetBoolean(key) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 155 | if answer is False: |
| 156 | _die("upload blocked by %s = false" % key) |
| 157 | |
| 158 | if answer is None: |
Shawn O. Pearce | 66bdd46 | 2009-04-17 18:47:22 -0700 | [diff] [blame] | 159 | date = branch.date |
| 160 | list = branch.commits |
| 161 | |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 162 | print 'Upload project %s/:' % project.relpath |
| 163 | print ' branch %s (%2d commit%s, %s):' % ( |
| 164 | name, |
| 165 | len(list), |
| 166 | len(list) != 1 and 's' or '', |
| 167 | date) |
| 168 | for commit in list: |
| 169 | print ' %s' % commit |
| 170 | |
Shawn O. Pearce | 8225cdc | 2009-04-18 11:00:35 -0700 | [diff] [blame] | 171 | sys.stdout.write('to %s (y/n)? ' % remote.review) |
Shawn O. Pearce | a608fb0 | 2009-04-17 12:11:24 -0700 | [diff] [blame] | 172 | answer = sys.stdin.readline().strip() |
| 173 | answer = answer in ('y', 'Y', 'yes', '1', 'true', 't') |
| 174 | |
| 175 | if answer: |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 176 | if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD: |
| 177 | answer = _ConfirmManyUploads() |
| 178 | |
| 179 | if answer: |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 180 | self._UploadAndReport(opt, [branch], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 181 | else: |
| 182 | _die("upload aborted by user") |
| 183 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 184 | def _MultipleBranches(self, opt, pending, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 185 | projects = {} |
| 186 | branches = {} |
| 187 | |
| 188 | script = [] |
| 189 | script.append('# Uncomment the branches to upload:') |
| 190 | for project, avail in pending: |
| 191 | script.append('#') |
| 192 | script.append('# project %s/:' % project.relpath) |
| 193 | |
| 194 | b = {} |
| 195 | for branch in avail: |
| 196 | name = branch.name |
| 197 | date = branch.date |
| 198 | list = branch.commits |
| 199 | |
| 200 | if b: |
| 201 | script.append('#') |
| 202 | script.append('# branch %s (%2d commit%s, %s):' % ( |
| 203 | name, |
| 204 | len(list), |
| 205 | len(list) != 1 and 's' or '', |
| 206 | date)) |
| 207 | for commit in list: |
| 208 | script.append('# %s' % commit) |
| 209 | b[name] = branch |
| 210 | |
| 211 | projects[project.relpath] = project |
| 212 | branches[project.name] = b |
| 213 | script.append('') |
| 214 | |
| 215 | script = Editor.EditString("\n".join(script)).split("\n") |
| 216 | |
| 217 | project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$') |
| 218 | branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*') |
| 219 | |
| 220 | project = None |
| 221 | todo = [] |
| 222 | |
| 223 | for line in script: |
| 224 | m = project_re.match(line) |
| 225 | if m: |
| 226 | name = m.group(1) |
| 227 | project = projects.get(name) |
| 228 | if not project: |
| 229 | _die('project %s not available for upload', name) |
| 230 | continue |
| 231 | |
| 232 | m = branch_re.match(line) |
| 233 | if m: |
| 234 | name = m.group(1) |
| 235 | if not project: |
| 236 | _die('project for branch %s not in script', name) |
| 237 | branch = branches[project.name].get(name) |
| 238 | if not branch: |
| 239 | _die('branch %s not in %s', name, project.relpath) |
| 240 | todo.append(branch) |
| 241 | if not todo: |
| 242 | _die("nothing uncommented for upload") |
Dan Morrill | 879a9a5 | 2010-05-04 16:56:07 -0700 | [diff] [blame] | 243 | |
| 244 | many_commits = False |
| 245 | for branch in todo: |
| 246 | if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD: |
| 247 | many_commits = True |
| 248 | break |
| 249 | if many_commits: |
| 250 | if not _ConfirmManyUploads(multiple_branches=True): |
| 251 | _die("upload aborted by user") |
| 252 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 253 | self._UploadAndReport(opt, todo, people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | |
Ben Komalo | 08a3f68 | 2010-07-15 16:03:02 -0700 | [diff] [blame] | 255 | def _AppendAutoCcList(self, branch, people): |
| 256 | """ |
| 257 | Appends the list of users in the CC list in the git project's config if a |
| 258 | non-empty reviewer list was found. |
| 259 | """ |
| 260 | |
| 261 | name = branch.name |
| 262 | project = branch.project |
| 263 | key = 'review.%s.autocopy' % project.GetBranch(name).remote.review |
| 264 | raw_list = project.config.GetString(key) |
| 265 | if not raw_list is None and len(people[0]) > 0: |
| 266 | people[1].extend([entry.strip() for entry in raw_list.split(',')]) |
| 267 | |
Ficus Kirkpatrick | bc7ef67 | 2009-05-04 12:45:11 -0700 | [diff] [blame] | 268 | def _FindGerritChange(self, branch): |
| 269 | last_pub = branch.project.WasPublished(branch.name) |
| 270 | if last_pub is None: |
| 271 | return "" |
| 272 | |
| 273 | refs = branch.GetPublishedRefs() |
| 274 | try: |
| 275 | # refs/changes/XYZ/N --> XYZ |
| 276 | return refs.get(last_pub).split('/')[-2] |
| 277 | except: |
| 278 | return "" |
| 279 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 280 | def _UploadAndReport(self, opt, todo, original_people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 281 | have_errors = False |
| 282 | for branch in todo: |
| 283 | try: |
Ben Komalo | 08a3f68 | 2010-07-15 16:03:02 -0700 | [diff] [blame] | 284 | people = copy.deepcopy(original_people) |
| 285 | self._AppendAutoCcList(branch, people) |
| 286 | |
Anthony Newnam | cc50bac | 2010-04-08 10:28:59 -0500 | [diff] [blame] | 287 | # Check if there are local changes that may have been forgotten |
| 288 | if branch.project.HasChanges(): |
| 289 | key = 'review.%s.autoupload' % branch.project.remote.review |
| 290 | answer = branch.project.config.GetBoolean(key) |
| 291 | |
| 292 | # if they want to auto upload, let's not ask because it could be automated |
| 293 | if answer is None: |
| 294 | sys.stdout.write('Uncommitted changes in ' + branch.project.name + ' (did you forget to amend?). Continue uploading? (y/n) ') |
| 295 | a = sys.stdin.readline().strip().lower() |
| 296 | if a not in ('y', 'yes', 't', 'true', 'on'): |
| 297 | print >>sys.stderr, "skipping upload" |
| 298 | branch.uploaded = False |
| 299 | branch.error = 'User aborted' |
| 300 | continue |
| 301 | |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 302 | branch.UploadForReview(people, auto_topic=opt.auto_topic) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 303 | branch.uploaded = True |
| 304 | except UploadError, e: |
| 305 | branch.error = e |
| 306 | branch.uploaded = False |
| 307 | have_errors = True |
| 308 | |
| 309 | print >>sys.stderr, '' |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 310 | print >>sys.stderr, '----------------------------------------------------------------------' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | |
| 312 | if have_errors: |
| 313 | for branch in todo: |
| 314 | if not branch.uploaded: |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 315 | if len(str(branch.error)) <= 30: |
| 316 | fmt = ' (%s)' |
| 317 | else: |
| 318 | fmt = '\n (%s)' |
| 319 | print >>sys.stderr, ('[FAILED] %-15s %-15s' + fmt) % ( |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 320 | branch.project.relpath + '/', \ |
| 321 | branch.name, \ |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 322 | str(branch.error)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | print >>sys.stderr, '' |
| 324 | |
| 325 | for branch in todo: |
| 326 | if branch.uploaded: |
| 327 | print >>sys.stderr, '[OK ] %-15s %s' % ( |
| 328 | branch.project.relpath + '/', |
| 329 | branch.name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 330 | |
| 331 | if have_errors: |
| 332 | sys.exit(1) |
| 333 | |
| 334 | def Execute(self, opt, args): |
| 335 | project_list = self.GetProjects(args) |
| 336 | pending = [] |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 337 | reviewers = [] |
| 338 | cc = [] |
| 339 | |
Doug Anderson | 37282b4 | 2011-03-04 11:54:18 -0800 | [diff] [blame] | 340 | for project in project_list: |
| 341 | avail = project.GetUploadableBranches() |
| 342 | if avail: |
| 343 | pending.append((project, avail)) |
| 344 | |
| 345 | if pending and (not opt.bypass_hooks): |
| 346 | hook = RepoHook('pre-upload', self.manifest.repo_hooks_project, |
| 347 | self.manifest.topdir, abort_if_user_denies=True) |
| 348 | pending_proj_names = [project.name for (project, avail) in pending] |
| 349 | try: |
| 350 | hook.Run(opt.allow_all_hooks, project_list=pending_proj_names) |
| 351 | except HookError, e: |
| 352 | print >>sys.stderr, "ERROR: %s" % str(e) |
| 353 | return |
| 354 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 355 | if opt.reviewers: |
| 356 | reviewers = _SplitEmails(opt.reviewers) |
| 357 | if opt.cc: |
| 358 | cc = _SplitEmails(opt.cc) |
| 359 | people = (reviewers,cc) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 360 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | if not pending: |
| 362 | print >>sys.stdout, "no branches ready for upload" |
| 363 | elif len(pending) == 1 and len(pending[0][1]) == 1: |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 364 | self._SingleBranch(opt, pending[0][1][0], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | else: |
Shawn O. Pearce | a5ece0e | 2010-07-15 16:52:42 -0700 | [diff] [blame] | 366 | self._MultipleBranches(opt, pending, people) |