From 941149942da69ff21a9759a76e6e2ebbe5d73f75 Mon Sep 17 00:00:00 2001 From: Lucas Cimon Date: Wed, 3 Feb 2016 09:03:59 +0100 Subject: [PATCH] Making it possible to invoke `pre-commit run --files some.file` from a subdirectory of the repository --- pre_commit/main.py | 9 ++++++++- pre_commit/runner.py | 2 +- tests/commands/run_test.py | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index ce16acde..7092a5a8 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -8,6 +8,7 @@ import pkg_resources from pre_commit import color from pre_commit import five +from pre_commit import git from pre_commit.commands.autoupdate import autoupdate from pre_commit.commands.clean import clean from pre_commit.commands.install_uninstall import install @@ -110,7 +111,8 @@ def main(argv=None): 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.', + '--files', nargs='*', default=[], + help='Specific filenames to run hooks on.', ) help = subparsers.add_parser( @@ -122,6 +124,11 @@ def main(argv=None): if len(argv) == 0: argv = ['run'] args = parser.parse_args(argv) + if args.command == 'run': + args.files = [ + os.path.relpath(os.path.abspath(filename), git.get_root()) + for filename in args.files + ] if args.command == 'help': if args.help_cmd: diff --git a/pre_commit/runner.py b/pre_commit/runner.py index 88028939..35ab3427 100644 --- a/pre_commit/runner.py +++ b/pre_commit/runner.py @@ -24,7 +24,7 @@ class Runner(object): def create(cls): """Creates a PreCommitRunner by doing the following: - Finds the root of the current git repository - - chdirs to that directory + - chdir to that directory """ root = git.get_root() os.chdir(root) diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 363743fb..6058df5d 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -83,7 +83,8 @@ def _do_run(repo, args, environ={}): runner = Runner(repo) write_mock = mock.Mock() write_fn = functools.partial(sys_stdout_write_wrapper, stream=write_mock) - ret = run(runner, args, write=write_fn, environ=environ) + with cwd(runner.git_root): # replicates Runner.create behaviour + ret = run(runner, args, write=write_fn, environ=environ) printed = get_write_mock_output(write_mock) return ret, printed