X Tutup
Skip to content

Commit 4da461d

Browse files
committed
Fix try-repo relpath while in a sub-directory
1 parent b31cb22 commit 4da461d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pre_commit/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,16 @@ def _adjust_args_and_chdir(args):
9494
args.config = os.path.abspath(args.config)
9595
if args.command in {'run', 'try-repo'}:
9696
args.files = [os.path.abspath(filename) for filename in args.files]
97+
if args.command == 'try-repo' and os.path.exists(args.repo):
98+
args.repo = os.path.abspath(args.repo)
9799

98100
os.chdir(git.get_root())
99101

100102
args.config = os.path.relpath(args.config)
101103
if args.command in {'run', 'try-repo'}:
102104
args.files = [os.path.relpath(filename) for filename in args.files]
105+
if args.command == 'try-repo' and os.path.exists(args.repo):
106+
args.repo = os.path.relpath(args.repo)
103107

104108

105109
def main(argv=None):

tests/main_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
4747
assert args.config == C.CONFIG_FILE
4848

4949

50+
def test_adjust_args_try_repo_repo_relative(in_git_dir):
51+
in_git_dir.join('foo').ensure_dir().chdir()
52+
53+
args = Args(command='try-repo', repo='../foo', files=[])
54+
assert os.path.exists(args.repo)
55+
main._adjust_args_and_chdir(args)
56+
assert os.getcwd() == in_git_dir
57+
assert os.path.exists(args.repo)
58+
assert args.repo == 'foo'
59+
60+
5061
FNS = (
5162
'autoupdate', 'clean', 'install', 'install_hooks', 'migrate_config', 'run',
5263
'sample_config', 'uninstall',

0 commit comments

Comments
 (0)
X Tutup