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
|
2021-05-01 16:02:01 +00:00
|
|
|
import functools
|
2021-05-03 04:51:52 +00:00
|
|
|
import optparse
|
2008-10-21 14:00:00 +00:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2021-05-01 16:02:01 +00:00
|
|
|
from command import DEFAULT_LOCAL_JOBS, InteractiveCommand
|
2008-10-21 14:00:00 +00:00
|
|
|
from editor import Editor
|
2020-08-01 16:36:44 +00:00
|
|
|
from error import UploadError
|
2013-09-30 22:54:38 +00:00
|
|
|
from git_command import GitCommand
|
2020-05-18 15:15:51 +00:00
|
|
|
from git_refs import R_HEADS
|
2020-09-10 08:38:04 +00:00
|
|
|
from hooks import RepoHook
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2013-03-01 13:44:38 +00:00
|
|
|
|
2010-05-05 15:18:35 +00:00
|
|
|
UNUSUAL_COMMIT_THRESHOLD = 5
|
2010-05-04 23:56:07 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2010-05-04 23:56:07 +00:00
|
|
|
def _ConfirmManyUploads(multiple_branches=False):
|
|
|
|
if multiple_branches:
|
2013-03-05 08:26:46 +00:00
|
|
|
print('ATTENTION: One or more branches has an unusually high number '
|
2012-11-02 05:59:27 +00:00
|
|
|
'of commits.')
|
2010-05-04 23:56:07 +00:00
|
|
|
else:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('ATTENTION: You are uploading an unusually high number of commits.')
|
2013-03-05 08:26:46 +00:00
|
|
|
print('YOU PROBABLY DO NOT MEAN TO DO THIS. (Did you rebase across '
|
2012-11-02 05:59:27 +00:00
|
|
|
'branches?)')
|
2013-03-01 13:44:38 +00:00
|
|
|
answer = input("If you are sure you intend to do this, type 'yes': ").strip()
|
2010-05-04 23:56:07 +00:00
|
|
|
return answer == "yes"
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _die(fmt, *args):
|
|
|
|
msg = fmt % args
|
2012-11-02 05:59:27 +00:00
|
|
|
print('error: %s' % msg, file=sys.stderr)
|
2008-10-21 14:00:00 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-11-17 21:56:36 +00:00
|
|
|
def _SplitEmails(values):
|
|
|
|
result = []
|
2012-09-24 03:15:13 +00:00
|
|
|
for value in values:
|
|
|
|
result.extend([s.strip() for s in value.split(',')])
|
2008-11-17 21:56:36 +00:00
|
|
|
return result
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
class Upload(InteractiveCommand):
|
|
|
|
common = True
|
|
|
|
helpSummary = "Upload changes for code review"
|
2012-11-14 03:09:38 +00:00
|
|
|
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
|
|
|
|
2018-10-10 05:05:11 +00:00
|
|
|
# Configuration
|
2009-04-17 19:11:24 +00:00
|
|
|
|
|
|
|
review.URL.autoupload:
|
|
|
|
|
2011-08-11 19:46:43 +00:00
|
|
|
To disable the "Upload ... (y/N)?" prompt, you can set a per-project
|
2009-04-17 19:11:24 +00:00
|
|
|
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.
|
|
|
|
|
2013-11-28 01:19:22 +00:00
|
|
|
review.URL.autoreviewer:
|
|
|
|
|
|
|
|
To automatically append a user or mailing list to reviews, you can set
|
|
|
|
a per-project or global Git option to do so.
|
|
|
|
|
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
|
|
|
|
2012-06-01 04:48:22 +00:00
|
|
|
review.URL.uploadtopic:
|
|
|
|
|
|
|
|
To add a topic branch whenever uploading a commit, you can set a
|
|
|
|
per-project or global Git option to do so. If review.URL.uploadtopic
|
|
|
|
is set to "true" then repo will assume you always want the equivalent
|
|
|
|
of the -t option to the repo command. If unset or set to "false" then
|
|
|
|
repo will make use of only the command line option.
|
|
|
|
|
2020-02-19 07:22:22 +00:00
|
|
|
review.URL.uploadhashtags:
|
|
|
|
|
|
|
|
To add hashtags whenever uploading a commit, you can set a per-project
|
|
|
|
or global Git option to do so. The value of review.URL.uploadhashtags
|
2020-02-24 20:38:07 +00:00
|
|
|
will be used as comma delimited hashtags like the --hashtag option.
|
|
|
|
|
|
|
|
review.URL.uploadlabels:
|
|
|
|
|
|
|
|
To add labels whenever uploading a commit, you can set a per-project
|
|
|
|
or global Git option to do so. The value of review.URL.uploadlabels
|
|
|
|
will be used as comma delimited labels like the --label option.
|
2020-02-19 07:22:22 +00:00
|
|
|
|
2020-03-14 21:39:03 +00:00
|
|
|
review.URL.uploadnotify:
|
|
|
|
|
|
|
|
Control e-mail notifications when uploading.
|
|
|
|
https://gerrit-review.googlesource.com/Documentation/user-upload.html#notify
|
|
|
|
|
2018-10-10 05:05:11 +00:00
|
|
|
# References
|
2009-04-18 17:59:33 +00:00
|
|
|
|
2018-10-10 04:57:44 +00:00
|
|
|
Gerrit Code Review: https://www.gerritcodereview.com/
|
2009-04-18 17:59:33 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
2021-05-01 16:02:01 +00:00
|
|
|
PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
|
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')
|
2020-02-19 07:22:22 +00:00
|
|
|
p.add_option('--hashtag', '--ht',
|
|
|
|
dest='hashtags', action='append', default=[],
|
|
|
|
help='Add hashtags (comma delimited) to the review.')
|
2020-02-19 08:00:26 +00:00
|
|
|
p.add_option('--hashtag-branch', '--htb',
|
|
|
|
action='store_true',
|
|
|
|
help='Add local branch name as a hashtag.')
|
2020-02-24 20:38:07 +00:00
|
|
|
p.add_option('-l', '--label',
|
|
|
|
dest='labels', action='append', default=[],
|
|
|
|
help='Add a label when uploading.')
|
2008-11-17 21:56:36 +00:00
|
|
|
p.add_option('--re', '--reviewers',
|
2020-02-12 04:52:31 +00:00
|
|
|
type='string', action='append', dest='reviewers',
|
2008-11-17 21:56:36 +00:00
|
|
|
help='Request reviews from these people.')
|
|
|
|
p.add_option('--cc',
|
2020-02-12 04:52:31 +00:00
|
|
|
type='string', action='append', dest='cc',
|
2008-11-17 21:56:36 +00:00
|
|
|
help='Also send email to these email addresses.')
|
2021-05-01 16:02:01 +00:00
|
|
|
p.add_option('--br', '--branch',
|
2020-02-12 04:52:31 +00:00
|
|
|
type='string', action='store', dest='branch',
|
2021-05-01 16:02:01 +00:00
|
|
|
help='(Local) branch to upload.')
|
2021-05-03 04:51:52 +00:00
|
|
|
p.add_option('-c', '--current-branch',
|
2012-04-06 14:39:32 +00:00
|
|
|
dest='current_branch', action='store_true',
|
|
|
|
help='Upload current git branch.')
|
2021-05-03 05:10:09 +00:00
|
|
|
p.add_option('--no-current-branch',
|
|
|
|
dest='current_branch', action='store_false',
|
|
|
|
help='Upload all git branches.')
|
2021-05-03 04:51:52 +00:00
|
|
|
# Turn this into a warning & remove this someday.
|
|
|
|
p.add_option('--cbr',
|
|
|
|
dest='current_branch', action='store_true',
|
|
|
|
help=optparse.SUPPRESS_HELP)
|
2018-10-31 20:48:01 +00:00
|
|
|
p.add_option('--ne', '--no-emails',
|
|
|
|
action='store_false', dest='notify', default=True,
|
|
|
|
help='If specified, do not send emails on upload.')
|
2017-08-02 14:55:03 +00:00
|
|
|
p.add_option('-p', '--private',
|
|
|
|
action='store_true', dest='private', default=False,
|
|
|
|
help='If specified, upload as a private change.')
|
|
|
|
p.add_option('-w', '--wip',
|
|
|
|
action='store_true', dest='wip', default=False,
|
|
|
|
help='If specified, upload as a work-in-progress change.')
|
2017-11-13 18:48:34 +00:00
|
|
|
p.add_option('-o', '--push-option',
|
|
|
|
type='string', action='append', dest='push_options',
|
|
|
|
default=[],
|
|
|
|
help='Additional push options to transmit')
|
2013-05-06 17:36:24 +00:00
|
|
|
p.add_option('-D', '--destination', '--dest',
|
|
|
|
type='string', action='store', dest='dest_branch',
|
|
|
|
metavar='BRANCH',
|
|
|
|
help='Submit for review on this target branch.')
|
2020-02-19 07:27:22 +00:00
|
|
|
p.add_option('-n', '--dry-run',
|
|
|
|
dest='dryrun', default=False, action='store_true',
|
|
|
|
help='Do everything except actually upload the CL.')
|
2020-02-19 07:32:52 +00:00
|
|
|
p.add_option('-y', '--yes',
|
|
|
|
default=False, action='store_true',
|
|
|
|
help='Answer yes to all safe prompts.')
|
2020-02-11 10:17:16 +00:00
|
|
|
p.add_option('--no-cert-checks',
|
|
|
|
dest='validate_certs', action='store_false', default=True,
|
|
|
|
help='Disable verifying ssl certs (unsafe).')
|
2020-08-01 16:36:44 +00:00
|
|
|
RepoHook.AddOptionGroup(p, 'pre-upload')
|
Support repo-level pre-upload hook and prep for future hooks.
All repo-level hooks are expected to live in a single project at the
top level of that project. The name of the hooks project is provided
in the manifest.xml. The manifest also lists which hooks are enabled
to make it obvious if a file somehow failed to sync down (or got
deleted).
Before running any hook, we will prompt the user to make sure that it
is OK. A user can deny running the hook, allow once, or allow
"forever" (until hooks change). This tries to keep with the git
spirit of not automatically running anything on the user's computer
that got synced down. Note that individual repo commands can add
always options to avoid these prompts as they see fit (see below for
the 'upload' options).
When hooks are run, they are loaded into the current interpreter (the
one running repo) and their main() function is run. This mechanism is
used (instead of using subprocess) to make it easier to expand to a
richer hook interface in the future. During loading, the
interpreter's sys.path is updated to contain the directory containing
the hooks so that hooks can be split into multiple files.
The upload command has two options that control hook behavior:
- no-verify=False, verify=False (DEFAULT):
If stdout is a tty, can prompt about running upload hooks if needed.
If user denies running hooks, the upload is cancelled. If stdout is
not a tty and we would need to prompt about upload hooks, upload is
cancelled.
- no-verify=False, verify=True:
Always run upload hooks with no prompt.
- no-verify=True, verify=False:
Never run upload hooks, but upload anyway (AKA bypass hooks).
- no-verify=True, verify=True:
Invalid
Sample bit of manifest.xml code for enabling hooks (assumes you have a
project named 'hooks' where hooks are stored):
<repo-hooks in-project="hooks" enabled-list="pre-upload" />
Sample main() function in pre-upload.py in hooks directory:
def main(project_list, **kwargs):
print ('These projects will be uploaded: %s' %
', '.join(project_list))
print ('I am being a good boy and ignoring anything in kwargs\n'
'that I don\'t understand.')
print 'I fail 50% of the time. How flaky.'
if random.random() <= .5:
raise Exception('Pre-upload hook failed. Have a nice day.')
Change-Id: I5cefa2cd5865c72589263cf8e2f152a43c122f70
2011-03-04 19:54:18 +00:00
|
|
|
|
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
|
2012-09-24 03:15:13 +00:00
|
|
|
commit_list = branch.commits
|
2009-04-18 01:47:22 +00:00
|
|
|
|
2013-06-24 08:32:12 +00:00
|
|
|
destination = opt.dest_branch or project.dest_branch or project.revisionExpr
|
2017-07-10 08:31:24 +00:00
|
|
|
print('Upload project %s/ to remote branch %s%s:' %
|
2020-02-25 07:58:04 +00:00
|
|
|
(project.relpath, destination, ' (private)' if opt.private else ''))
|
2012-11-02 05:59:27 +00:00
|
|
|
print(' branch %s (%2d commit%s, %s):' % (
|
2020-02-12 05:58:39 +00:00
|
|
|
name,
|
|
|
|
len(commit_list),
|
|
|
|
len(commit_list) != 1 and 's' or '',
|
|
|
|
date))
|
2012-09-24 03:15:13 +00:00
|
|
|
for commit in commit_list:
|
2012-11-02 05:59:27 +00:00
|
|
|
print(' %s' % commit)
|
2009-04-17 19:11:24 +00:00
|
|
|
|
2019-07-04 21:35:11 +00:00
|
|
|
print('to %s (y/N)? ' % remote.review, end='')
|
|
|
|
# TODO: When we require Python 3, use flush=True w/print above.
|
|
|
|
sys.stdout.flush()
|
2020-02-19 07:32:52 +00:00
|
|
|
if opt.yes:
|
|
|
|
print('<--yes>')
|
|
|
|
answer = True
|
|
|
|
else:
|
|
|
|
answer = sys.stdin.readline().strip().lower()
|
|
|
|
answer = answer in ('y', 'yes', '1', 'true', 't')
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2010-05-04 23:56:07 +00:00
|
|
|
if answer:
|
|
|
|
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:
|
2013-05-31 19:28:05 +00:00
|
|
|
if branch is None:
|
|
|
|
continue
|
2008-10-21 14:00:00 +00:00
|
|
|
name = branch.name
|
|
|
|
date = branch.date
|
2012-09-24 03:15:13 +00:00
|
|
|
commit_list = branch.commits
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
if b:
|
|
|
|
script.append('#')
|
2013-05-31 19:45:28 +00:00
|
|
|
destination = opt.dest_branch or project.dest_branch or project.revisionExpr
|
2011-04-28 12:13:14 +00:00
|
|
|
script.append('# branch %s (%2d commit%s, %s) to remote branch %s:' % (
|
2008-10-21 14:00:00 +00:00
|
|
|
name,
|
2012-09-24 03:15:13 +00:00
|
|
|
len(commit_list),
|
|
|
|
len(commit_list) != 1 and 's' or '',
|
2011-04-28 12:13:14 +00:00
|
|
|
date,
|
2013-05-31 19:45:28 +00:00
|
|
|
destination))
|
2012-09-24 03:15:13 +00:00
|
|
|
for commit in commit_list:
|
2008-10-21 14:00:00 +00:00
|
|
|
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
|
|
|
|
2013-11-28 01:19:22 +00:00
|
|
|
def _AppendAutoList(self, branch, people):
|
2010-07-15 23:03:02 +00:00
|
|
|
"""
|
2013-11-28 01:19:22 +00:00
|
|
|
Appends the list of reviewers in the git project's config.
|
2010-07-15 23:03:02 +00:00
|
|
|
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
|
2013-11-28 01:19:22 +00:00
|
|
|
|
|
|
|
key = 'review.%s.autoreviewer' % project.GetBranch(name).remote.review
|
|
|
|
raw_list = project.config.GetString(key)
|
2020-02-12 02:20:36 +00:00
|
|
|
if raw_list is not None:
|
2013-11-28 01:19:22 +00:00
|
|
|
people[0].extend([entry.strip() for entry in raw_list.split(',')])
|
|
|
|
|
2010-07-15 23:03:02 +00:00
|
|
|
key = 'review.%s.autocopy' % project.GetBranch(name).remote.review
|
|
|
|
raw_list = project.config.GetString(key)
|
2020-02-12 02:20:36 +00:00
|
|
|
if raw_list is not None and len(people[0]) > 0:
|
2010-07-15 23:03:02 +00:00
|
|
|
people[1].extend([entry.strip() for entry in raw_list.split(',')])
|
|
|
|
|
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]
|
2012-10-25 03:23:11 +00:00
|
|
|
except (AttributeError, IndexError):
|
2009-05-04 19:45:11 +00:00
|
|
|
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)
|
2013-11-28 01:19:22 +00:00
|
|
|
self._AppendAutoList(branch, people)
|
2010-07-15 23:03:02 +00:00
|
|
|
|
2010-04-08 15:28:59 +00:00
|
|
|
# Check if there are local changes that may have been forgotten
|
2014-10-05 22:40:30 +00:00
|
|
|
changes = branch.project.UncommitedFiles()
|
|
|
|
if changes:
|
2012-11-14 02:36:51 +00:00
|
|
|
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:
|
2019-07-04 21:35:11 +00:00
|
|
|
print()
|
|
|
|
print('Uncommitted changes in %s (did you forget to amend?):'
|
|
|
|
% branch.project.name)
|
|
|
|
print('\n'.join(changes))
|
|
|
|
print('Continue uploading? (y/N) ', end='')
|
|
|
|
# TODO: When we require Python 3, use flush=True w/print above.
|
|
|
|
sys.stdout.flush()
|
2020-02-19 07:32:52 +00:00
|
|
|
if opt.yes:
|
|
|
|
print('<--yes>')
|
|
|
|
a = 'yes'
|
|
|
|
else:
|
|
|
|
a = sys.stdin.readline().strip().lower()
|
2012-11-14 02:36:51 +00:00
|
|
|
if a not in ('y', 'yes', 't', 'true', 'on'):
|
|
|
|
print("skipping upload", file=sys.stderr)
|
|
|
|
branch.uploaded = False
|
|
|
|
branch.error = 'User aborted'
|
|
|
|
continue
|
2010-04-08 15:28:59 +00:00
|
|
|
|
2012-06-01 04:48:22 +00:00
|
|
|
# Check if topic branches should be sent to the server during upload
|
|
|
|
if opt.auto_topic is not True:
|
2012-11-14 02:36:51 +00:00
|
|
|
key = 'review.%s.uploadtopic' % branch.project.remote.review
|
|
|
|
opt.auto_topic = branch.project.config.GetBoolean(key)
|
2012-06-01 04:48:22 +00:00
|
|
|
|
2020-02-24 20:38:07 +00:00
|
|
|
def _ExpandCommaList(value):
|
|
|
|
"""Split |value| up into comma delimited entries."""
|
2020-02-19 07:22:22 +00:00
|
|
|
if not value:
|
|
|
|
return
|
2020-02-24 20:38:07 +00:00
|
|
|
for ret in value.split(','):
|
|
|
|
ret = ret.strip()
|
|
|
|
if ret:
|
|
|
|
yield ret
|
|
|
|
|
|
|
|
# Check if hashtags should be included.
|
2020-02-19 07:22:22 +00:00
|
|
|
key = 'review.%s.uploadhashtags' % branch.project.remote.review
|
2020-02-24 20:38:07 +00:00
|
|
|
hashtags = set(_ExpandCommaList(branch.project.config.GetString(key)))
|
2020-02-19 07:22:22 +00:00
|
|
|
for tag in opt.hashtags:
|
2020-02-24 20:38:07 +00:00
|
|
|
hashtags.update(_ExpandCommaList(tag))
|
2020-02-19 08:00:26 +00:00
|
|
|
if opt.hashtag_branch:
|
|
|
|
hashtags.add(branch.name)
|
2020-02-19 07:22:22 +00:00
|
|
|
|
2020-02-24 20:38:07 +00:00
|
|
|
# Check if labels should be included.
|
|
|
|
key = 'review.%s.uploadlabels' % branch.project.remote.review
|
|
|
|
labels = set(_ExpandCommaList(branch.project.config.GetString(key)))
|
|
|
|
for label in opt.labels:
|
|
|
|
labels.update(_ExpandCommaList(label))
|
|
|
|
# Basic sanity check on label syntax.
|
|
|
|
for label in labels:
|
|
|
|
if not re.match(r'^.+[+-][0-9]+$', label):
|
|
|
|
print('repo: error: invalid label syntax "%s": labels use forms '
|
|
|
|
'like CodeReview+1 or Verified-1' % (label,), file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-03-14 21:39:03 +00:00
|
|
|
# Handle e-mail notifications.
|
|
|
|
if opt.notify is False:
|
|
|
|
notify = 'NONE'
|
|
|
|
else:
|
|
|
|
key = 'review.%s.uploadnotify' % branch.project.remote.review
|
|
|
|
notify = branch.project.config.GetString(key)
|
|
|
|
|
2013-10-09 06:10:52 +00:00
|
|
|
destination = opt.dest_branch or branch.project.dest_branch
|
2013-09-30 22:54:38 +00:00
|
|
|
|
|
|
|
# Make sure our local branch is not setup to track a different remote branch
|
|
|
|
merge_branch = self._GetMergeBranch(branch.project)
|
2013-10-15 19:59:00 +00:00
|
|
|
if destination:
|
2020-05-18 15:15:51 +00:00
|
|
|
full_dest = destination
|
|
|
|
if not full_dest.startswith(R_HEADS):
|
|
|
|
full_dest = R_HEADS + full_dest
|
|
|
|
|
2013-10-15 19:59:00 +00:00
|
|
|
if not opt.dest_branch and merge_branch and merge_branch != full_dest:
|
|
|
|
print('merge branch %s does not match destination branch %s'
|
|
|
|
% (merge_branch, full_dest))
|
|
|
|
print('skipping upload.')
|
|
|
|
print('Please use `--destination %s` if this is intentional'
|
|
|
|
% destination)
|
|
|
|
branch.uploaded = False
|
|
|
|
continue
|
2013-09-30 22:54:38 +00:00
|
|
|
|
2017-08-02 14:55:03 +00:00
|
|
|
branch.UploadForReview(people,
|
2020-02-19 07:27:22 +00:00
|
|
|
dryrun=opt.dryrun,
|
2017-08-02 14:55:03 +00:00
|
|
|
auto_topic=opt.auto_topic,
|
2020-02-19 07:22:22 +00:00
|
|
|
hashtags=hashtags,
|
2020-02-24 20:38:07 +00:00
|
|
|
labels=labels,
|
2017-08-02 14:55:03 +00:00
|
|
|
private=opt.private,
|
2020-03-14 21:39:03 +00:00
|
|
|
notify=notify,
|
2017-08-02 14:55:03 +00:00
|
|
|
wip=opt.wip,
|
2017-08-08 08:18:11 +00:00
|
|
|
dest_branch=destination,
|
2017-11-13 18:48:34 +00:00
|
|
|
validate_certs=opt.validate_certs,
|
|
|
|
push_options=opt.push_options)
|
2017-08-08 08:18:11 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
branch.uploaded = True
|
2012-09-09 22:37:57 +00:00
|
|
|
except UploadError as e:
|
2008-10-21 14:00:00 +00:00
|
|
|
branch.error = e
|
|
|
|
branch.uploaded = False
|
|
|
|
have_errors = True
|
|
|
|
|
2012-11-02 05:59:27 +00:00
|
|
|
print(file=sys.stderr)
|
|
|
|
print('----------------------------------------------------------------------', file=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)'
|
2012-11-02 05:59:27 +00:00
|
|
|
print(('[FAILED] %-15s %-15s' + fmt) % (
|
2020-02-12 05:58:39 +00:00
|
|
|
branch.project.relpath + '/',
|
|
|
|
branch.name,
|
|
|
|
str(branch.error)),
|
|
|
|
file=sys.stderr)
|
2012-11-02 05:59:27 +00:00
|
|
|
print()
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
for branch in todo:
|
2012-11-14 02:36:51 +00:00
|
|
|
if branch.uploaded:
|
|
|
|
print('[OK ] %-15s %s' % (
|
2020-02-12 05:58:39 +00:00
|
|
|
branch.project.relpath + '/',
|
|
|
|
branch.name),
|
|
|
|
file=sys.stderr)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
if have_errors:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-09-30 22:54:38 +00:00
|
|
|
def _GetMergeBranch(self, project):
|
|
|
|
p = GitCommand(project,
|
|
|
|
['rev-parse', '--abbrev-ref', 'HEAD'],
|
2020-02-12 04:56:59 +00:00
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True)
|
2013-09-30 22:54:38 +00:00
|
|
|
p.Wait()
|
|
|
|
local_branch = p.stdout.strip()
|
|
|
|
p = GitCommand(project,
|
|
|
|
['config', '--get', 'branch.%s.merge' % local_branch],
|
2020-02-12 04:56:59 +00:00
|
|
|
capture_stdout=True,
|
|
|
|
capture_stderr=True)
|
2013-09-30 22:54:38 +00:00
|
|
|
p.Wait()
|
|
|
|
merge_branch = p.stdout.strip()
|
|
|
|
return merge_branch
|
|
|
|
|
2021-05-01 16:02:01 +00:00
|
|
|
@staticmethod
|
|
|
|
def _GatherOne(opt, project):
|
|
|
|
"""Figure out the upload status for |project|."""
|
|
|
|
if opt.current_branch:
|
|
|
|
cbr = project.CurrentBranch
|
|
|
|
up_branch = project.GetUploadableBranch(cbr)
|
|
|
|
avail = [up_branch] if up_branch else None
|
|
|
|
else:
|
|
|
|
avail = project.GetUploadableBranches(opt.branch)
|
|
|
|
return (project, avail)
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def Execute(self, opt, args):
|
2021-05-01 16:02:01 +00:00
|
|
|
projects = self.GetProjects(args)
|
|
|
|
|
|
|
|
def _ProcessResults(_pool, _out, results):
|
|
|
|
pending = []
|
|
|
|
for result in results:
|
|
|
|
project, avail = result
|
|
|
|
if avail is None:
|
2021-05-01 04:45:19 +00:00
|
|
|
print('repo: error: %s: Unable to upload branch "%s". '
|
2020-11-10 06:09:51 +00:00
|
|
|
'You might be able to fix the branch by running:\n'
|
|
|
|
' git branch --set-upstream-to m/%s' %
|
2021-05-01 16:02:01 +00:00
|
|
|
(project.relpath, project.CurrentBranch, self.manifest.branch),
|
2013-11-28 00:20:57 +00:00
|
|
|
file=sys.stderr)
|
2021-05-01 16:02:01 +00:00
|
|
|
elif avail:
|
|
|
|
pending.append(result)
|
|
|
|
return pending
|
|
|
|
|
|
|
|
pending = self.ExecuteInParallel(
|
|
|
|
opt.jobs,
|
|
|
|
functools.partial(self._GatherOne, opt),
|
|
|
|
projects,
|
|
|
|
callback=_ProcessResults)
|
Support repo-level pre-upload hook and prep for future hooks.
All repo-level hooks are expected to live in a single project at the
top level of that project. The name of the hooks project is provided
in the manifest.xml. The manifest also lists which hooks are enabled
to make it obvious if a file somehow failed to sync down (or got
deleted).
Before running any hook, we will prompt the user to make sure that it
is OK. A user can deny running the hook, allow once, or allow
"forever" (until hooks change). This tries to keep with the git
spirit of not automatically running anything on the user's computer
that got synced down. Note that individual repo commands can add
always options to avoid these prompts as they see fit (see below for
the 'upload' options).
When hooks are run, they are loaded into the current interpreter (the
one running repo) and their main() function is run. This mechanism is
used (instead of using subprocess) to make it easier to expand to a
richer hook interface in the future. During loading, the
interpreter's sys.path is updated to contain the directory containing
the hooks so that hooks can be split into multiple files.
The upload command has two options that control hook behavior:
- no-verify=False, verify=False (DEFAULT):
If stdout is a tty, can prompt about running upload hooks if needed.
If user denies running hooks, the upload is cancelled. If stdout is
not a tty and we would need to prompt about upload hooks, upload is
cancelled.
- no-verify=False, verify=True:
Always run upload hooks with no prompt.
- no-verify=True, verify=False:
Never run upload hooks, but upload anyway (AKA bypass hooks).
- no-verify=True, verify=True:
Invalid
Sample bit of manifest.xml code for enabling hooks (assumes you have a
project named 'hooks' where hooks are stored):
<repo-hooks in-project="hooks" enabled-list="pre-upload" />
Sample main() function in pre-upload.py in hooks directory:
def main(project_list, **kwargs):
print ('These projects will be uploaded: %s' %
', '.join(project_list))
print ('I am being a good boy and ignoring anything in kwargs\n'
'that I don\'t understand.')
print 'I fail 50% of the time. How flaky.'
if random.random() <= .5:
raise Exception('Pre-upload hook failed. Have a nice day.')
Change-Id: I5cefa2cd5865c72589263cf8e2f152a43c122f70
2011-03-04 19:54:18 +00:00
|
|
|
|
2016-04-04 21:31:32 +00:00
|
|
|
if not pending:
|
2021-05-01 16:02:01 +00:00
|
|
|
if opt.branch is None:
|
2020-02-19 05:14:32 +00:00
|
|
|
print('repo: error: no branches ready for upload', file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print('repo: error: no branches named "%s" ready for upload' %
|
2021-05-01 16:02:01 +00:00
|
|
|
(opt.branch,), file=sys.stderr)
|
2020-02-19 05:14:32 +00:00
|
|
|
return 1
|
2016-04-04 21:31:32 +00:00
|
|
|
|
2020-08-01 16:36:44 +00:00
|
|
|
pending_proj_names = [project.name for (project, available) in pending]
|
|
|
|
pending_worktrees = [project.worktree for (project, available) in pending]
|
|
|
|
hook = RepoHook.FromSubcmd(
|
|
|
|
hook_type='pre-upload', manifest=self.manifest,
|
|
|
|
opt=opt, abort_if_user_denies=True)
|
|
|
|
if not hook.Run(
|
|
|
|
project_list=pending_proj_names,
|
|
|
|
worktree_list=pending_worktrees):
|
|
|
|
return 1
|
Support repo-level pre-upload hook and prep for future hooks.
All repo-level hooks are expected to live in a single project at the
top level of that project. The name of the hooks project is provided
in the manifest.xml. The manifest also lists which hooks are enabled
to make it obvious if a file somehow failed to sync down (or got
deleted).
Before running any hook, we will prompt the user to make sure that it
is OK. A user can deny running the hook, allow once, or allow
"forever" (until hooks change). This tries to keep with the git
spirit of not automatically running anything on the user's computer
that got synced down. Note that individual repo commands can add
always options to avoid these prompts as they see fit (see below for
the 'upload' options).
When hooks are run, they are loaded into the current interpreter (the
one running repo) and their main() function is run. This mechanism is
used (instead of using subprocess) to make it easier to expand to a
richer hook interface in the future. During loading, the
interpreter's sys.path is updated to contain the directory containing
the hooks so that hooks can be split into multiple files.
The upload command has two options that control hook behavior:
- no-verify=False, verify=False (DEFAULT):
If stdout is a tty, can prompt about running upload hooks if needed.
If user denies running hooks, the upload is cancelled. If stdout is
not a tty and we would need to prompt about upload hooks, upload is
cancelled.
- no-verify=False, verify=True:
Always run upload hooks with no prompt.
- no-verify=True, verify=False:
Never run upload hooks, but upload anyway (AKA bypass hooks).
- no-verify=True, verify=True:
Invalid
Sample bit of manifest.xml code for enabling hooks (assumes you have a
project named 'hooks' where hooks are stored):
<repo-hooks in-project="hooks" enabled-list="pre-upload" />
Sample main() function in pre-upload.py in hooks directory:
def main(project_list, **kwargs):
print ('These projects will be uploaded: %s' %
', '.join(project_list))
print ('I am being a good boy and ignoring anything in kwargs\n'
'that I don\'t understand.')
print 'I fail 50% of the time. How flaky.'
if random.random() <= .5:
raise Exception('Pre-upload hook failed. Have a nice day.')
Change-Id: I5cefa2cd5865c72589263cf8e2f152a43c122f70
2011-03-04 19:54:18 +00:00
|
|
|
|
2021-05-01 16:02:01 +00:00
|
|
|
reviewers = _SplitEmails(opt.reviewers) if opt.reviewers else []
|
|
|
|
cc = _SplitEmails(opt.cc) if opt.cc else []
|
2012-11-14 03:09:38 +00:00
|
|
|
people = (reviewers, cc)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2016-04-04 21:31:32 +00:00
|
|
|
if 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)
|