mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Push remote env var details
This commit is contained in:
parent
b66d28964b
commit
57cc814b8b
5 changed files with 50 additions and 3 deletions
|
|
@ -312,6 +312,10 @@ def run(
|
||||||
environ['PRE_COMMIT_ORIGIN'] = args.origin
|
environ['PRE_COMMIT_ORIGIN'] = args.origin
|
||||||
environ['PRE_COMMIT_SOURCE'] = args.source
|
environ['PRE_COMMIT_SOURCE'] = args.source
|
||||||
|
|
||||||
|
if args.push_remote_name and args.push_remote_url:
|
||||||
|
environ['PRE_COMMIT_REMOTE_NAME'] = args.push_remote_name
|
||||||
|
environ['PRE_COMMIT_REMOTE_URL'] = args.push_remote_url
|
||||||
|
|
||||||
with contextlib.ExitStack() as exit_stack:
|
with contextlib.ExitStack() as exit_stack:
|
||||||
if stash:
|
if stash:
|
||||||
exit_stack.enter_context(staged_files_only(store.directory))
|
exit_stack.enter_context(staged_files_only(store.directory))
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,12 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
|
||||||
'--commit-msg-filename',
|
'--commit-msg-filename',
|
||||||
help='Filename to check when running during `commit-msg`',
|
help='Filename to check when running during `commit-msg`',
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--push-remote-name', help='Remote name used by `git push`.',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--push-remote-url', help='Remote url used by `git push`.',
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--hook-stage', choices=C.STAGES, default='commit',
|
'--hook-stage', choices=C.STAGES, default='commit',
|
||||||
help='The stage during which the hook is fired. One of %(choices)s',
|
help='The stage during which the hook is fired. One of %(choices)s',
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,8 @@ def _rev_exists(rev: str) -> bool:
|
||||||
|
|
||||||
|
|
||||||
def _pre_push(stdin: bytes) -> Tuple[str, ...]:
|
def _pre_push(stdin: bytes) -> Tuple[str, ...]:
|
||||||
remote = sys.argv[1]
|
remote_name = sys.argv[1]
|
||||||
|
remote_url = sys.argv[2]
|
||||||
|
|
||||||
opts: Tuple[str, ...] = ()
|
opts: Tuple[str, ...] = ()
|
||||||
for line in stdin.decode().splitlines():
|
for line in stdin.decode().splitlines():
|
||||||
|
|
@ -133,7 +134,7 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
|
||||||
# ancestors not found in remote
|
# ancestors not found in remote
|
||||||
ancestors = subprocess.check_output((
|
ancestors = subprocess.check_output((
|
||||||
'git', 'rev-list', local_sha, '--topo-order', '--reverse',
|
'git', 'rev-list', local_sha, '--topo-order', '--reverse',
|
||||||
'--not', f'--remotes={remote}',
|
'--not', f'--remotes={remote_name}',
|
||||||
)).decode().strip()
|
)).decode().strip()
|
||||||
if not ancestors:
|
if not ancestors:
|
||||||
continue
|
continue
|
||||||
|
|
@ -150,7 +151,10 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
|
||||||
opts = ('--origin', local_sha, '--source', source)
|
opts = ('--origin', local_sha, '--source', source)
|
||||||
|
|
||||||
if opts:
|
if opts:
|
||||||
return opts
|
remote_opts = (
|
||||||
|
'--push-remote-name', remote_name, '--push-remote-url', remote_url,
|
||||||
|
)
|
||||||
|
return opts + remote_opts
|
||||||
else:
|
else:
|
||||||
# An attempt to push an empty changeset
|
# An attempt to push an empty changeset
|
||||||
raise EarlyExit()
|
raise EarlyExit()
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,8 @@ def run_opts(
|
||||||
hook=None,
|
hook=None,
|
||||||
origin='',
|
origin='',
|
||||||
source='',
|
source='',
|
||||||
|
push_remote_name='',
|
||||||
|
push_remote_url='',
|
||||||
hook_stage='commit',
|
hook_stage='commit',
|
||||||
show_diff_on_failure=False,
|
show_diff_on_failure=False,
|
||||||
commit_msg_filename='',
|
commit_msg_filename='',
|
||||||
|
|
@ -81,6 +83,8 @@ def run_opts(
|
||||||
hook=hook,
|
hook=hook,
|
||||||
origin=origin,
|
origin=origin,
|
||||||
source=source,
|
source=source,
|
||||||
|
push_remote_name=push_remote_name,
|
||||||
|
push_remote_url=push_remote_url,
|
||||||
hook_stage=hook_stage,
|
hook_stage=hook_stage,
|
||||||
show_diff_on_failure=show_diff_on_failure,
|
show_diff_on_failure=show_diff_on_failure,
|
||||||
commit_msg_filename=commit_msg_filename,
|
commit_msg_filename=commit_msg_filename,
|
||||||
|
|
|
||||||
|
|
@ -687,6 +687,35 @@ def test_stages(cap_out, store, repo_with_passing_hook):
|
||||||
assert _run_for_stage('commit-msg').startswith(b'hook 5...')
|
assert _run_for_stage('commit-msg').startswith(b'hook 5...')
|
||||||
|
|
||||||
|
|
||||||
|
def test_push_remote_environment(cap_out, store, repo_with_passing_hook):
|
||||||
|
config = {
|
||||||
|
'repo': 'local',
|
||||||
|
'hooks': [
|
||||||
|
{
|
||||||
|
'id': 'print-push-remote',
|
||||||
|
'name': 'Print push remote name',
|
||||||
|
'entry': 'entry: bash -c \'echo "$PRE_COMMIT_REMOTE_NAME"\'',
|
||||||
|
'language': 'system',
|
||||||
|
'verbose': bool(1),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
add_config_to_repo(repo_with_passing_hook, config)
|
||||||
|
|
||||||
|
_test_run(
|
||||||
|
cap_out,
|
||||||
|
store,
|
||||||
|
repo_with_passing_hook,
|
||||||
|
opts={
|
||||||
|
'push_remote_name': 'origin',
|
||||||
|
'push_remote_url': 'https://github.com/pre-commit/pre-commit',
|
||||||
|
},
|
||||||
|
expected_outputs=[b'Print push remote name', b'Passed'],
|
||||||
|
expected_ret=0,
|
||||||
|
stage=['push'],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_commit_msg_hook(cap_out, store, commit_msg_repo):
|
def test_commit_msg_hook(cap_out, store, commit_msg_repo):
|
||||||
filename = '.git/COMMIT_EDITMSG'
|
filename = '.git/COMMIT_EDITMSG'
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue