Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 1 | #!/bin/sh -e |
| 2 | |
| 3 | root=$(git rev-parse --show-cdup 2>/dev/null) || \ |
| 4 | { echo "Not under git control. Cannot install git hooks." >&2 ; exit 0 ; } |
| 5 | |
Nico Huber | 4164c54 | 2017-12-22 02:09:18 +0100 | [diff] [blame] | 6 | [ -z "${root}" ] || \ |
| 7 | { echo "Not in root directory. Can only run from git root." >&2 ; exit 1 ; } |
| 8 | |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 9 | src=util/git-hooks/ # relative to root |
Nico Huber | 4164c54 | 2017-12-22 02:09:18 +0100 | [diff] [blame] | 10 | hooks=$(cd "${src}" && git ls-files -c | grep -Ev 'install.sh|wrapper.sh') |
| 11 | |
| 12 | if [ "$(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="../../" |
| 16 | else |
| 17 | dst=$(git rev-parse --git-path hooks/) |
| 18 | rel=$(git rev-parse --prefix "${dst}" --show-cdup) |
| 19 | fi |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 20 | |
| 21 | for h in $hooks; do |
| 22 | # Test if hook is not already installed, i.e. doesn't point at the wrapper |
Nico Huber | 4164c54 | 2017-12-22 02:09:18 +0100 | [diff] [blame] | 23 | if [ ! "${dst}$h" -ef "${src}wrapper.sh" ]; then |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 24 | # preserve custom hooks if any |
| 25 | if [ -e "${dst}$h" ]; then |
| 26 | mv "${dst}$h" "${dst}$h.local" |
| 27 | fi |
Nico Huber | 4164c54 | 2017-12-22 02:09:18 +0100 | [diff] [blame] | 28 | ln -s "${rel}${src}wrapper.sh" "${dst}$h" |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 29 | fi |
| 30 | done |