Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57cc814

Browse files
David Martinez Barreiroasottile
authored andcommitted
Push remote env var details
1 parent b66d289 commit 57cc814

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

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.push_remote_name and args.push_remote_url:
316+
environ['PRE_COMMIT_REMOTE_NAME'] = args.push_remote_name
317+
environ['PRE_COMMIT_REMOTE_URL'] = args.push_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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ 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+
'--push-remote-name', help='Remote name used by `git push`.',
106+
)
107+
parser.add_argument(
108+
'--push-remote-url', help='Remote url used by `git push`.',
109+
)
104110
parser.add_argument(
105111
'--hook-stage', choices=C.STAGES, default='commit',
106112
help='The stage during which the hook is fired. One of %(choices)s',

pre_commit/resources/hook-tmpl

Lines changed: 7 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,10 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
150151
opts = ('--origin', local_sha, '--source', source)
151152

152153
if opts:
153-
return opts
154+
remote_opts = (
155+
'--push-remote-name', remote_name, '--push-remote-url', remote_url,
156+
)
157+
return opts + remote_opts
154158
else:
155159
# An attempt to push an empty changeset
156160
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+
push_remote_name='',
71+
push_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+
push_remote_name=push_remote_name,
87+
push_remote_url=push_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/run_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,35 @@ def _run_for_stage(stage):
687687
assert _run_for_stage('commit-msg').startswith(b'hook 5...')
688688

689689

690+
def test_push_remote_environment(cap_out, store, repo_with_passing_hook):
691+
config = {
692+
'repo': 'local',
693+
'hooks': [
694+
{
695+
'id': 'print-push-remote',
696+
'name': 'Print push remote name',
697+
'entry': 'entry: bash -c \'echo "$PRE_COMMIT_REMOTE_NAME"\'',
698+
'language': 'system',
699+
'verbose': bool(1),
700+
},
701+
],
702+
}
703+
add_config_to_repo(repo_with_passing_hook, config)
704+
705+
_test_run(
706+
cap_out,
707+
store,
708+
repo_with_passing_hook,
709+
opts={
710+
'push_remote_name': 'origin',
711+
'push_remote_url': 'https://github.com/pre-commit/pre-commit',
712+
},
713+
expected_outputs=[b'Print push remote name', b'Passed'],
714+
expected_ret=0,
715+
stage=['push'],
716+
)
717+
718+
690719
def test_commit_msg_hook(cap_out, store, commit_msg_repo):
691720
filename = '.git/COMMIT_EDITMSG'
692721
with open(filename, 'w') as f:

0 commit comments

Comments
 (0)