Skip to content

Commit 72b61a8

Browse files
author
Dmitriy Kunitskiy
committed
fix for issue 246
1 parent 2c70476 commit 72b61a8

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727

2828

2929
def is_our_pre_commit(filename):
30+
if not os.path.exists(filename):
31+
return False
3032
return IDENTIFYING_HASH in io.open(filename).read()
3133

3234

3335
def is_previous_pre_commit(filename):
36+
if not os.path.exists(filename):
37+
return False
3438
contents = io.open(filename).read()
3539
return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)
3640

@@ -53,7 +57,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
5357

5458
# If we have an existing hook, move it to pre-commit.legacy
5559
if (
56-
os.path.exists(hook_path) and
60+
os.path.lexists(hook_path) and
5761
not is_our_pre_commit(hook_path) and
5862
not is_previous_pre_commit(hook_path)
5963
):

testing/util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ def platform_supports_pcre():
7373
not platform_supports_pcre(),
7474
reason='grep -P is not supported on this platform',
7575
)
76+
77+
xfailif_no_symlink = pytest.mark.xfail(
78+
not hasattr(os, 'symlink'),
79+
reason='Symlink is not supported on this platform',
80+
)

tests/commands/install_uninstall_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pre_commit.util import resource_filename
2525
from testing.fixtures import git_dir
2626
from testing.fixtures import make_consuming_repo
27+
from testing.util import xfailif_no_symlink
2728

2829

2930
def test_is_not_our_pre_commit():
@@ -88,6 +89,15 @@ def test_install_hooks_directory_not_present(tmpdir_factory):
8889
assert os.path.exists(runner.pre_commit_path)
8990

9091

92+
@xfailif_no_symlink
93+
def test_install_hooks_dead_symlink(tmpdir_factory):
94+
path = git_dir(tmpdir_factory)
95+
os.symlink('/fake/baz', os.path.join(path, '.git', 'hooks', 'pre-commit'))
96+
runner = Runner(path)
97+
install(runner)
98+
assert os.path.exists(runner.pre_commit_path)
99+
100+
91101
def test_uninstall_does_not_blow_up_when_not_there(tmpdir_factory):
92102
path = git_dir(tmpdir_factory)
93103
runner = Runner(path)

0 commit comments

Comments
 (0)