Skip to content

Commit 9411499

Browse files
Lucas-Casottile
authored andcommitted
Making it possible to invoke pre-commit run --files some.file from a subdirectory of the repository
1 parent 8948624 commit 9411499

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

pre_commit/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pre_commit import color
1010
from pre_commit import five
11+
from pre_commit import git
1112
from pre_commit.commands.autoupdate import autoupdate
1213
from pre_commit.commands.clean import clean
1314
from pre_commit.commands.install_uninstall import install
@@ -110,7 +111,8 @@ def main(argv=None):
110111
help='Run on all the files in the repo. Implies --no-stash.',
111112
)
112113
run_mutex_group.add_argument(
113-
'--files', nargs='*', help='Specific filenames to run hooks on.',
114+
'--files', nargs='*', default=[],
115+
help='Specific filenames to run hooks on.',
114116
)
115117

116118
help = subparsers.add_parser(
@@ -122,6 +124,11 @@ def main(argv=None):
122124
if len(argv) == 0:
123125
argv = ['run']
124126
args = parser.parse_args(argv)
127+
if args.command == 'run':
128+
args.files = [
129+
os.path.relpath(os.path.abspath(filename), git.get_root())
130+
for filename in args.files
131+
]
125132

126133
if args.command == 'help':
127134
if args.help_cmd:

pre_commit/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, git_root):
2424
def create(cls):
2525
"""Creates a PreCommitRunner by doing the following:
2626
- Finds the root of the current git repository
27-
- chdirs to that directory
27+
- chdir to that directory
2828
"""
2929
root = git.get_root()
3030
os.chdir(root)

tests/commands/run_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _do_run(repo, args, environ={}):
8383
runner = Runner(repo)
8484
write_mock = mock.Mock()
8585
write_fn = functools.partial(sys_stdout_write_wrapper, stream=write_mock)
86-
ret = run(runner, args, write=write_fn, environ=environ)
86+
with cwd(runner.git_root): # replicates Runner.create behaviour
87+
ret = run(runner, args, write=write_fn, environ=environ)
8788
printed = get_write_mock_output(write_mock)
8889
return ret, printed
8990

0 commit comments

Comments
 (0)