blob: 681e1a16c36e0968dcb26ea3070f3e86a00489df [file] [log] [blame]
#! /usr/bin/env sh
dir=`realpath $(dirname "$BASH_SOURCE:-0")`
githookdir=`git rev-parse --git-dir`/hooks
# Space separated list of hooks to install
declare -a HOOKS=("pre-commit" "commit-msg")
hookConfigExists="$(git hook list pre-commit 2>/dev/null >> /dev/null ; echo $?)"
for hook in ${HOOKS[@]}; do
if [ ! -f "${dir}/${hook}" ]; then
continue
fi
# Soft remove the old script in .git/hooks so that git doesn't try to run
# multiple hooks.
if [ -f "${githookdir}/${hook}" ]; then
mv "${githookdir}/${hook}" "${githookdir}/${hook}.old"
fi
if [ "$hookConfigExists" -eq "0" ]; then
git config --unset-all hook.${hook}.command
git config --add hook.${hook}.command "${dir}/${hook}"
# There should be a single hook per line and it should point to the
# git-hooks repository.
git hook list "${hook}"
else
cp ${dir}/${hook} ${githookdir}/${hook}
fi
done
if [ "$hookConfigExists" -eq "0" ]; then
echo "If a single command is listed per hook, then this script was succesful."
echo "Checks will be run at pre-submit."
else
echo "The 'git hook' command is not available on this device."
echo "The hooks will be copied to ${githookdir}, but will not automatically remain up-to-date with the git-hooks repository"
done