Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bffa587

Browse files
committed
hook paths are only computed in install_uninstall
1 parent d04823e commit bffa587

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os.path
77
import sys
88

9+
from pre_commit import git
910
from pre_commit import output
1011
from pre_commit.repository import repositories
1112
from pre_commit.util import cmd_output
@@ -29,6 +30,11 @@
2930
TEMPLATE_END = '# end templated\n'
3031

3132

33+
def _hook_paths(git_root, hook_type):
34+
pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type)
35+
return pth, '{}.legacy'.format(pth)
36+
37+
3238
def is_our_script(filename):
3339
if not os.path.exists(filename):
3440
return False
@@ -48,8 +54,7 @@ def install(
4854
)
4955
return 1
5056

51-
hook_path = runner.get_hook_path(hook_type)
52-
legacy_path = hook_path + '.legacy'
57+
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
5358

5459
mkdirp(os.path.dirname(hook_path))
5560

@@ -102,8 +107,8 @@ def install_hooks(runner, store):
102107

103108
def uninstall(runner, hook_type='pre-commit'):
104109
"""Uninstall the pre-commit hooks."""
105-
hook_path = runner.get_hook_path(hook_type)
106-
legacy_path = hook_path + '.legacy'
110+
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
111+
107112
# If our file doesn't exist or it isn't ours, gtfo.
108113
if not os.path.exists(hook_path) or not is_our_script(hook_path):
109114
return 0

pre_commit/runner.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,3 @@ def config_file_path(self):
3434
@cached_property
3535
def config(self):
3636
return load_config(self.config_file_path)
37-
38-
def get_hook_path(self, hook_type):
39-
return os.path.join(git.get_git_dir(self.git_root), 'hooks', hook_type)

tests/commands/install_uninstall_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ def test_commit_msg_integration_passing(
640640
def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
641641
runner = Runner(commit_msg_repo, C.CONFIG_FILE)
642642

643-
hook_path = runner.get_hook_path('commit-msg')
643+
hook_path = os.path.join(commit_msg_repo, '.git/hooks/commit-msg')
644644
mkdirp(os.path.dirname(hook_path))
645645
with io.open(hook_path, 'w') as hook_file:
646646
hook_file.write(

0 commit comments

Comments
 (0)