mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 04:20:28 -06:00
Output a message when a hook fails due to file modification
This commit is contained in:
@@ -89,8 +89,10 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
|
||||
retcode, stdout, stderr = repo.run_hook(hook, filenames)
|
||||
diff_after = cmd_output('git', 'diff', retcode=None, encoding=None)
|
||||
|
||||
file_modifications = diff_before != diff_after
|
||||
|
||||
# If the hook makes changes, fail the commit
|
||||
if diff_before != diff_after:
|
||||
if file_modifications:
|
||||
retcode = 1
|
||||
|
||||
if retcode:
|
||||
@@ -104,9 +106,19 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
|
||||
|
||||
write(color.format_color(pass_fail, print_color, args.color) + '\n')
|
||||
|
||||
if (stdout or stderr) and (retcode or args.verbose):
|
||||
if (stdout or stderr or file_modifications) and (retcode or args.verbose):
|
||||
write('hookid: {0}\n'.format(hook['id']))
|
||||
write('\n')
|
||||
|
||||
# Print a message if failing due to file modifications
|
||||
if file_modifications:
|
||||
write('Files were modified by this hook.')
|
||||
|
||||
if stdout or stderr:
|
||||
write(' Additional output:\n')
|
||||
|
||||
write('\n')
|
||||
|
||||
for output in (stdout, stderr):
|
||||
assert type(output) is bytes, type(output)
|
||||
if output.strip():
|
||||
|
||||
6
testing/resources/modified_file_returns_zero_repo/bin/hook3.sh
Executable file
6
testing/resources/modified_file_returns_zero_repo/bin/hook3.sh
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
for f in $@; do
|
||||
# Non UTF-8 bytes
|
||||
echo -e '\x01\x97' > "$f"
|
||||
done
|
||||
@@ -2,9 +2,14 @@
|
||||
name: Bash hook
|
||||
entry: bin/hook.sh
|
||||
language: script
|
||||
files: ''
|
||||
files: 'foo.py'
|
||||
- id: bash_hook2
|
||||
name: Bash hook
|
||||
entry: bin/hook2.sh
|
||||
language: script
|
||||
files: ''
|
||||
- id: bash_hook3
|
||||
name: Bash hook
|
||||
entry: bin/hook3.sh
|
||||
language: script
|
||||
files: 'bar.py'
|
||||
|
||||
@@ -40,9 +40,9 @@ def repo_with_failing_hook(tempdir_factory):
|
||||
yield git_path
|
||||
|
||||
|
||||
def stage_a_file():
|
||||
cmd_output('touch', 'foo.py')
|
||||
cmd_output('git', 'add', 'foo.py')
|
||||
def stage_a_file(filename='foo.py'):
|
||||
cmd_output('touch', filename)
|
||||
cmd_output('git', 'add', filename)
|
||||
|
||||
|
||||
def get_write_mock_output(write_mock):
|
||||
@@ -127,16 +127,22 @@ def test_hook_that_modifies_but_returns_zero(
|
||||
tempdir_factory, 'modified_file_returns_zero_repo',
|
||||
)
|
||||
with cwd(git_path):
|
||||
stage_a_file('bar.py')
|
||||
_test_run(
|
||||
git_path,
|
||||
{},
|
||||
(
|
||||
# The first should fail
|
||||
b'Failed',
|
||||
# With a modified file (the hook's output)
|
||||
# With a modified file (default message + the hook's output)
|
||||
b'Files were modified by this hook. Additional output:\n\n'
|
||||
b'Modified: foo.py',
|
||||
# The next hook should pass despite the first modifying
|
||||
b'Passed',
|
||||
# The next hook should fail
|
||||
b'Failed',
|
||||
# bar.py was modified, but provides no additional output
|
||||
b'Files were modified by this hook.\n',
|
||||
),
|
||||
1,
|
||||
True,
|
||||
|
||||
Reference in New Issue
Block a user