Add first pass at migration mode.

This commit is contained in:
Anthony Sottile
2014-06-16 15:31:22 -07:00
parent 8f3f5c364a
commit 1d8394afd0
3 changed files with 153 additions and 37 deletions

View File

@@ -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

View File

@@ -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