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

Skip to content

Commit de24712

Browse files
committed
Skip hooks if no files to check. Closes #69.
1 parent 6c940c5 commit de24712

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

pre_commit/commands.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ def _run_single_hook(runner, repository, hook_id, args, write):
151151

152152
hook = repository.hooks[hook_id]
153153

154+
filenames = get_filenames(hook['files'], hook['exclude'])
155+
if not filenames:
156+
no_files_msg = '(no files to check) '
157+
skipped_msg = 'Skipped'
158+
write(
159+
'{0}{1}{2}{3}\n'.format(
160+
hook['name'],
161+
'.' * (
162+
COLS -
163+
len(hook['name']) -
164+
len(no_files_msg) -
165+
len(skipped_msg) -
166+
6
167+
),
168+
no_files_msg,
169+
color.format_color(skipped_msg, color.TURQUOISE, args.color),
170+
)
171+
)
172+
return 0
173+
154174
# Print the hook and the dots first in case the hook takes hella long to
155175
# run.
156176
write(
@@ -164,7 +184,7 @@ def _run_single_hook(runner, repository, hook_id, args, write):
164184
retcode, stdout, stderr = repository.run_hook(
165185
runner.cmd_runner,
166186
hook_id,
167-
get_filenames(hook['files'], hook['exclude']),
187+
filenames,
168188
)
169189

170190
if retcode != repository.hooks[hook_id]['expected_return_value']:

tests/commands_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def test_run_all_hooks_failing(repo_with_failing_hook):
235235
0,
236236
True,
237237
),
238+
({}, ('Bash hook', '(no files to check)', 'Skipped'), 0, False),
238239
)
239240
)
240241
def test_run(repo_with_passing_hook, options, outputs, expected_ret, stage):

0 commit comments

Comments
 (0)