From bffa58753d39dacefa05fdbc5411d8ce1c8afafa Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 5 Jul 2018 12:49:01 -0700 Subject: [PATCH] hook paths are only computed in install_uninstall --- pre_commit/commands/install_uninstall.py | 13 +++++++++---- pre_commit/runner.py | 3 --- tests/commands/install_uninstall_test.py | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index 6b2d16f5..f5947de7 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -6,6 +6,7 @@ import logging import os.path import sys +from pre_commit import git from pre_commit import output from pre_commit.repository import repositories from pre_commit.util import cmd_output @@ -29,6 +30,11 @@ TEMPLATE_START = '# start templated\n' TEMPLATE_END = '# end templated\n' +def _hook_paths(git_root, hook_type): + pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type) + return pth, '{}.legacy'.format(pth) + + def is_our_script(filename): if not os.path.exists(filename): return False @@ -48,8 +54,7 @@ def install( ) return 1 - hook_path = runner.get_hook_path(hook_type) - legacy_path = hook_path + '.legacy' + hook_path, legacy_path = _hook_paths(runner.git_root, hook_type) mkdirp(os.path.dirname(hook_path)) @@ -102,8 +107,8 @@ def install_hooks(runner, store): def uninstall(runner, hook_type='pre-commit'): """Uninstall the pre-commit hooks.""" - hook_path = runner.get_hook_path(hook_type) - legacy_path = hook_path + '.legacy' + hook_path, legacy_path = _hook_paths(runner.git_root, hook_type) + # If our file doesn't exist or it isn't ours, gtfo. if not os.path.exists(hook_path) or not is_our_script(hook_path): return 0 diff --git a/pre_commit/runner.py b/pre_commit/runner.py index c172d3fc..53107007 100644 --- a/pre_commit/runner.py +++ b/pre_commit/runner.py @@ -34,6 +34,3 @@ class Runner(object): @cached_property def config(self): return load_config(self.config_file_path) - - def get_hook_path(self, hook_type): - return os.path.join(git.get_git_dir(self.git_root), 'hooks', hook_type) diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 6aa9c7fa..7345cfbd 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -640,7 +640,7 @@ def test_commit_msg_integration_passing( def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store): runner = Runner(commit_msg_repo, C.CONFIG_FILE) - hook_path = runner.get_hook_path('commit-msg') + hook_path = os.path.join(commit_msg_repo, '.git/hooks/commit-msg') mkdirp(os.path.dirname(hook_path)) with io.open(hook_path, 'w') as hook_file: hook_file.write(