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 = """ |
| 41 | The '%prog' command is used to send changes to the Gerrit code |
| 42 | review system. It searches for changes in local projects that do |
| 43 | not yet exist in the corresponding remote repository. If multiple |
| 44 | changes are found, '%prog' opens an editor to allow the |
| 45 | user to choose which change to upload. After a successful upload, |
| 46 | repo prints the URL for the change in the Gerrit code review system. |
| 47 | |
| 48 | '%prog' searches for uploadable changes in all projects listed |
| 49 | at the command line. Projects can be specified either by name, or |
| 50 | by a relative or absolute path to the project's local directory. If |
| 51 | no projects are specified, '%prog' will search for uploadable |
| 52 | changes in all projects listed in the manifest. |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 53 | |
| 54 | If the --reviewers or --cc options are passed, those emails are |
| 55 | added to the respective list of users, and emails are sent to any |
| 56 | new users. Users passed to --reviewers must be already registered |
| 57 | with the code review system, or the upload will fail. |
Shawn O. Pearce | a6df7d2 | 2008-12-12 08:04:07 -0800 | [diff] [blame] | 58 | |
| 59 | If the --replace option is passed the user can designate which |
| 60 | existing change(s) in Gerrit match up to the commits in the branch |
| 61 | being uploaded. For each matched pair of change,commit the commit |
| 62 | will be added as a new patch set, completely replacing the set of |
| 63 | files and description associated with the change in Gerrit. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | """ |
| 65 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 66 | def _Options(self, p): |
| 67 | p.add_option('--replace', |
| 68 | dest='replace', action='store_true', |
| 69 | help='Upload replacement patchesets from this branch') |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 70 | p.add_option('--re', '--reviewers', |
| 71 | type='string', action='append', dest='reviewers', |
| 72 | help='Request reviews from these people.') |
| 73 | p.add_option('--cc', |
| 74 | type='string', action='append', dest='cc', |
| 75 | help='Also send email to these email addresses.') |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 76 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 77 | def _SingleBranch(self, branch, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | project = branch.project |
| 79 | name = branch.name |
| 80 | date = branch.date |
| 81 | list = branch.commits |
| 82 | |
| 83 | print 'Upload project %s/:' % project.relpath |
| 84 | print ' branch %s (%2d commit%s, %s):' % ( |
| 85 | name, |
| 86 | len(list), |
| 87 | len(list) != 1 and 's' or '', |
| 88 | date) |
| 89 | for commit in list: |
| 90 | print ' %s' % commit |
| 91 | |
| 92 | sys.stdout.write('(y/n)? ') |
| 93 | answer = sys.stdin.readline().strip() |
| 94 | if answer in ('y', 'Y', 'yes', '1', 'true', 't'): |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 95 | self._UploadAndReport([branch], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | else: |
| 97 | _die("upload aborted by user") |
| 98 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 99 | def _MultipleBranches(self, pending, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | projects = {} |
| 101 | branches = {} |
| 102 | |
| 103 | script = [] |
| 104 | script.append('# Uncomment the branches to upload:') |
| 105 | for project, avail in pending: |
| 106 | script.append('#') |
| 107 | script.append('# project %s/:' % project.relpath) |
| 108 | |
| 109 | b = {} |
| 110 | for branch in avail: |
| 111 | name = branch.name |
| 112 | date = branch.date |
| 113 | list = branch.commits |
| 114 | |
| 115 | if b: |
| 116 | script.append('#') |
| 117 | script.append('# branch %s (%2d commit%s, %s):' % ( |
| 118 | name, |
| 119 | len(list), |
| 120 | len(list) != 1 and 's' or '', |
| 121 | date)) |
| 122 | for commit in list: |
| 123 | script.append('# %s' % commit) |
| 124 | b[name] = branch |
| 125 | |
| 126 | projects[project.relpath] = project |
| 127 | branches[project.name] = b |
| 128 | script.append('') |
| 129 | |
| 130 | script = Editor.EditString("\n".join(script)).split("\n") |
| 131 | |
| 132 | project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$') |
| 133 | branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*') |
| 134 | |
| 135 | project = None |
| 136 | todo = [] |
| 137 | |
| 138 | for line in script: |
| 139 | m = project_re.match(line) |
| 140 | if m: |
| 141 | name = m.group(1) |
| 142 | project = projects.get(name) |
| 143 | if not project: |
| 144 | _die('project %s not available for upload', name) |
| 145 | continue |
| 146 | |
| 147 | m = branch_re.match(line) |
| 148 | if m: |
| 149 | name = m.group(1) |
| 150 | if not project: |
| 151 | _die('project for branch %s not in script', name) |
| 152 | branch = branches[project.name].get(name) |
| 153 | if not branch: |
| 154 | _die('branch %s not in %s', name, project.relpath) |
| 155 | todo.append(branch) |
| 156 | if not todo: |
| 157 | _die("nothing uncommented for upload") |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 158 | self._UploadAndReport(todo, people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | |
Shawn O. Pearce | e92ceeb | 2008-11-24 15:51:25 -0800 | [diff] [blame] | 160 | def _ReplaceBranch(self, project, people): |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 161 | branch = project.CurrentBranch |
| 162 | if not branch: |
| 163 | print >>sys.stdout, "no branches ready for upload" |
| 164 | return |
| 165 | branch = project.GetUploadableBranch(branch) |
| 166 | if not branch: |
| 167 | print >>sys.stdout, "no branches ready for upload" |
| 168 | return |
| 169 | |
| 170 | script = [] |
| 171 | script.append('# Replacing from branch %s' % branch.name) |
| 172 | for commit in branch.commits: |
| 173 | script.append('[ ] %s' % commit) |
| 174 | script.append('') |
| 175 | script.append('# Insert change numbers in the brackets to add a new patch set.') |
| 176 | script.append('# To create a new change record, leave the brackets empty.') |
| 177 | |
| 178 | script = Editor.EditString("\n".join(script)).split("\n") |
| 179 | |
| 180 | change_re = re.compile(r'^\[\s*(\d{1,})\s*\]\s*([0-9a-f]{1,}) .*$') |
| 181 | to_replace = dict() |
| 182 | full_hashes = branch.unabbrev_commits |
| 183 | |
| 184 | for line in script: |
| 185 | m = change_re.match(line) |
| 186 | if m: |
Shawn O. Pearce | 6709244 | 2008-12-12 08:01:12 -0800 | [diff] [blame] | 187 | c = m.group(1) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 188 | f = m.group(2) |
| 189 | try: |
| 190 | f = full_hashes[f] |
| 191 | except KeyError: |
| 192 | print 'fh = %s' % full_hashes |
| 193 | print >>sys.stderr, "error: commit %s not found" % f |
| 194 | sys.exit(1) |
Shawn O. Pearce | 6709244 | 2008-12-12 08:01:12 -0800 | [diff] [blame] | 195 | if c in to_replace: |
| 196 | print >>sys.stderr,\ |
| 197 | "error: change %s cannot accept multiple commits" % c |
| 198 | sys.exit(1) |
| 199 | to_replace[c] = f |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 200 | |
| 201 | if not to_replace: |
| 202 | print >>sys.stderr, "error: no replacements specified" |
| 203 | print >>sys.stderr, " use 'repo upload' without --replace" |
| 204 | sys.exit(1) |
| 205 | |
| 206 | branch.replace_changes = to_replace |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 207 | self._UploadAndReport([branch], people) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 208 | |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 209 | def _UploadAndReport(self, todo, people): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | have_errors = False |
| 211 | for branch in todo: |
| 212 | try: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 213 | branch.UploadForReview(people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | branch.uploaded = True |
| 215 | except UploadError, e: |
| 216 | branch.error = e |
| 217 | branch.uploaded = False |
| 218 | have_errors = True |
| 219 | |
| 220 | print >>sys.stderr, '' |
| 221 | print >>sys.stderr, '--------------------------------------------' |
| 222 | |
| 223 | if have_errors: |
| 224 | for branch in todo: |
| 225 | if not branch.uploaded: |
| 226 | print >>sys.stderr, '[FAILED] %-15s %-15s (%s)' % ( |
| 227 | branch.project.relpath + '/', \ |
| 228 | branch.name, \ |
| 229 | branch.error) |
| 230 | print >>sys.stderr, '' |
| 231 | |
| 232 | for branch in todo: |
| 233 | if branch.uploaded: |
| 234 | print >>sys.stderr, '[OK ] %-15s %s' % ( |
| 235 | branch.project.relpath + '/', |
| 236 | branch.name) |
| 237 | print >>sys.stderr, '%s' % branch.tip_url |
Shawn O. Pearce | 0758d2f | 2008-10-22 13:13:40 -0700 | [diff] [blame] | 238 | print >>sys.stderr, '(as %s)' % branch.owner_email |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | print >>sys.stderr, '' |
| 240 | |
| 241 | if have_errors: |
| 242 | sys.exit(1) |
| 243 | |
| 244 | def Execute(self, opt, args): |
| 245 | project_list = self.GetProjects(args) |
| 246 | pending = [] |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 247 | reviewers = [] |
| 248 | cc = [] |
| 249 | |
| 250 | if opt.reviewers: |
| 251 | reviewers = _SplitEmails(opt.reviewers) |
| 252 | if opt.cc: |
| 253 | cc = _SplitEmails(opt.cc) |
| 254 | people = (reviewers,cc) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 255 | |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 256 | if opt.replace: |
| 257 | if len(project_list) != 1: |
| 258 | print >>sys.stderr, \ |
| 259 | 'error: --replace requires exactly one project' |
| 260 | sys.exit(1) |
Shawn O. Pearce | e92ceeb | 2008-11-24 15:51:25 -0800 | [diff] [blame] | 261 | self._ReplaceBranch(project_list[0], people) |
Shawn O. Pearce | c99883f | 2008-11-11 17:12:43 -0800 | [diff] [blame] | 262 | return |
| 263 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | for project in project_list: |
| 265 | avail = project.GetUploadableBranches() |
| 266 | if avail: |
| 267 | pending.append((project, avail)) |
| 268 | |
| 269 | if not pending: |
| 270 | print >>sys.stdout, "no branches ready for upload" |
| 271 | elif len(pending) == 1 and len(pending[0][1]) == 1: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 272 | self._SingleBranch(pending[0][1][0], people) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | else: |
Joe Onorato | 2896a79 | 2008-11-17 16:56:36 -0500 | [diff] [blame] | 274 | self._MultipleBranches(pending, people) |