mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Output a message when a hook fails due to file modification
This commit is contained in:
parent
c6200329a2
commit
91a547ed61
4 changed files with 36 additions and 7 deletions
|
|
@ -89,8 +89,10 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
|
||||||
retcode, stdout, stderr = repo.run_hook(hook, filenames)
|
retcode, stdout, stderr = repo.run_hook(hook, filenames)
|
||||||
diff_after = cmd_output('git', 'diff', retcode=None, encoding=None)
|
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 the hook makes changes, fail the commit
|
||||||
if diff_before != diff_after:
|
if file_modifications:
|
||||||
retcode = 1
|
retcode = 1
|
||||||
|
|
||||||
if retcode:
|
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')
|
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('hookid: {0}\n'.format(hook['id']))
|
||||||
write('\n')
|
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):
|
for output in (stdout, stderr):
|
||||||
assert type(output) is bytes, type(output)
|
assert type(output) is bytes, type(output)
|
||||||
if output.strip():
|
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
|
name: Bash hook
|
||||||
entry: bin/hook.sh
|
entry: bin/hook.sh
|
||||||
language: script
|
language: script
|
||||||
files: ''
|
files: 'foo.py'
|
||||||
- id: bash_hook2
|
- id: bash_hook2
|
||||||
name: Bash hook
|
name: Bash hook
|
||||||
entry: bin/hook2.sh
|
entry: bin/hook2.sh
|
||||||
language: script
|
language: script
|
||||||
files: ''
|
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
|
yield git_path
|
||||||
|
|
||||||
|
|
||||||
def stage_a_file():
|
def stage_a_file(filename='foo.py'):
|
||||||
cmd_output('touch', 'foo.py')
|
cmd_output('touch', filename)
|
||||||
cmd_output('git', 'add', 'foo.py')
|
cmd_output('git', 'add', filename)
|
||||||
|
|
||||||
|
|
||||||
def get_write_mock_output(write_mock):
|
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',
|
tempdir_factory, 'modified_file_returns_zero_repo',
|
||||||
)
|
)
|
||||||
with cwd(git_path):
|
with cwd(git_path):
|
||||||
|
stage_a_file('bar.py')
|
||||||
_test_run(
|
_test_run(
|
||||||
git_path,
|
git_path,
|
||||||
{},
|
{},
|
||||||
(
|
(
|
||||||
# The first should fail
|
# The first should fail
|
||||||
b'Failed',
|
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',
|
b'Modified: foo.py',
|
||||||
# The next hook should pass despite the first modifying
|
# The next hook should pass despite the first modifying
|
||||||
b'Passed',
|
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,
|
1,
|
||||||
True,
|
True,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue