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

Skip to content

Commit 5e8a641

Browse files
committed
Use either pre-commit or python -m pre_commit.main.
1 parent fe29f33 commit 5e8a641

3 files changed

Lines changed: 102 additions & 10 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@
99

1010

1111
# This is used to identify the hook file we install
12-
IDENTIFYING_HASH = 'd8ee923c46731b42cd95cc869add4062'
12+
PREVIOUS_IDENTIFYING_HASHES = [
13+
'd8ee923c46731b42cd95cc869add4062',
14+
]
15+
16+
17+
IDENTIFYING_HASH = '4d9958c90bc262f47553e2c073f14cfe'
1318

1419

1520
def is_our_pre_commit(filename):
1621
return IDENTIFYING_HASH in io.open(filename).read()
1722

1823

24+
def is_previous_pre_commit(filename):
25+
contents = io.open(filename).read()
26+
return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)
27+
28+
1929
def make_executable(filename):
2030
original_mode = os.stat(filename).st_mode
2131
os.chmod(
@@ -33,7 +43,8 @@ def install(runner, overwrite=False):
3343
# If we have an existing hook, move it to pre-commit.legacy
3444
if (
3545
os.path.exists(runner.pre_commit_path) and
36-
not is_our_pre_commit(runner.pre_commit_path)
46+
not is_our_pre_commit(runner.pre_commit_path) and
47+
not is_previous_pre_commit(runner.pre_commit_path)
3748
):
3849
os.rename(runner.pre_commit_path, runner.pre_commit_legacy_path)
3950

@@ -58,9 +69,17 @@ def install(runner, overwrite=False):
5869

5970
def uninstall(runner):
6071
"""Uninstall the pre-commit hooks."""
61-
if os.path.exists(runner.pre_commit_path):
62-
os.remove(runner.pre_commit_path)
63-
print('pre-commit uninstalled')
72+
# If our file doesn't exist or it isn't ours, gtfo.
73+
if (
74+
not os.path.exists(runner.pre_commit_path) or (
75+
not is_our_pre_commit(runner.pre_commit_path) and
76+
not is_previous_pre_commit(runner.pre_commit_path)
77+
)
78+
):
79+
return 0
80+
81+
os.remove(runner.pre_commit_path)
82+
print('pre-commit uninstalled')
6483

6584
if os.path.exists(runner.pre_commit_legacy_path):
6685
os.rename(runner.pre_commit_legacy_path, runner.pre_commit_path)

pre_commit/resources/pre-commit-hook

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#!/usr/bin/env bash
22
# This is a randomish md5 to identify this script
3-
# d8ee923c46731b42cd95cc869add4062
3+
# 4d9958c90bc262f47553e2c073f14cfe
44

55
HERE=$(dirname $(readlink -f "$0"))
66

77
retv=0
88

9-
which pre-commit > /dev/null
10-
if [ $? -ne 0 ]; then
9+
which pre-commit >& /dev/null
10+
WHICH_RETV=$?
11+
python -c 'import pre_commit.main' >& /dev/null
12+
PYTHON_RETV=$?
13+
14+
if [ $WHICH_RETV -ne 0 ] && [ $PYTHON_RETV -ne 0 ]; then
1115
echo '`pre-commit` not found. Did you forget to activate your virtualenv?'
1216
exit 1
1317
fi
@@ -23,8 +27,15 @@ fi
2327

2428

2529
# Run pre-commit
26-
pre-commit
27-
if [ $? -ne 0 ]; then
30+
if [ $WHICH_RETV -eq 0 ]; then
31+
pre-commit
32+
PRE_COMMIT_RETV=$?
33+
else
34+
python -m pre_commit.main
35+
PRE_COMMIT_RETV=$?
36+
fi
37+
38+
if [ $PRE_COMMIT_RETV -ne 0 ]; then
2839
retv=1
2940
fi
3041

tests/commands/install_uninstall_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
import stat
1111
from plumbum import local
1212

13+
from pre_commit.commands.install_uninstall import IDENTIFYING_HASH
14+
from pre_commit.commands.install_uninstall import PREVIOUS_IDENTIFYING_HASHES
1315
from pre_commit.commands.install_uninstall import install
1416
from pre_commit.commands.install_uninstall import is_our_pre_commit
17+
from pre_commit.commands.install_uninstall import is_previous_pre_commit
1518
from pre_commit.commands.install_uninstall import make_executable
1619
from pre_commit.commands.install_uninstall import uninstall
1720
from pre_commit.runner import Runner
@@ -31,6 +34,25 @@ def test_is_our_pre_commit():
3134
) is True
3235

3336

37+
def test_is_not_previous_pre_commit():
38+
assert is_previous_pre_commit('setup.py') is False
39+
40+
41+
def test_is_also_not_previous_pre_commit():
42+
assert is_previous_pre_commit(
43+
pkg_resources.resource_filename(
44+
'pre_commit', 'resources/pre-commit-hook',
45+
)
46+
) is False
47+
48+
49+
def test_is_previous_pre_commit(in_tmpdir):
50+
with io.open('foo', 'w') as foo_file:
51+
foo_file.write(PREVIOUS_IDENTIFYING_HASHES[0])
52+
53+
assert is_previous_pre_commit('foo')
54+
55+
3456
def test_install_pre_commit(tmpdir_factory):
3557
path = git_dir(tmpdir_factory)
3658
runner = Runner(path)
@@ -276,3 +298,43 @@ def test_uninstall_restores_legacy_hooks(tmpdir_factory):
276298
ret, output = _get_commit_output(tmpdir_factory, touch_file='baz')
277299
assert ret == 0
278300
assert EXISTING_COMMIT_RUN.match(output)
301+
302+
303+
def test_replace_old_commit_script(tmpdir_factory):
304+
path = make_consuming_repo(tmpdir_factory, 'script_hooks_repo')
305+
with local.cwd(path):
306+
runner = Runner(path)
307+
308+
# Install a script that looks like our old script
309+
pre_commit_contents = io.open(
310+
pkg_resources.resource_filename(
311+
'pre_commit', 'resources/pre-commit-hook',
312+
)
313+
).read()
314+
new_contents = pre_commit_contents.replace(
315+
IDENTIFYING_HASH, PREVIOUS_IDENTIFYING_HASHES[-1],
316+
)
317+
318+
with io.open(runner.pre_commit_path, 'w') as pre_commit_file:
319+
pre_commit_file.write(new_contents)
320+
make_executable(runner.pre_commit_path)
321+
322+
# Install normally
323+
assert install(runner) == 0
324+
325+
ret, output = _get_commit_output(tmpdir_factory)
326+
assert ret == 0
327+
assert NORMAL_PRE_COMMIT_RUN.match(output)
328+
329+
330+
def test_uninstall_doesnt_remove_not_our_hooks(tmpdir_factory):
331+
path = git_dir(tmpdir_factory)
332+
with local.cwd(path):
333+
runner = Runner(path)
334+
with io.open(runner.pre_commit_path, 'w') as pre_commit_file:
335+
pre_commit_file.write('#!/usr/bin/env bash\necho 1\n')
336+
make_executable(runner.pre_commit_path)
337+
338+
assert uninstall(runner) == 0
339+
340+
assert os.path.exists(runner.pre_commit_path)

0 commit comments

Comments
 (0)