Use either pre-commit or python -m pre_commit.main.

This commit is contained in:
Anthony Sottile 2014-06-17 17:01:51 -07:00
parent fe29f334e8
commit 5e8a6414cd
3 changed files with 102 additions and 10 deletions

View file

@ -9,13 +9,23 @@ import stat
# This is used to identify the hook file we install
IDENTIFYING_HASH = 'd8ee923c46731b42cd95cc869add4062'
PREVIOUS_IDENTIFYING_HASHES = [
'd8ee923c46731b42cd95cc869add4062',
]
IDENTIFYING_HASH = '4d9958c90bc262f47553e2c073f14cfe'
def is_our_pre_commit(filename):
return IDENTIFYING_HASH in io.open(filename).read()
def is_previous_pre_commit(filename):
contents = io.open(filename).read()
return any(hash in contents for hash in PREVIOUS_IDENTIFYING_HASHES)
def make_executable(filename):
original_mode = os.stat(filename).st_mode
os.chmod(
@ -33,7 +43,8 @@ def install(runner, overwrite=False):
# If we have an existing hook, move it to pre-commit.legacy
if (
os.path.exists(runner.pre_commit_path) and
not is_our_pre_commit(runner.pre_commit_path)
not is_our_pre_commit(runner.pre_commit_path) and
not is_previous_pre_commit(runner.pre_commit_path)
):
os.rename(runner.pre_commit_path, runner.pre_commit_legacy_path)
@ -58,9 +69,17 @@ def install(runner, overwrite=False):
def uninstall(runner):
"""Uninstall the pre-commit hooks."""
if os.path.exists(runner.pre_commit_path):
os.remove(runner.pre_commit_path)
print('pre-commit uninstalled')
# If our file doesn't exist or it isn't ours, gtfo.
if (
not os.path.exists(runner.pre_commit_path) or (
not is_our_pre_commit(runner.pre_commit_path) and
not is_previous_pre_commit(runner.pre_commit_path)
)
):
return 0
os.remove(runner.pre_commit_path)
print('pre-commit uninstalled')
if os.path.exists(runner.pre_commit_legacy_path):
os.rename(runner.pre_commit_legacy_path, runner.pre_commit_path)

View file

@ -1,13 +1,17 @@
#!/usr/bin/env bash
# This is a randomish md5 to identify this script
# d8ee923c46731b42cd95cc869add4062
# 4d9958c90bc262f47553e2c073f14cfe
HERE=$(dirname $(readlink -f "$0"))
retv=0
which pre-commit > /dev/null
if [ $? -ne 0 ]; then
which pre-commit >& /dev/null
WHICH_RETV=$?
python -c 'import pre_commit.main' >& /dev/null
PYTHON_RETV=$?
if [ $WHICH_RETV -ne 0 ] && [ $PYTHON_RETV -ne 0 ]; then
echo '`pre-commit` not found. Did you forget to activate your virtualenv?'
exit 1
fi
@ -23,8 +27,15 @@ fi
# Run pre-commit
pre-commit
if [ $? -ne 0 ]; then
if [ $WHICH_RETV -eq 0 ]; then
pre-commit
PRE_COMMIT_RETV=$?
else
python -m pre_commit.main
PRE_COMMIT_RETV=$?
fi
if [ $PRE_COMMIT_RETV -ne 0 ]; then
retv=1
fi