xref: /aosp_15_r20/external/flashrom/util/git-hooks/install.sh (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1#!/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
6[ -z "${root}" ] || \
7     { echo "Not in root directory. Can only run from git root." >&2 ; exit 1 ; }
8
9src=util/git-hooks/ # relative to root
10hooks=$(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
20
21for h in $hooks; do
22	# Test if hook is not already installed, i.e. doesn't point at the wrapper
23	if [ ! "${dst}$h" -ef "${src}wrapper.sh" ]; then
24		# preserve custom hooks if any
25		if [ -e "${dst}$h" ]; then
26			mv "${dst}$h" "${dst}$h.local"
27		fi
28		ln -s "${rel}${src}wrapper.sh" "${dst}$h"
29	fi
30done
31