2008-10-21 14:00:00 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2010-07-15 23:03:02 +00:00
|
|
|
import copy
|
2008-10-21 14:00:00 +00:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from command import InteractiveCommand
|
|
|
|
from editor import Editor
|
|
|
|
from error import UploadError
|
|
|
|
|
2010-05-05 15:18:35 +00:00
|
|
|
UNUSUAL_COMMIT_THRESHOLD = 5
|
2010-05-04 23:56:07 +00:00
|
|
|
|
|
|
|
def _ConfirmManyUploads(multiple_branches=False):
|
|
|
|
if multiple_branches:
|
|
|
|
print "ATTENTION: One or more branches has an unusually high number of commits."
|
|
|
|
else:
|
|
|
|
print "ATTENTION: You are uploading an unusually high number of commits."
|
|
|
|
print "YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across branches?)"
|
|
|
|
answer = raw_input("If you are sure you intend to do this, type 'yes': ").strip()
|
|
|
|
return answer == "yes"
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _die(fmt, *args):
|
|
|
|
msg = fmt % args
|
|
|
|
print >>sys.stderr, 'error: %s' % msg
|
|
|
|
sys.exit(1)
|
|
|
|
|
2008-11-17 21:56:36 +00:00
|
|
|
def _SplitEmails(values):
|
|
|
|
result = []
|
|
|
|
for str in values:
|
|
|
|
result.extend([s.strip() for s in str.split(',')])
|
|
|
|
return result
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
class Upload(InteractiveCommand):
|
|
|
|
common = True
|
|
|
|
helpSummary = "Upload changes for code review"
|
|
|
|
helpUsage="""
|
2010-10-22 20:06:47 +00:00
|
|
|
%prog [--re --cc] [<project>]...
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
|
|
|
helpDescription = """
|
2009-04-18 17:59:33 +00:00
|
|
|
The '%prog' command is used to send changes to the Gerrit Code
|
|
|
|
Review system. It searches for topic branches in local projects
|
|
|
|
that have not yet been published for review. If multiple topic
|
|
|
|
branches are found, '%prog' opens an editor to allow the user to
|
|
|
|
select which branches to upload.
|
|
|
|
|
|
|
|
'%prog' searches for uploadable changes in all projects listed at
|
|
|
|
the command line. Projects can be specified either by name, or by
|
|
|
|
a relative or absolute path to the project's local directory. If no
|
|
|
|
projects are specified, '%prog' will search for uploadable changes
|
|
|
|
in all projects listed in the manifest.
|
2008-11-17 21:56:36 +00:00
|
|
|
|
|
|
|
If the --reviewers or --cc options are passed, those emails are
|
|
|
|
added to the respective list of users, and emails are sent to any
|
2009-04-18 17:59:33 +00:00
|
|
|
new users. Users passed as --reviewers must already be registered
|
2008-11-17 21:56:36 +00:00
|
|
|
with the code review system, or the upload will fail.
|
2008-12-12 16:04:07 +00:00
|
|
|
|
2009-04-17 19:11:24 +00:00
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
|
|
|
review.URL.autoupload:
|
|
|
|
|
|
|
|
To disable the "Upload ... (y/n)?" prompt, you can set a per-project
|
|
|
|
or global Git configuration option. If review.URL.autoupload is set
|
|
|
|
to "true" then repo will assume you always answer "y" at the prompt,
|
|
|
|
and will not prompt you further. If it is set to "false" then repo
|
|
|
|
will assume you always answer "n", and will abort.
|
|
|
|
|
2010-07-15 23:03:02 +00:00
|
|
|
review.URL.autocopy:
|
|
|
|
|
|
|
|
To automatically copy a user or mailing list to all uploaded reviews,
|
|
|
|
you can set a per-project or global Git option to do so. Specifically,
|
|
|
|
review.URL.autocopy can be set to a comma separated list of reviewers
|
|
|
|
who you always want copied on all uploads with a non-empty --re
|
|
|
|
argument.
|
|
|
|
|
2010-07-16 00:00:14 +00:00
|
|
|
review.URL.username:
|
|
|
|
|
|
|
|
Override the username used to connect to Gerrit Code Review.
|
|
|
|
By default the local part of the email address is used.
|
|
|
|
|
2009-04-17 19:11:24 +00:00
|
|
|
The URL must match the review URL listed in the manifest XML file,
|
|
|
|
or in the .git/config within the project. For example:
|
|
|
|
|
|
|
|
[remote "origin"]
|
|
|
|
url = git://git.example.com/project.git
|
|
|
|
review = http://review.example.com/
|
|
|
|
|
|
|
|
[review "http://review.example.com/"]
|
|
|
|
autoupload = true
|
2010-07-15 23:03:02 +00:00
|
|
|
autocopy = johndoe@company.com,my-team-alias@company.com
|
2009-04-17 19:11:24 +00:00
|
|
|
|
2009-04-18 17:59:33 +00:00
|
|
|
References
|
|
|
|
----------
|
|
|
|
|
|
|
|
Gerrit Code Review: http://code.google.com/p/gerrit/
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
|
|
|
|
2008-11-12 01:12:43 +00:00
|
|
|
def _Options(self, p):
|
2010-07-15 23:52:42 +00:00
|
|
|
p.add_option('-t',
|
|
|
|
dest='auto_topic', action='store_true',
|
|
|
|
help='Send local branch name to Gerrit Code Review')
|
2008-11-17 21:56:36 +00:00
|
|
|
p.add_option('--re', '--reviewers',
|
|
|
|
type='string', action='append', dest='reviewers',
|
|
|
|
help='Request reviews from these people.')
|
|
|
|
p.add_option('--cc',
|
|
|
|
type='string', action='append', dest='cc',
|
|
|
|
help='Also send email to these email addresses.')
|
|
|
|
|
2010-07-15 23:52:42 +00:00
|
|
|
def _SingleBranch(self, opt, branch, people):
|
2008-10-21 14:00:00 +00:00
|
|
|
project = branch.project
|
|
|
|
name = branch.name
|
2009-04-17 19:11:24 +00:00
|
|
|
remote = project.GetBranch(name).remote
|
|
|
|
|
|
|
|
key = 'review.%s.autoupload' % remote.review
|
|
|
|
answer = project.config.GetBoolean(key)
|
|
|
|
|
|
|
|
if answer is False:
|
|
|
|
_die("upload blocked by %s = false" % key)
|
|
|
|
|
|
|
|
if answer is None:
|
2009-04-18 01:47:22 +00:00
|
|
|
date = branch.date
|
|
|
|
list = branch.commits
|
|
|
|
|
2009-04-17 19:11:24 +00:00
|
|
|
print 'Upload project %s/:' % project.relpath
|
|
|
|
print ' branch %s (%2d commit%s, %s):' % (
|
|
|
|
name,
|
|
|
|
len(list),
|
|
|
|
len(list) != 1 and 's' or '',
|
|
|
|
date)
|
|
|
|
for commit in list:
|
|
|
|
print ' %s' % commit
|
|
|
|
|
2009-04-18 18:00:35 +00:00
|
|
|
sys.stdout.write('to %s (y/n)? ' % remote.review)
|
2009-04-17 19:11:24 +00:00
|
|
|
answer = sys.stdin.readline().strip()
|
|
|
|
answer = answer in ('y', 'Y', 'yes', '1', 'true', 't')
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-04-17 19:11:24 +00:00
|
|
|
if answer:
|
2010-05-04 23:56:07 +00:00
|
|
|
if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD:
|
|
|
|
answer = _ConfirmManyUploads()
|
|
|
|
|
2009-04-17 19:11:24 +00:00
|
|
|
if answer:
|
2010-07-15 23:52:42 +00:00
|
|
|
self._UploadAndReport(opt, [branch], people)
|
2008-10-21 14:00:00 +00:00
|
|
|
else:
|
|
|
|
_die("upload aborted by user")
|
|
|
|
|
2010-07-15 23:52:42 +00:00
|
|
|
def _MultipleBranches(self, opt, pending, people):
|
2008-10-21 14:00:00 +00:00
|
|
|
projects = {}
|
|
|
|
branches = {}
|
|
|
|
|
|
|
|
script = []
|
|
|
|
script.append('# Uncomment the branches to upload:')
|
|
|
|
for project, avail in pending:
|
|
|
|
script.append('#')
|
|
|
|
script.append('# project %s/:' % project.relpath)
|
|
|
|
|
|
|
|
b = {}
|
|
|
|
for branch in avail:
|
|
|
|
name = branch.name
|
|
|
|
date = branch.date
|
|
|
|
list = branch.commits
|
|
|
|
|
|
|
|
if b:
|
|
|
|
script.append('#')
|
|
|
|
script.append('# branch %s (%2d commit%s, %s):' % (
|
|
|
|
name,
|
|
|
|
len(list),
|
|
|
|
len(list) != 1 and 's' or '',
|
|
|
|
date))
|
|
|
|
for commit in list:
|
|
|
|
script.append('# %s' % commit)
|
|
|
|
b[name] = branch
|
|
|
|
|
|
|
|
projects[project.relpath] = project
|
|
|
|
branches[project.name] = b
|
|
|
|
script.append('')
|
|
|
|
|
|
|
|
script = Editor.EditString("\n".join(script)).split("\n")
|
|
|
|
|
|
|
|
project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
|
|
|
|
branch_re = re.compile(r'^\s*branch\s*([^\s(]+)\s*\(.*')
|
|
|
|
|
|
|
|
project = None
|
|
|
|
todo = []
|
|
|
|
|
|
|
|
for line in script:
|
|
|
|
m = project_re.match(line)
|
|
|
|
if m:
|
|
|
|
name = m.group(1)
|
|
|
|
project = projects.get(name)
|
|
|
|
if not project:
|
|
|
|
_die('project %s not available for upload', name)
|
|
|
|
continue
|
|
|
|
|
|
|
|
m = branch_re.match(line)
|
|
|
|
if m:
|
|
|
|
name = m.group(1)
|
|
|
|
if not project:
|
|
|
|
_die('project for branch %s not in script', name)
|
|
|
|
branch = branches[project.name].get(name)
|
|
|
|
if not branch:
|
|
|
|
_die('branch %s not in %s', name, project.relpath)
|
|
|
|
todo.append(branch)
|
|
|
|
if not todo:
|
|
|
|
_die("nothing uncommented for upload")
|
2010-05-04 23:56:07 +00:00
|
|
|
|
|
|
|
many_commits = False
|
|
|
|
for branch in todo:
|
|
|
|
if len(branch.commits) > UNUSUAL_COMMIT_THRESHOLD:
|
|
|
|
many_commits = True
|
|
|
|
break
|
|
|
|
if many_commits:
|
|
|
|
if not _ConfirmManyUploads(multiple_branches=True):
|
|
|
|
_die("upload aborted by user")
|
|
|
|
|
2010-07-15 23:52:42 +00:00
|
|
|
self._UploadAndReport(opt, todo, people)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2010-07-15 23:03:02 +00:00
|
|
|
def _AppendAutoCcList(self, branch, people):
|
|
|
|
"""
|
|
|
|
Appends the list of users in the CC list in the git project's config if a
|
|
|
|
non-empty reviewer list was found.
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = branch.name
|
|
|
|
project = branch.project
|
|
|
|
key = 'review.%s.autocopy' % project.GetBranch(name).remote.review
|
|
|
|
raw_list = project.config.GetString(key)
|
|
|
|
if not raw_list is None and len(people[0]) > 0:
|
|
|
|
people[1].extend([entry.strip() for entry in raw_list.split(',')])
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-05-04 19:45:11 +00:00
|
|
|
def _FindGerritChange(self, branch):
|
|
|
|
last_pub = branch.project.WasPublished(branch.name)
|
|
|
|
if last_pub is None:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
refs = branch.GetPublishedRefs()
|
|
|
|
try:
|
|
|
|
# refs/changes/XYZ/N --> XYZ
|
|
|
|
return refs.get(last_pub).split('/')[-2]
|
|
|
|
except:
|
|
|
|
return ""
|
|
|
|
|
2010-07-15 23:52:42 +00:00
|
|
|
def _UploadAndReport(self, opt, todo, original_people):
|
2008-10-21 14:00:00 +00:00
|
|
|
have_errors = False
|
|
|
|
for branch in todo:
|
|
|
|
try:
|
2010-07-15 23:03:02 +00:00
|
|
|
people = copy.deepcopy(original_people)
|
|
|
|
self._AppendAutoCcList(branch, people)
|
|
|
|
|
2010-04-08 15:28:59 +00:00
|
|
|
# Check if there are local changes that may have been forgotten
|
|
|
|
if branch.project.HasChanges():
|
|
|
|
key = 'review.%s.autoupload' % branch.project.remote.review
|
|
|
|
answer = branch.project.config.GetBoolean(key)
|
|
|
|
|
|
|
|
# if they want to auto upload, let's not ask because it could be automated
|
|
|
|
if answer is None:
|
|
|
|
sys.stdout.write('Uncommitted changes in ' + branch.project.name + ' (did you forget to amend?). Continue uploading? (y/n) ')
|
|
|
|
a = sys.stdin.readline().strip().lower()
|
|
|
|
if a not in ('y', 'yes', 't', 'true', 'on'):
|
|
|
|
print >>sys.stderr, "skipping upload"
|
|
|
|
branch.uploaded = False
|
|
|
|
branch.error = 'User aborted'
|
|
|
|
continue
|
|
|
|
|
2010-07-15 23:52:42 +00:00
|
|
|
branch.UploadForReview(people, auto_topic=opt.auto_topic)
|
2008-10-21 14:00:00 +00:00
|
|
|
branch.uploaded = True
|
|
|
|
except UploadError, e:
|
|
|
|
branch.error = e
|
|
|
|
branch.uploaded = False
|
|
|
|
have_errors = True
|
|
|
|
|
|
|
|
print >>sys.stderr, ''
|
2009-08-23 01:39:49 +00:00
|
|
|
print >>sys.stderr, '----------------------------------------------------------------------'
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
if have_errors:
|
|
|
|
for branch in todo:
|
|
|
|
if not branch.uploaded:
|
2009-08-23 01:39:49 +00:00
|
|
|
if len(str(branch.error)) <= 30:
|
|
|
|
fmt = ' (%s)'
|
|
|
|
else:
|
|
|
|
fmt = '\n (%s)'
|
|
|
|
print >>sys.stderr, ('[FAILED] %-15s %-15s' + fmt) % (
|
2008-10-21 14:00:00 +00:00
|
|
|
branch.project.relpath + '/', \
|
|
|
|
branch.name, \
|
2009-08-23 01:39:49 +00:00
|
|
|
str(branch.error))
|
2008-10-21 14:00:00 +00:00
|
|
|
print >>sys.stderr, ''
|
|
|
|
|
|
|
|
for branch in todo:
|
|
|
|
if branch.uploaded:
|
|
|
|
print >>sys.stderr, '[OK ] %-15s %s' % (
|
|
|
|
branch.project.relpath + '/',
|
|
|
|
branch.name)
|
|
|
|
|
|
|
|
if have_errors:
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def Execute(self, opt, args):
|
|
|
|
project_list = self.GetProjects(args)
|
|
|
|
pending = []
|
2008-11-17 21:56:36 +00:00
|
|
|
reviewers = []
|
|
|
|
cc = []
|
|
|
|
|
|
|
|
if opt.reviewers:
|
|
|
|
reviewers = _SplitEmails(opt.reviewers)
|
|
|
|
if opt.cc:
|
|
|
|
cc = _SplitEmails(opt.cc)
|
|
|
|
people = (reviewers,cc)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
for project in project_list:
|
|
|
|
avail = project.GetUploadableBranches()
|
|
|
|
if avail:
|
|
|
|
pending.append((project, avail))
|
|
|
|
|
|
|
|
if not pending:
|
|
|
|
print >>sys.stdout, "no branches ready for upload"
|
|
|
|
elif len(pending) == 1 and len(pending[0][1]) == 1:
|
2010-07-15 23:52:42 +00:00
|
|
|
self._SingleBranch(opt, pending[0][1][0], people)
|
2008-10-21 14:00:00 +00:00
|
|
|
else:
|
2010-07-15 23:52:42 +00:00
|
|
|
self._MultipleBranches(opt, pending, people)
|