mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Add --hook-args option to try-repo
This commit is contained in:
parent
99fa9ba5ef
commit
6d22fc01d0
3 changed files with 53 additions and 3 deletions
|
|
@ -4,6 +4,7 @@ import argparse
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import warnings
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
|
|
@ -54,8 +55,15 @@ def try_repo(args: argparse.Namespace) -> int:
|
||||||
|
|
||||||
store = Store(tempdir)
|
store = Store(tempdir)
|
||||||
if args.hook:
|
if args.hook:
|
||||||
hooks = [{'id': args.hook}]
|
hook = {'id': args.hook}
|
||||||
|
if args.hook_args is not None:
|
||||||
|
hook['args'] = args.hook_args
|
||||||
|
hooks = [hook]
|
||||||
else:
|
else:
|
||||||
|
if args.hook_args is not None:
|
||||||
|
warnings.warn(
|
||||||
|
"Unused flag '--hook-args' as 'hook' is not specified.",
|
||||||
|
)
|
||||||
repo_path = store.clone(repo, ref)
|
repo_path = store.clone(repo, ref)
|
||||||
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
|
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
|
||||||
manifest = sorted(manifest, key=lambda hook: hook['id'])
|
manifest = sorted(manifest, key=lambda hook: hook['id'])
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
|
@ -171,6 +172,14 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _add_hook_flags_option(parser: argparse.ArgumentParser) -> None:
|
||||||
|
parser.add_argument(
|
||||||
|
'--hook-args',
|
||||||
|
type=shlex.split,
|
||||||
|
help='Raw string with flags to pass into the hook (if specified).',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
|
||||||
# `--config` was specified relative to the non-root working directory
|
# `--config` was specified relative to the non-root working directory
|
||||||
if os.path.exists(args.config):
|
if os.path.exists(args.config):
|
||||||
|
|
@ -324,6 +333,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
_add_run_options(try_repo_parser)
|
_add_run_options(try_repo_parser)
|
||||||
|
_add_hook_flags_option(try_repo_parser)
|
||||||
|
|
||||||
uninstall_parser = _add_cmd(
|
uninstall_parser = _add_cmd(
|
||||||
'uninstall', help='Uninstall the pre-commit script.',
|
'uninstall', help='Uninstall the pre-commit script.',
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,13 @@ from testing.util import git_commit
|
||||||
from testing.util import run_opts
|
from testing.util import run_opts
|
||||||
|
|
||||||
|
|
||||||
def try_repo_opts(repo, ref=None, **kwargs):
|
def try_repo_opts(repo, ref=None, hook_args=None, **kwargs):
|
||||||
return auto_namedtuple(repo=repo, ref=ref, **run_opts(**kwargs)._asdict())
|
return auto_namedtuple(
|
||||||
|
repo=repo,
|
||||||
|
ref=ref,
|
||||||
|
hook_args=hook_args,
|
||||||
|
**run_opts(**kwargs)._asdict(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_out(cap_out):
|
def _get_out(cap_out):
|
||||||
|
|
@ -89,6 +94,33 @@ Bash hook............................................(no files to check)Skipped
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def test_try_repo_with_specific_hook_and_args(cap_out, tempdir_factory):
|
||||||
|
_run_try_repo(
|
||||||
|
tempdir_factory,
|
||||||
|
hook='bash_hook',
|
||||||
|
hook_args=['pwd', '&&', 'ls'],
|
||||||
|
verbose=True,
|
||||||
|
)
|
||||||
|
start, config, rest = _get_out(cap_out)
|
||||||
|
assert start == ''
|
||||||
|
config_pattern = re_assert.Matches(
|
||||||
|
'^repos:\n'
|
||||||
|
'- repo: .+\n'
|
||||||
|
' rev: .+\n'
|
||||||
|
' hooks:\n'
|
||||||
|
' - id: bash_hook\n'
|
||||||
|
' args:\n'
|
||||||
|
' - pwd\n'
|
||||||
|
' - \'&&\'\n'
|
||||||
|
' - ls\n$',
|
||||||
|
)
|
||||||
|
config_pattern.assert_matches(config)
|
||||||
|
assert rest == '''\
|
||||||
|
Bash hook............................................(no files to check)Skipped
|
||||||
|
- hook id: bash_hook
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
def test_try_repo_relative_path(cap_out, tempdir_factory):
|
def test_try_repo_relative_path(cap_out, tempdir_factory):
|
||||||
repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo')
|
repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo')
|
||||||
with cwd(git_dir(tempdir_factory)):
|
with cwd(git_dir(tempdir_factory)):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue