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

Skip to content

Commit d8d7893

Browse files
committed
Add ability to pass filenames as arguments.
1 parent cfd86d5 commit d8d7893

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

pre_commit/commands/run.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def _print_user_skipped(hook, write, args):
4949

5050

5151
def _run_single_hook(runner, repository, hook, args, write, skips=set()):
52-
if args.all_files:
52+
if args.files:
53+
get_filenames = git.get_files_matching(lambda: args.files)
54+
elif args.all_files:
5355
get_filenames = git.get_all_files_matching
5456
elif git.is_in_merge_conflict():
5557
get_filenames = git.get_conflicted_files_matching
@@ -136,7 +138,8 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
136138
logger.error('Unmerged files. Resolve before committing.')
137139
return 1
138140

139-
if args.no_stash or args.all_files:
141+
# Don't stash if specified or files are specified
142+
if args.no_stash or args.all_files or args.files:
140143
ctx = noop_context()
141144
else:
142145
ctx = staged_files_only(runner.cmd_runner)

pre_commit/main.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ def main(argv=None):
5555

5656
run_parser = subparsers.add_parser('run', help='Run hooks.')
5757
run_parser.add_argument('hook', nargs='?', help='A single hook-id to run')
58-
run_parser.add_argument(
59-
'--all-files', '-a', action='store_true', default=False,
60-
help='Run on all the files in the repo. Implies --no-stash.',
61-
)
6258
run_parser.add_argument(
6359
'--color', default='auto', type=color.use_color,
6460
help='Whether to use color in output. Defaults to `auto`',
@@ -70,6 +66,14 @@ def main(argv=None):
7066
run_parser.add_argument(
7167
'--verbose', '-v', action='store_true', default=False,
7268
)
69+
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
70+
run_mutex_group.add_argument(
71+
'--all-files', '-a', action='store_true', default=False,
72+
help='Run on all the files in the repo. Implies --no-stash.',
73+
)
74+
run_mutex_group.add_argument(
75+
'--files', nargs='*', help='Specific filenames to run hooks on.',
76+
)
7377

7478
help = subparsers.add_parser(
7579
'help', help='Show help for a specific command.'

tests/commands/run_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ def get_write_mock_output(write_mock):
4343

4444
def _get_opts(
4545
all_files=False,
46+
files=(),
4647
color=False,
4748
verbose=False,
4849
hook=None,
4950
no_stash=False,
5051
):
52+
# These are mutually exclusive
53+
assert not (all_files and files)
5154
return auto_namedtuple(
5255
all_files=all_files,
56+
files=files,
5357
color=color,
5458
verbose=verbose,
5559
hook=hook,
@@ -100,6 +104,12 @@ def test_run_all_hooks_failing(
100104
0,
101105
True,
102106
),
107+
(
108+
{'files': ('foo.py',), 'verbose': True},
109+
('foo.py'),
110+
0,
111+
True,
112+
),
103113
({}, ('Bash hook', '(no files to check)', 'Skipped'), 0, False),
104114
)
105115
)

0 commit comments

Comments
 (0)