From 42fe699ef490ad3d81971d74ea2e9d8e9a4b75a2 Mon Sep 17 00:00:00 2001 From: Simon Segerblom Rex Date: Thu, 14 Mar 2024 13:59:50 +0100 Subject: [PATCH] Fix coverage and path to repo dir --- pre_commit/commands/install_uninstall.py | 5 +++-- tests/commands/install_uninstall_test.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index c5795236..1223a43f 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -75,9 +75,10 @@ def _install_hook_script( git_dir = git_dir if git_dir is not None else git.get_git_common_dir() # If the hooks directory links to a directory outside the # git repo we shouldn't try to mess with it + repo_dir = os.path.dirname(os.path.realpath(git_dir)) if os.path.commonpath( - [os.path.realpath(git_dir), os.path.realpath(hook_path)], - ) != os.path.realpath(git_dir): + [repo_dir, os.path.realpath(hook_path)] + ) != repo_dir: logger.error( 'Cowardly refusing to install hook script to a directory ' 'outside of the git repo.\n' diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 40099dd1..0673ff2a 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -109,6 +109,14 @@ def test_install_hooks_dead_symlink(in_git_dir, store): assert hook.exists() +def test_install_hooks_symlink_inside_git_repo(in_git_dir, store): + hooks_dir = in_git_dir.join('.git/hooks') + os.symlink(in_git_dir.join('hooks').ensure_dir().strpath, hooks_dir.strpath) + hook = hooks_dir.join('pre-commit') + assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0 + assert hook.exists() + + def test_install_hooks_symlink_outisde_git_repo(in_git_dir, store): hooks_dir = in_git_dir.join('.git/hooks') os.symlink(in_git_dir.join('../hooks').ensure_dir().strpath, hooks_dir.strpath)