Make install_hooks check if git hook is available
When git hook isn't available, we can still have
install_hooks.sh copy the commands to .git/hooks. However, this means
that the hooks will not remain up-to-date as this repository is updated,
so the user is warned.
Bug: b/187094215
Change-Id: I7401b78d7e6afa12fec691efed7c21b62d987422
diff --git a/install_hooks.sh b/install_hooks.sh
index 822a324..681e1a1 100755
--- a/install_hooks.sh
+++ b/install_hooks.sh
@@ -6,20 +6,32 @@
# 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
- git config --unset-all hook.${hook}.command
- git config --add hook.${hook}.command "${dir}/${hook}"
- # Sanity check: there should be a single hook per line and it should point to the
- # git-hooks repository.
- git hook list "${hook}"
+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