Skip to content

Commit dde988b

Browse files
authored
Merge pull request #1288 from dmbarreiro/push_remote
Push remote env var details
2 parents b66d289 + 0bb8a8f commit dde988b

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

pre_commit/commands/run.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ def run(
312312
environ['PRE_COMMIT_ORIGIN'] = args.origin
313313
environ['PRE_COMMIT_SOURCE'] = args.source
314314

315+
if args.remote_name and args.remote_url:
316+
environ['PRE_COMMIT_REMOTE_NAME'] = args.remote_name
317+
environ['PRE_COMMIT_REMOTE_URL'] = args.remote_url
318+
315319
with contextlib.ExitStack() as exit_stack:
316320
if stash:
317321
exit_stack.enter_context(staged_files_only(store.directory))

pre_commit/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
101101
'--commit-msg-filename',
102102
help='Filename to check when running during `commit-msg`',
103103
)
104+
parser.add_argument(
105+
'--remote-name', help='Remote name used by `git push`.',
106+
)
107+
parser.add_argument('--remote-url', help='Remote url used by `git push`.')
104108
parser.add_argument(
105109
'--hook-stage', choices=C.STAGES, default='commit',
106110
help='The stage during which the hook is fired. One of %(choices)s',

pre_commit/resources/hook-tmpl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def _rev_exists(rev: str) -> bool:
120120

121121

122122
def _pre_push(stdin: bytes) -> Tuple[str, ...]:
123-
remote = sys.argv[1]
123+
remote_name = sys.argv[1]
124+
remote_url = sys.argv[2]
124125

125126
opts: Tuple[str, ...] = ()
126127
for line in stdin.decode().splitlines():
@@ -133,7 +134,7 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
133134
# ancestors not found in remote
134135
ancestors = subprocess.check_output((
135136
'git', 'rev-list', local_sha, '--topo-order', '--reverse',
136-
'--not', f'--remotes={remote}',
137+
'--not', f'--remotes={remote_name}',
137138
)).decode().strip()
138139
if not ancestors:
139140
continue
@@ -150,7 +151,9 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
150151
opts = ('--origin', local_sha, '--source', source)
151152

152153
if opts:
153-
return opts
154+
return (
155+
*opts, '--remote-name', remote_name, '--remote-url', remote_url,
156+
)
154157
else:
155158
# An attempt to push an empty changeset
156159
raise EarlyExit()

testing/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def run_opts(
6767
hook=None,
6868
origin='',
6969
source='',
70+
remote_name='',
71+
remote_url='',
7072
hook_stage='commit',
7173
show_diff_on_failure=False,
7274
commit_msg_filename='',
@@ -81,6 +83,8 @@ def run_opts(
8183
hook=hook,
8284
origin=origin,
8385
source=source,
86+
remote_name=remote_name,
87+
remote_url=remote_url,
8488
hook_stage=hook_stage,
8589
show_diff_on_failure=show_diff_on_failure,
8690
commit_msg_filename=commit_msg_filename,

tests/commands/install_uninstall_test.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pre_commit.util import cmd_output
1616
from pre_commit.util import make_executable
1717
from pre_commit.util import resource_text
18+
from testing.fixtures import add_config_to_repo
1819
from testing.fixtures import git_dir
1920
from testing.fixtures import make_consuming_repo
2021
from testing.fixtures import remove_config_from_repo
@@ -512,9 +513,9 @@ def test_installed_from_venv(tempdir_factory, store):
512513
assert NORMAL_PRE_COMMIT_RUN.match(output)
513514

514515

515-
def _get_push_output(tempdir_factory, opts=()):
516+
def _get_push_output(tempdir_factory, remote='origin', opts=()):
516517
return cmd_output_mocked_pre_commit_home(
517-
'git', 'push', 'origin', 'HEAD:new_branch', *opts,
518+
'git', 'push', remote, 'HEAD:new_branch', *opts,
518519
tempdir_factory=tempdir_factory,
519520
retcode=None,
520521
)[:2]
@@ -589,6 +590,33 @@ def test_pre_push_new_upstream(tempdir_factory, store):
589590
assert 'Passed' in output
590591

591592

593+
def test_pre_push_environment_variables(tempdir_factory, store):
594+
config = {
595+
'repo': 'local',
596+
'hooks': [
597+
{
598+
'id': 'print-remote-info',
599+
'name': 'print remote info',
600+
'entry': 'bash -c "echo remote: $PRE_COMMIT_REMOTE_NAME"',
601+
'language': 'system',
602+
'verbose': True,
603+
},
604+
],
605+
}
606+
607+
upstream = git_dir(tempdir_factory)
608+
clone = tempdir_factory.get()
609+
cmd_output('git', 'clone', upstream, clone)
610+
add_config_to_repo(clone, config)
611+
with cwd(clone):
612+
install(C.CONFIG_FILE, store, hook_types=['pre-push'])
613+
614+
cmd_output('git', 'remote', 'rename', 'origin', 'origin2')
615+
retc, output = _get_push_output(tempdir_factory, remote='origin2')
616+
assert retc == 0
617+
assert '\nremote: origin2\n' in output
618+
619+
592620
def test_pre_push_integration_empty_push(tempdir_factory, store):
593621
upstream = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
594622
path = tempdir_factory.get()

tests/commands/run_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,11 @@ def test_origin_source_error_msg_error(
456456
assert b'Specify both --origin and --source.' in printed
457457

458458

459-
def test_origin_source_both_ok(cap_out, store, repo_with_passing_hook):
460-
args = run_opts(origin='master', source='master')
459+
def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
460+
args = run_opts(
461+
origin='master', source='master',
462+
remote_name='origin', remote_url='https://example.com/repo',
463+
)
461464
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)
462465
assert ret == 0
463466
assert b'Specify both --origin and --source.' not in printed

0 commit comments

Comments
 (0)