From d8d7893cf77edeecbd22c5551a068e0664ed79a2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 16 Sep 2014 17:27:40 -0700 Subject: [PATCH] Add ability to pass filenames as arguments. --- pre_commit/commands/run.py | 7 +++++-- pre_commit/main.py | 12 ++++++++---- tests/commands/run_test.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 1f1ee853..7d930ef0 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -49,7 +49,9 @@ def _print_user_skipped(hook, write, args): def _run_single_hook(runner, repository, hook, args, write, skips=set()): - if args.all_files: + if args.files: + get_filenames = git.get_files_matching(lambda: args.files) + elif args.all_files: get_filenames = git.get_all_files_matching elif git.is_in_merge_conflict(): get_filenames = git.get_conflicted_files_matching @@ -136,7 +138,8 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ): logger.error('Unmerged files. Resolve before committing.') return 1 - if args.no_stash or args.all_files: + # Don't stash if specified or files are specified + if args.no_stash or args.all_files or args.files: ctx = noop_context() else: ctx = staged_files_only(runner.cmd_runner) diff --git a/pre_commit/main.py b/pre_commit/main.py index fb1642bb..a8134b2d 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -55,10 +55,6 @@ def main(argv=None): run_parser = subparsers.add_parser('run', help='Run hooks.') run_parser.add_argument('hook', nargs='?', help='A single hook-id to run') - run_parser.add_argument( - '--all-files', '-a', action='store_true', default=False, - help='Run on all the files in the repo. Implies --no-stash.', - ) run_parser.add_argument( '--color', default='auto', type=color.use_color, help='Whether to use color in output. Defaults to `auto`', @@ -70,6 +66,14 @@ def main(argv=None): run_parser.add_argument( '--verbose', '-v', action='store_true', default=False, ) + run_mutex_group = run_parser.add_mutually_exclusive_group(required=False) + run_mutex_group.add_argument( + '--all-files', '-a', action='store_true', default=False, + help='Run on all the files in the repo. Implies --no-stash.', + ) + run_mutex_group.add_argument( + '--files', nargs='*', help='Specific filenames to run hooks on.', + ) help = subparsers.add_parser( 'help', help='Show help for a specific command.' diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 2f22c166..3a83a60c 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -43,13 +43,17 @@ def get_write_mock_output(write_mock): def _get_opts( all_files=False, + files=(), color=False, verbose=False, hook=None, no_stash=False, ): + # These are mutually exclusive + assert not (all_files and files) return auto_namedtuple( all_files=all_files, + files=files, color=color, verbose=verbose, hook=hook, @@ -100,6 +104,12 @@ def test_run_all_hooks_failing( 0, True, ), + ( + {'files': ('foo.py',), 'verbose': True}, + ('foo.py'), + 0, + True, + ), ({}, ('Bash hook', '(no files to check)', 'Skipped'), 0, False), ) )