Skip to content

Commit b0a4446

Browse files
committed
Merge pull request pre-commit#235 from pre-commit/hooks_dir_not_there_234
Handle when the hooks directory is not there on install. Resolves pre-commit#234.
2 parents b4bc5e4 + a97cb38 commit b0a4446

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
4848
hook_path = runner.get_hook_path(hook_type)
4949
legacy_path = hook_path + '.legacy'
5050

51+
if not os.path.exists(os.path.dirname(hook_path)):
52+
os.makedirs(os.path.dirname(hook_path))
53+
5154
# If we have an existing hook, move it to pre-commit.legacy
5255
if (
5356
os.path.exists(hook_path) and

tests/commands/install_uninstall_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import os.path
77
import re
8+
import shutil
89
import subprocess
910
import sys
1011

@@ -78,6 +79,15 @@ def test_install_pre_commit(tmpdir_factory):
7879
assert pre_push_contents == expected_contents
7980

8081

82+
def test_install_hooks_directory_not_present(tmpdir_factory):
83+
path = git_dir(tmpdir_factory)
84+
# Simulate some git clients which don't make .git/hooks #234
85+
shutil.rmtree(os.path.join(path, '.git', 'hooks'))
86+
runner = Runner(path)
87+
install(runner)
88+
assert os.path.exists(runner.pre_commit_path)
89+
90+
8191
def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory):
8292
path = git_dir(tmpdir_factory)
8393
runner = Runner(path)

0 commit comments

Comments
 (0)