Reland "Add --push-options in git cl upload"

This is a reland of e2c65d7b75

Original change's description:
> Add --push-options in git cl upload
>
> --push-option parameter is passed to git push as is.
>
> R=ehmaldonado@google.com
>
> Bug: 1184393
> Change-Id: Id1f7da1f0c8e8a23144b547d50d817fe8d4efeb1
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2786080
> Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
> Commit-Queue: Josip Sokcevic <sokcevic@google.com>

R=gavinmak@google.com

Bug: 1184393
Change-Id: I9302a2e908b931a963a63ed2bb3778e21c4d6e53
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2794787
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
changes/87/2794787/3
Josip Sokcevic 4 years ago committed by LUCI CQ
parent 5260b2b204
commit f2cfd3dac0

@ -2166,7 +2166,11 @@ class Changelist(object):
gclient_utils.rmtree(git_info_dir) gclient_utils.rmtree(git_info_dir)
def _RunGitPushWithTraces(self, refspec, refspec_opts, git_push_metadata): def _RunGitPushWithTraces(self,
refspec,
refspec_opts,
git_push_metadata,
git_push_options=None):
"""Run git push and collect the traces resulting from the execution.""" """Run git push and collect the traces resulting from the execution."""
# Create a temporary directory to store traces in. Traces will be compressed # Create a temporary directory to store traces in. Traces will be compressed
# and stored in a 'traces' dir inside depot_tools. # and stored in a 'traces' dir inside depot_tools.
@ -2186,8 +2190,13 @@ class Changelist(object):
push_returncode = 0 push_returncode = 0
remote_url = self.GetRemoteUrl() remote_url = self.GetRemoteUrl()
before_push = time_time() before_push = time_time()
push_cmd = ['git', 'push', remote_url, refspec]
if git_push_options:
for opt in git_push_options:
push_cmd.extend(['-o', opt])
push_stdout = gclient_utils.CheckCallAndFilter( push_stdout = gclient_utils.CheckCallAndFilter(
['git', 'push', remote_url, refspec], push_cmd,
env=env, env=env,
print_stdout=True, print_stdout=True,
# Flush after every line: useful for seeing progress when running as # Flush after every line: useful for seeing progress when running as
@ -2423,8 +2432,10 @@ class Changelist(object):
'change_id': change_id, 'change_id': change_id,
'description': change_desc.description, 'description': change_desc.description,
} }
push_stdout = self._RunGitPushWithTraces(refspec, refspec_opts, push_stdout = self._RunGitPushWithTraces(refspec, refspec_opts,
git_push_metadata) git_push_metadata,
options.push_options)
if options.squash: if options.squash:
regex = re.compile(r'remote:\s+https?://[\w\-\.\+\/#]*/(\d+)\s.*') regex = re.compile(r'remote:\s+https?://[\w\-\.\+\/#]*/(\d+)\s.*')
@ -4192,6 +4203,13 @@ def CMDupload(parser, args):
help='Run presubmit checks in the ResultSink environment ' help='Run presubmit checks in the ResultSink environment '
'and send results to the ResultDB database.') 'and send results to the ResultDB database.')
parser.add_option('--realm', help='LUCI realm if reporting to ResultDB') parser.add_option('--realm', help='LUCI realm if reporting to ResultDB')
parser.add_option('-o',
'--push-options',
action='append',
default=[],
help='Transmit the given string to the server when '
'performing git push (pass-through). See git-push '
'documentation for more details.')
orig_args = args orig_args = args
(options, args) = parser.parse_args(args) (options, args) = parser.parse_args(args)

@ -808,16 +808,27 @@ class TestGitCl(unittest.TestCase):
return calls return calls
def _gerrit_upload_calls(self, description, reviewers, squash, def _gerrit_upload_calls(self,
description,
reviewers,
squash,
squash_mode='default', squash_mode='default',
title=None, notify=False, title=None,
post_amend_description=None, issue=None, cc=None, notify=False,
custom_cl_base=None, tbr=None, post_amend_description=None,
issue=None,
cc=None,
custom_cl_base=None,
tbr=None,
short_hostname='chromium', short_hostname='chromium',
labels=None, change_id=None, labels=None,
final_description=None, gitcookies_exists=True, change_id=None,
force=False, edit_description=None, final_description=None,
default_branch='master'): gitcookies_exists=True,
force=False,
edit_description=None,
default_branch='master',
push_opts=None):
if post_amend_description is None: if post_amend_description is None:
post_amend_description = description post_amend_description = description
cc = cc or [] cc = cc or []
@ -952,35 +963,47 @@ class TestGitCl(unittest.TestCase):
] ]
calls += [ calls += [
(('time.time',), 1000,), (
((['git', 'push', ('time.time', ),
'https://%s.googlesource.com/my/repo' % short_hostname, 1000,
ref_to_push + ':refs/for/refs/heads/' + default_branch + ref_suffix],), ),
(('remote:\n' (
'remote: Processing changes: (\)\n' ([
'remote: Processing changes: (|)\n' 'git', 'push',
'remote: Processing changes: (/)\n' 'https://%s.googlesource.com/my/repo' % short_hostname,
'remote: Processing changes: (-)\n' ref_to_push + ':refs/for/refs/heads/' + default_branch +
'remote: Processing changes: new: 1 (/)\n' ref_suffix
'remote: Processing changes: new: 1, done\n' ] + (push_opts if push_opts else []), ),
'remote:\n' (('remote:\n'
'remote: New Changes:\n' 'remote: Processing changes: (\)\n'
'remote: https://%s-review.googlesource.com/#/c/my/repo/+/123456' 'remote: Processing changes: (|)\n'
' XXX\n' 'remote: Processing changes: (/)\n'
'remote:\n' 'remote: Processing changes: (-)\n'
'To https://%s.googlesource.com/my/repo\n' 'remote: Processing changes: new: 1 (/)\n'
' * [new branch] hhhh -> refs/for/refs/heads/%s\n' 'remote: Processing changes: new: 1, done\n'
) % (short_hostname, short_hostname, default_branch)),), 'remote:\n'
(('time.time',), 2000,), 'remote: New Changes:\n'
(('add_repeated', 'remote: '
'sub_commands', 'https://%s-review.googlesource.com/#/c/my/repo/+/123456'
{ ' XXX\n'
'execution_time': 1000, 'remote:\n'
'command': 'git push', 'To https://%s.googlesource.com/my/repo\n'
'exit_code': 0, ' * [new branch] hhhh -> refs/for/refs/heads/%s\n') %
'arguments': sorted(metrics_arguments), (short_hostname, short_hostname, default_branch)),
}), ),
None,), (
('time.time', ),
2000,
),
(
('add_repeated', 'sub_commands', {
'execution_time': 1000,
'command': 'git push',
'exit_code': 0,
'arguments': sorted(metrics_arguments),
}),
None,
),
] ]
final_description = final_description or post_amend_description.strip() final_description = final_description or post_amend_description.strip()
@ -1087,32 +1110,32 @@ class TestGitCl(unittest.TestCase):
] ]
return calls return calls
def _run_gerrit_upload_test( def _run_gerrit_upload_test(self,
self, upload_args,
upload_args, description,
description, reviewers=None,
reviewers=None, squash=True,
squash=True, squash_mode=None,
squash_mode=None, title=None,
title=None, notify=False,
notify=False, post_amend_description=None,
post_amend_description=None, issue=None,
issue=None, cc=None,
cc=None, fetched_status=None,
fetched_status=None, other_cl_owner=None,
other_cl_owner=None, custom_cl_base=None,
custom_cl_base=None, tbr=None,
tbr=None, short_hostname='chromium',
short_hostname='chromium', labels=None,
labels=None, change_id=None,
change_id=None, final_description=None,
final_description=None, gitcookies_exists=True,
gitcookies_exists=True, force=False,
force=False, log_description=None,
log_description=None, edit_description=None,
edit_description=None, fetched_description=None,
fetched_description=None, default_branch='master',
default_branch='master'): push_opts=None):
"""Generic gerrit upload test framework.""" """Generic gerrit upload test framework."""
if squash_mode is None: if squash_mode is None:
if '--no-squash' in upload_args: if '--no-squash' in upload_args:
@ -1192,12 +1215,17 @@ class TestGitCl(unittest.TestCase):
'gclient_utils.temporary_file', TemporaryFileMock()).start() 'gclient_utils.temporary_file', TemporaryFileMock()).start()
mock.patch('os.remove', return_value=True).start() mock.patch('os.remove', return_value=True).start()
self.calls += self._gerrit_upload_calls( self.calls += self._gerrit_upload_calls(
description, reviewers, squash, description,
reviewers,
squash,
squash_mode=squash_mode, squash_mode=squash_mode,
title=title, notify=notify, title=title,
notify=notify,
post_amend_description=post_amend_description, post_amend_description=post_amend_description,
issue=issue, cc=cc, issue=issue,
custom_cl_base=custom_cl_base, tbr=tbr, cc=cc,
custom_cl_base=custom_cl_base,
tbr=tbr,
short_hostname=short_hostname, short_hostname=short_hostname,
labels=labels, labels=labels,
change_id=change_id, change_id=change_id,
@ -1205,7 +1233,8 @@ class TestGitCl(unittest.TestCase):
gitcookies_exists=gitcookies_exists, gitcookies_exists=gitcookies_exists,
force=force, force=force,
edit_description=edit_description, edit_description=edit_description,
default_branch=default_branch) default_branch=default_branch,
push_opts=push_opts)
# Uncomment when debugging. # Uncomment when debugging.
# print('\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))) # print('\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))))
git_cl.main(['upload'] + upload_args) git_cl.main(['upload'] + upload_args)
@ -1260,16 +1289,24 @@ class TestGitCl(unittest.TestCase):
squash_mode='override_nosquash', squash_mode='override_nosquash',
change_id='I123456789') change_id='I123456789')
def test_gerrit_push_opts(self):
self._run_gerrit_upload_test(['-o', 'wip'],
'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n',
[],
squash=False,
squash_mode='override_nosquash',
change_id='I123456789',
push_opts=['-o', 'wip'])
def test_gerrit_no_reviewer_non_chromium_host(self): def test_gerrit_no_reviewer_non_chromium_host(self):
# TODO(crbug/877717): remove this test case. # TODO(crbug/877717): remove this test case.
self._run_gerrit_upload_test( self._run_gerrit_upload_test([],
[], 'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n',
'desc ✔\n\nBUG=\n\nChange-Id: I123456789\n', [],
[], squash=False,
squash=False, squash_mode='override_nosquash',
squash_mode='override_nosquash', short_hostname='other',
short_hostname='other', change_id='I123456789')
change_id='I123456789')
def test_gerrit_patchset_title_special_chars_nosquash(self): def test_gerrit_patchset_title_special_chars_nosquash(self):
self._run_gerrit_upload_test( self._run_gerrit_upload_test(

Loading…
Cancel
Save