blob: fda0098f680ee62fdf771ab1224bdfe020d09c12 [file] [log] [blame]
Stefan Tauner76347082016-11-27 17:45:49 +01001#!/bin/sh -e
2
3root=$(git rev-parse --show-cdup 2>/dev/null) || \
4 { echo "Not under git control. Cannot install git hooks." >&2 ; exit 0 ; }
5
Nico Huber4164c542017-12-22 02:09:18 +01006[ -z "${root}" ] || \
7 { echo "Not in root directory. Can only run from git root." >&2 ; exit 1 ; }
8
Stefan Tauner76347082016-11-27 17:45:49 +01009src=util/git-hooks/ # relative to root
Nico Huber4164c542017-12-22 02:09:18 +010010hooks=$(cd "${src}" && git ls-files -c | grep -Ev 'install.sh|wrapper.sh')
11
12if [ "$(git rev-parse --git-path 2>/dev/null)" = "--git-path" ]; then
13 # very old git, we have to guess
14 dst=".git/hooks/"
15 rel="../../"
16else
17 dst=$(git rev-parse --git-path hooks/)
18 rel=$(git rev-parse --prefix "${dst}" --show-cdup)
19fi
Stefan Tauner76347082016-11-27 17:45:49 +010020
21for h in $hooks; do
22 # Test if hook is not already installed, i.e. doesn't point at the wrapper
Nico Huber4164c542017-12-22 02:09:18 +010023 if [ ! "${dst}$h" -ef "${src}wrapper.sh" ]; then
Stefan Tauner76347082016-11-27 17:45:49 +010024 # preserve custom hooks if any
25 if [ -e "${dst}$h" ]; then
26 mv "${dst}$h" "${dst}$h.local"
27 fi
Nico Huber4164c542017-12-22 02:09:18 +010028 ln -s "${rel}${src}wrapper.sh" "${dst}$h"
Stefan Tauner76347082016-11-27 17:45:49 +010029 fi
30done