From de24712a6fab092c04cd342317ec2d171842b72b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 13 Apr 2014 17:25:05 -0700 Subject: [PATCH] Skip hooks if no files to check. Closes #69. --- pre_commit/commands.py | 22 +++++++++++++++++++++- tests/commands_test.py | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pre_commit/commands.py b/pre_commit/commands.py index 1db112ae..909f8de4 100644 --- a/pre_commit/commands.py +++ b/pre_commit/commands.py @@ -151,6 +151,26 @@ def _run_single_hook(runner, repository, hook_id, args, write): hook = repository.hooks[hook_id] + filenames = get_filenames(hook['files'], hook['exclude']) + if not filenames: + no_files_msg = '(no files to check) ' + skipped_msg = 'Skipped' + write( + '{0}{1}{2}{3}\n'.format( + hook['name'], + '.' * ( + COLS - + len(hook['name']) - + len(no_files_msg) - + len(skipped_msg) - + 6 + ), + no_files_msg, + color.format_color(skipped_msg, color.TURQUOISE, args.color), + ) + ) + return 0 + # Print the hook and the dots first in case the hook takes hella long to # run. write( @@ -164,7 +184,7 @@ def _run_single_hook(runner, repository, hook_id, args, write): retcode, stdout, stderr = repository.run_hook( runner.cmd_runner, hook_id, - get_filenames(hook['files'], hook['exclude']), + filenames, ) if retcode != repository.hooks[hook_id]['expected_return_value']: diff --git a/tests/commands_test.py b/tests/commands_test.py index 11f228b4..1d1cb1ab 100644 --- a/tests/commands_test.py +++ b/tests/commands_test.py @@ -235,6 +235,7 @@ def test_run_all_hooks_failing(repo_with_failing_hook): 0, True, ), + ({}, ('Bash hook', '(no files to check)', 'Skipped'), 0, False), ) ) def test_run(repo_with_passing_hook, options, outputs, expected_ret, stage):