diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index c84d29b4..47c9484c 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -48,6 +48,9 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): hook_path = runner.get_hook_path(hook_type) legacy_path = hook_path + '.legacy' + if not os.path.exists(os.path.dirname(hook_path)): + os.makedirs(os.path.dirname(hook_path)) + # If we have an existing hook, move it to pre-commit.legacy if ( os.path.exists(hook_path) and diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py index 9e1806e1..ca82c061 100644 --- a/tests/commands/install_uninstall_test.py +++ b/tests/commands/install_uninstall_test.py @@ -5,6 +5,7 @@ import io import os import os.path import re +import shutil import subprocess import sys @@ -78,6 +79,15 @@ def test_install_pre_commit(tmpdir_factory): assert pre_push_contents == expected_contents +def test_install_hooks_directory_not_present(tmpdir_factory): + path = git_dir(tmpdir_factory) + # Simulate some git clients which don't make .git/hooks #234 + shutil.rmtree(os.path.join(path, '.git', 'hooks')) + runner = Runner(path) + install(runner) + assert os.path.exists(runner.pre_commit_path) + + def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path)