Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 512a6a2

Browse files
committed
Merge pull request #305 from jdb8/output-on-files-changed
Output a message when a hook fails due to file modification
2 parents 956eefc + 91a547e commit 512a6a2

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

pre_commit/commands/run.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
8989
retcode, stdout, stderr = repo.run_hook(hook, filenames)
9090
diff_after = cmd_output('git', 'diff', retcode=None, encoding=None)
9191

92+
file_modifications = diff_before != diff_after
93+
9294
# If the hook makes changes, fail the commit
93-
if diff_before != diff_after:
95+
if file_modifications:
9496
retcode = 1
9597

9698
if retcode:
@@ -104,9 +106,19 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()):
104106

105107
write(color.format_color(pass_fail, print_color, args.color) + '\n')
106108

107-
if (stdout or stderr) and (retcode or args.verbose):
109+
if (stdout or stderr or file_modifications) and (retcode or args.verbose):
108110
write('hookid: {0}\n'.format(hook['id']))
109111
write('\n')
112+
113+
# Print a message if failing due to file modifications
114+
if file_modifications:
115+
write('Files were modified by this hook.')
116+
117+
if stdout or stderr:
118+
write(' Additional output:\n')
119+
120+
write('\n')
121+
110122
for output in (stdout, stderr):
111123
assert type(output) is bytes, type(output)
112124
if output.strip():
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
for f in $@; do
4+
# Non UTF-8 bytes
5+
echo -e '\x01\x97' > "$f"
6+
done

testing/resources/modified_file_returns_zero_repo/hooks.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
name: Bash hook
33
entry: bin/hook.sh
44
language: script
5-
files: ''
5+
files: 'foo.py'
66
- id: bash_hook2
77
name: Bash hook
88
entry: bin/hook2.sh
99
language: script
1010
files: ''
11+
- id: bash_hook3
12+
name: Bash hook
13+
entry: bin/hook3.sh
14+
language: script
15+
files: 'bar.py'

tests/commands/run_test.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def repo_with_failing_hook(tempdir_factory):
4040
yield git_path
4141

4242

43-
def stage_a_file():
44-
cmd_output('touch', 'foo.py')
45-
cmd_output('git', 'add', 'foo.py')
43+
def stage_a_file(filename='foo.py'):
44+
cmd_output('touch', filename)
45+
cmd_output('git', 'add', filename)
4646

4747

4848
def get_write_mock_output(write_mock):
@@ -127,16 +127,22 @@ def test_hook_that_modifies_but_returns_zero(
127127
tempdir_factory, 'modified_file_returns_zero_repo',
128128
)
129129
with cwd(git_path):
130+
stage_a_file('bar.py')
130131
_test_run(
131132
git_path,
132133
{},
133134
(
134135
# The first should fail
135136
b'Failed',
136-
# With a modified file (the hook's output)
137+
# With a modified file (default message + the hook's output)
138+
b'Files were modified by this hook. Additional output:\n\n'
137139
b'Modified: foo.py',
138140
# The next hook should pass despite the first modifying
139141
b'Passed',
142+
# The next hook should fail
143+
b'Failed',
144+
# bar.py was modified, but provides no additional output
145+
b'Files were modified by this hook.\n',
140146
),
141147
1,
142148
True,

0 commit comments

Comments
 (0)