Add --quiet to the hook-impl command.

Can now add --quiet to the scripts like
.git/hooks/pre-commit to run in quiet mode.
This commit is contained in:
Peter Cock 2020-08-22 13:27:33 +01:00
parent b7e48bf702
commit 7aacceff93
3 changed files with 25 additions and 12 deletions

View file

@ -75,6 +75,7 @@ def _ns(
remote_url: Optional[str] = None, remote_url: Optional[str] = None,
commit_msg_filename: Optional[str] = None, commit_msg_filename: Optional[str] = None,
checkout_type: Optional[str] = None, checkout_type: Optional[str] = None,
quiet: bool = False,
) -> argparse.Namespace: ) -> argparse.Namespace:
return argparse.Namespace( return argparse.Namespace(
color=color, color=color,
@ -89,7 +90,7 @@ def _ns(
files=(), files=(),
hook=None, hook=None,
verbose=False, verbose=False,
quiet=False, quiet=quiet,
show_diff_on_failure=False, show_diff_on_failure=False,
) )
@ -180,6 +181,7 @@ def _check_args_length(hook_type: str, args: Sequence[str]) -> None:
def _run_ns( def _run_ns(
hook_type: str, hook_type: str,
color: bool, color: bool,
quiet: bool,
args: Sequence[str], args: Sequence[str],
stdin: bytes, stdin: bytes,
) -> Optional[argparse.Namespace]: ) -> Optional[argparse.Namespace]:
@ -189,7 +191,7 @@ def _run_ns(
elif hook_type in {'commit-msg', 'prepare-commit-msg'}: elif hook_type in {'commit-msg', 'prepare-commit-msg'}:
return _ns(hook_type, color, commit_msg_filename=args[0]) return _ns(hook_type, color, commit_msg_filename=args[0])
elif hook_type in {'post-commit', 'pre-merge-commit', 'pre-commit'}: elif hook_type in {'post-commit', 'pre-merge-commit', 'pre-commit'}:
return _ns(hook_type, color) return _ns(hook_type, color, quiet=quiet)
elif hook_type == 'post-checkout': elif hook_type == 'post-checkout':
return _ns( return _ns(
hook_type, color, hook_type, color,
@ -207,11 +209,12 @@ def hook_impl(
hook_type: str, hook_type: str,
hook_dir: str, hook_dir: str,
skip_on_missing_config: bool, skip_on_missing_config: bool,
quiet: bool,
args: Sequence[str], args: Sequence[str],
) -> int: ) -> int:
retv, stdin = _run_legacy(hook_type, hook_dir, args) retv, stdin = _run_legacy(hook_type, hook_dir, args)
_validate_config(retv, config, skip_on_missing_config) _validate_config(retv, config, skip_on_missing_config)
ns = _run_ns(hook_type, color, args, stdin) ns = _run_ns(hook_type, color, quiet, args, stdin)
if ns is None: if ns is None:
return retv return retv
else: else:

View file

@ -222,6 +222,11 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
hook_impl_parser.add_argument( hook_impl_parser.add_argument(
'--skip-on-missing-config', action='store_true', '--skip-on-missing-config', action='store_true',
) )
hook_impl_parser.add_argument(
'--quiet', action='store_true', default=False,
help='Enable quiet mode (in post-commit, pre-merge-commit, '
'pre-commit hooks).',
)
hook_impl_parser.add_argument(dest='rest', nargs=argparse.REMAINDER) hook_impl_parser.add_argument(dest='rest', nargs=argparse.REMAINDER)
gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.') gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')
@ -370,6 +375,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
hook_type=args.hook_type, hook_type=args.hook_type,
hook_dir=args.hook_dir, hook_dir=args.hook_dir,
skip_on_missing_config=args.skip_on_missing_config, skip_on_missing_config=args.skip_on_missing_config,
quiet=args.quiet,
args=args.rest[1:], args=args.rest[1:],
) )
elif args.command == 'install': elif args.command == 'install':

View file

@ -136,14 +136,16 @@ def test_check_args_length_prepare_commit_msg_error():
def test_run_ns_pre_commit(): def test_run_ns_pre_commit():
ns = hook_impl._run_ns('pre-commit', True, (), b'') ns = hook_impl._run_ns('pre-commit', True, False, (), b'')
assert ns is not None assert ns is not None
assert ns.hook_stage == 'commit' assert ns.hook_stage == 'commit'
assert ns.color is True assert ns.color is True
def test_run_ns_commit_msg(): def test_run_ns_commit_msg():
ns = hook_impl._run_ns('commit-msg', False, ('.git/COMMIT_MSG',), b'') ns = hook_impl._run_ns(
'commit-msg', False, False, ('.git/COMMIT_MSG',), b'',
)
assert ns is not None assert ns is not None
assert ns.hook_stage == 'commit-msg' assert ns.hook_stage == 'commit-msg'
assert ns.color is False assert ns.color is False
@ -151,14 +153,14 @@ def test_run_ns_commit_msg():
def test_run_ns_post_commit(): def test_run_ns_post_commit():
ns = hook_impl._run_ns('post-commit', True, (), b'') ns = hook_impl._run_ns('post-commit', True, False, (), b'')
assert ns is not None assert ns is not None
assert ns.hook_stage == 'post-commit' assert ns.hook_stage == 'post-commit'
assert ns.color is True assert ns.color is True
def test_run_ns_post_checkout(): def test_run_ns_post_checkout():
ns = hook_impl._run_ns('post-checkout', True, ('a', 'b', 'c'), b'') ns = hook_impl._run_ns('post-checkout', True, False, ('a', 'b', 'c'), b'')
assert ns is not None assert ns is not None
assert ns.hook_stage == 'post-checkout' assert ns.hook_stage == 'post-checkout'
assert ns.color is True assert ns.color is True
@ -186,7 +188,7 @@ def test_run_ns_pre_push_updating_branch(push_example):
with cwd(clone): with cwd(clone):
args = ('origin', src) args = ('origin', src)
stdin = f'HEAD {clone_head} refs/heads/b {src_head}\n'.encode() stdin = f'HEAD {clone_head} refs/heads/b {src_head}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin) ns = hook_impl._run_ns('pre-push', False, False, args, stdin)
assert ns is not None assert ns is not None
assert ns.hook_stage == 'push' assert ns.hook_stage == 'push'
@ -204,7 +206,7 @@ def test_run_ns_pre_push_new_branch(push_example):
with cwd(clone): with cwd(clone):
args = ('origin', src) args = ('origin', src)
stdin = f'HEAD {clone_head} refs/heads/b {hook_impl.Z40}\n'.encode() stdin = f'HEAD {clone_head} refs/heads/b {hook_impl.Z40}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin) ns = hook_impl._run_ns('pre-push', False, False, args, stdin)
assert ns is not None assert ns is not None
assert ns.from_ref == src_head assert ns.from_ref == src_head
@ -217,7 +219,7 @@ def test_run_ns_pre_push_new_branch_existing_rev(push_example):
with cwd(clone): with cwd(clone):
args = ('origin', src) args = ('origin', src)
stdin = f'HEAD {src_head} refs/heads/b2 {hook_impl.Z40}\n'.encode() stdin = f'HEAD {src_head} refs/heads/b2 {hook_impl.Z40}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin) ns = hook_impl._run_ns('pre-push', False, False, args, stdin)
assert ns is None assert ns is None
@ -232,7 +234,7 @@ def test_pushing_orphan_branch(push_example):
with cwd(clone): with cwd(clone):
args = ('origin', src) args = ('origin', src)
stdin = f'HEAD {clone_rev} refs/heads/b2 {hook_impl.Z40}\n'.encode() stdin = f'HEAD {clone_rev} refs/heads/b2 {hook_impl.Z40}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin) ns = hook_impl._run_ns('pre-push', False, False, args, stdin)
assert ns is not None assert ns is not None
assert ns.all_files is True assert ns.all_files is True
@ -244,7 +246,7 @@ def test_run_ns_pre_push_deleting_branch(push_example):
with cwd(clone): with cwd(clone):
args = ('origin', src) args = ('origin', src)
stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode() stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin) ns = hook_impl._run_ns('pre-push', False, False, args, stdin)
assert ns is None assert ns is None
@ -262,6 +264,7 @@ def test_hook_impl_main_noop_pre_push(cap_out, store, push_example):
color=False, color=False,
hook_type='pre-push', hook_type='pre-push',
hook_dir='.git/hooks', hook_dir='.git/hooks',
quiet=False,
skip_on_missing_config=False, skip_on_missing_config=False,
args=('origin', src), args=('origin', src),
) )
@ -278,6 +281,7 @@ def test_hook_impl_main_runs_hooks(cap_out, tempdir_factory, store):
color=False, color=False,
hook_type='pre-commit', hook_type='pre-commit',
hook_dir='.git/hooks', hook_dir='.git/hooks',
quiet=False,
skip_on_missing_config=False, skip_on_missing_config=False,
args=(), args=(),
) )