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