Use some bash best practices and simplify hook template

This commit is contained in:
Anthony Sottile 2017-09-21 16:09:45 -07:00
parent eefeaaf844
commit 916ca72bb1

View file

@ -2,9 +2,9 @@
# This is a randomish md5 to identify this script # This is a randomish md5 to identify this script
# 138fd403232d2ddd5efb44317e38bf03 # 138fd403232d2ddd5efb44317e38bf03
pushd `dirname $0` > /dev/null pushd "$(dirname "$0")" >& /dev/null
HERE=`pwd` HERE="$(pwd)"
popd > /dev/null popd >& /dev/null
retv=0 retv=0
args="" args=""
@ -12,35 +12,28 @@ args=""
ENV_PYTHON={sys_executable} ENV_PYTHON={sys_executable}
SKIP_ON_MISSING_CONF={skip_on_missing_conf} SKIP_ON_MISSING_CONF={skip_on_missing_conf}
which pre-commit >& /dev/null if which pre-commit >& /dev/null; then
WHICH_RETV=$? exe="pre-commit"
"$ENV_PYTHON" -c 'import pre_commit.main' >& /dev/null run_args=""
ENV_PYTHON_RETV=$? elif "$ENV_PYTHON" -c 'import pre_commit.main' >& /dev/null; then
python -c 'import pre_commit.main' >& /dev/null exe="$ENV_PYTHON"
PYTHON_RETV=$? run_args="-m pre_commit.main"
elif python -c 'import pre_commit.main' >& /dev/null; then
exe="python"
if (( run_args="-m pre_commit.main"
(WHICH_RETV != 0) && else
(ENV_PYTHON_RETV != 0) &&
(PYTHON_RETV != 0)
)); then
echo '`pre-commit` not found. Did you forget to activate your virtualenv?' echo '`pre-commit` not found. Did you forget to activate your virtualenv?'
exit 1 exit 1
fi fi
# Run the legacy pre-commit if it exists # Run the legacy pre-commit if it exists
if [ -x "$HERE"/{hook_type}.legacy ]; then if [ -x "$HERE"/{hook_type}.legacy ] && ! "$HERE"/{hook_type}.legacy; then
"$HERE"/{hook_type}.legacy
if [ $? -ne 0 ]; then
retv=1 retv=1
fi fi
fi
CONF_FILE="$(git rev-parse --show-toplevel)/{config_file}" CONF_FILE="$(git rev-parse --show-toplevel)/{config_file}"
if [ ! -f $CONF_FILE ]; then if [ ! -f "$CONF_FILE" ]; then
if [ $SKIP_ON_MISSING_CONF = true ] || [ ! -z $PRE_COMMIT_ALLOW_NO_CONFIG ]; then if [ "$SKIP_ON_MISSING_CONF" = true -o ! -z "$PRE_COMMIT_ALLOW_NO_CONFIG" ]; then
echo '`{config_file}` config file not found. Skipping `pre-commit`.' echo '`{config_file}` config file not found. Skipping `pre-commit`.'
exit $retv exit $retv
else else
@ -55,18 +48,7 @@ fi
{hook_specific} {hook_specific}
# Run pre-commit # Run pre-commit
if ((WHICH_RETV == 0)); then if ! "$exe" $run_args run $args --config {config_file}; then
pre-commit run $args --config {config_file}
PRE_COMMIT_RETV=$?
elif ((ENV_PYTHON_RETV == 0)); then
"$ENV_PYTHON" -m pre_commit.main run $args
PRE_COMMIT_RETV=$?
else
python -m pre_commit.main run $args
PRE_COMMIT_RETV=$?
fi
if ((PRE_COMMIT_RETV != 0)); then
retv=1 retv=1
fi fi