mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 20:40:08 -06:00
Add first pass at migration mode.
This commit is contained in:
@@ -1,24 +1,44 @@
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import io
|
||||
import os
|
||||
import os.path
|
||||
import pkg_resources
|
||||
import stat
|
||||
|
||||
# This is used to identify the hook file we install
|
||||
IDENTIFYING_HASH = 'd8ee923c46731b42cd95cc869add4062'
|
||||
|
||||
|
||||
def is_our_pre_commit(filename):
|
||||
return IDENTIFYING_HASH in io.open(filename).read()
|
||||
|
||||
|
||||
def make_executable(filename):
|
||||
original_mode = os.stat(filename).st_mode
|
||||
os.chmod(
|
||||
filename,
|
||||
original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
|
||||
)
|
||||
|
||||
|
||||
def install(runner):
|
||||
"""Install the pre-commit hooks."""
|
||||
pre_commit_file = pkg_resources.resource_filename(
|
||||
'pre_commit', 'resources/pre-commit-hook',
|
||||
)
|
||||
|
||||
# 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)
|
||||
):
|
||||
os.rename(runner.pre_commit_path, runner.pre_commit_path + '.legacy')
|
||||
|
||||
with open(runner.pre_commit_path, 'w') as pre_commit_file_obj:
|
||||
pre_commit_file_obj.write(open(pre_commit_file).read())
|
||||
|
||||
original_mode = os.stat(runner.pre_commit_path).st_mode
|
||||
os.chmod(
|
||||
runner.pre_commit_path,
|
||||
original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
|
||||
)
|
||||
make_executable(runner.pre_commit_path)
|
||||
|
||||
print('pre-commit installed at {0}'.format(runner.pre_commit_path))
|
||||
return 0
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
# This is a randomish md5 to identify this script
|
||||
# d8ee923c46731b42cd95cc869add4062
|
||||
|
||||
HERE=$(dirname $(readlink -f "$0"))
|
||||
|
||||
retv=0
|
||||
|
||||
which pre-commit > /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
@@ -6,4 +12,20 @@ if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
# Run the legacy pre-commit if it exists
|
||||
if [ -x "$HERE"/pre-commit.legacy ]; then
|
||||
"$HERE"/pre-commit.legacy
|
||||
if [ $? -ne 0 ]; then
|
||||
retv=1
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# Run pre-commit
|
||||
pre-commit
|
||||
if [ $? -ne 0 ]; then
|
||||
retv=1
|
||||
fi
|
||||
|
||||
exit $retv
|
||||
|
||||
Reference in New Issue
Block a user