From 4da461d90aa55d55859cd3e6160fe2db041db305 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 1 Jan 2019 11:57:06 -0800 Subject: [PATCH] Fix try-repo relpath while in a sub-directory --- pre_commit/main.py | 4 ++++ tests/main_test.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/pre_commit/main.py b/pre_commit/main.py index a5a4a817..99f34070 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -94,12 +94,16 @@ def _adjust_args_and_chdir(args): args.config = os.path.abspath(args.config) if args.command in {'run', 'try-repo'}: args.files = [os.path.abspath(filename) for filename in args.files] + if args.command == 'try-repo' and os.path.exists(args.repo): + args.repo = os.path.abspath(args.repo) os.chdir(git.get_root()) args.config = os.path.relpath(args.config) if args.command in {'run', 'try-repo'}: args.files = [os.path.relpath(filename) for filename in args.files] + if args.command == 'try-repo' and os.path.exists(args.repo): + args.repo = os.path.relpath(args.repo) def main(argv=None): diff --git a/tests/main_test.py b/tests/main_test.py index 83e7d22f..83758bf4 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -47,6 +47,17 @@ def test_adjust_args_and_chdir_non_relative_config(in_git_dir): assert args.config == C.CONFIG_FILE +def test_adjust_args_try_repo_repo_relative(in_git_dir): + in_git_dir.join('foo').ensure_dir().chdir() + + args = Args(command='try-repo', repo='../foo', files=[]) + assert os.path.exists(args.repo) + main._adjust_args_and_chdir(args) + assert os.getcwd() == in_git_dir + assert os.path.exists(args.repo) + assert args.repo == 'foo' + + FNS = ( 'autoupdate', 'clean', 'install', 'install_hooks', 'migrate_config', 'run', 'sample_config', 'uninstall',