When run is called with -v, then show hook.description if available.

When developers run the pre-commit, they don't always understand what to or where to find more information. The hook description field is already present and often used to provide more descriptive information. For brevity this is not shown in normal cases, but this PR changes the tool behavior to show the description in verbose mode (`-v`). So most users won't see any difference but this can easily be enabled for people who want it.

Alternatives considered:
* Always show the description: Some people may not like this though as it can be spammy.
* Provide a new field: Possible but seems to be just more work with description already present and not otherwise used.
* Show the `repo` link. This is at the wrong level and has a slightly different purpose. Though description can just be set to the same url where that is correct.
This commit is contained in:
helly25 2024-04-25 09:10:44 +00:00
parent 85fe18253f
commit 689f1203f7
No known key found for this signature in database
GPG key ID: 95B24A6EAE247816
4 changed files with 32 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,6 +1,8 @@
*.egg-info *.egg-info
*.py[co] *.py[co]
*.pytest_cache
/.coverage /.coverage
/.tox /.tox
/build
/dist /dist
.vscode/ .vscode/

View file

@ -222,6 +222,13 @@ def _run_single_hook(
if retcode: if retcode:
_subtle_line(f'- exit code: {retcode}', use_color) _subtle_line(f'- exit code: {retcode}', use_color)
if verbose and hook.description:
if '\n' in hook.description:
_subtle_line('- description: |', use_color)
for l in hook.description.splitlines():
_subtle_line(' ' + l, use_color)
else:
_subtle_line(f'- description: {hook.description}', use_color)
# Print a message if failing due to file modifications # Print a message if failing due to file modifications
if files_modified: if files_modified:

View file

@ -1,5 +1,8 @@
- id: failing_hook - id: failing_hook
name: Failing hook name: Failing hook
description: |
Failing Hook Description
Longer description.
entry: bin/hook.sh entry: bin/hook.sh
language: script language: script
files: . files: .

View file

@ -173,6 +173,26 @@ def test_run_all_hooks_failing(cap_out, store, repo_with_failing_hook):
) )
def test_run_all_hooks_failing_verbose(cap_out, store, repo_with_failing_hook):
_test_run(
cap_out,
store,
repo_with_failing_hook,
{'verbose': True},
(
b'Failing hook',
b'Failed',
b'hook id: failing_hook',
b'description: |',
b' Failing Hook Description',
b' Longer description.',
b'Fail\nfoo.py\n',
),
expected_ret=1,
stage=True,
)
def test_arbitrary_bytes_hook(cap_out, store, tempdir_factory): def test_arbitrary_bytes_hook(cap_out, store, tempdir_factory):
git_path = make_consuming_repo(tempdir_factory, 'arbitrary_bytes_repo') git_path = make_consuming_repo(tempdir_factory, 'arbitrary_bytes_repo')
with cwd(git_path): with cwd(git_path):