git.lucas.co / gitsite
the static git browser that builds this site
git clone https://git.lucas.co/gitsite.git

install-hooks.sh (1.7K)

 1 #!/bin/sh
 2 # Symlink the hooks into every repo listed in repos.conf: post-commit-push as
 3 # its post-commit hook and pre-commit-scan as its pre-commit hook. Safe to
 4 # re-run: an existing symlink is refreshed, and a hook that is a real file
 5 # (someone's own) is left alone and reported.
 6 #
 7 # Work trees are found by NAME under $GIT_WORK_ROOTS, since repos.conf records
 8 # only where a repo is mirrored from. A name that resolves to nothing fails
 9 # the run rather than being skipped.
10 set -eu
11 
12 BASE=$(dirname "$(readlink -f "$0")")
13 # hook-name=file, one pair per word.
14 HOOKS="pre-commit=pre-commit-scan post-commit=post-commit-push"
15 WORK_ROOTS="${GIT_WORK_ROOTS:-$HOME/projects/cce $HOME/projects}"
16 FAILED=""
17 
18 names=$(grep -v '^#' "$BASE/repos.conf" | grep -v '^[[:space:]]*$' | cut -d'|' -f1)
19 for name in $names; do
20     wt=""
21     for root in $WORK_ROOTS; do
22         if [ -d "$root/$name/.git" ]; then wt="$root/$name"; break; fi
23     done
24     if [ -z "$wt" ]; then
25         echo "!! $name: no work tree under $WORK_ROOTS"
26         FAILED="$FAILED $name"
27         continue
28     fi
29     hooks=$(git -C "$wt" rev-parse --path-format=absolute --git-path hooks)
30     mkdir -p "$hooks"
31     bad=""
32     for pair in $HOOKS; do
33         dest="$hooks/${pair%%=*}"
34         if [ -e "$dest" ] && [ ! -L "$dest" ]; then
35             echo "!! $name: $dest exists and is not a symlink -- left alone"
36             bad=1
37             continue
38         fi
39         ln -sfn "$BASE/${pair#*=}" "$dest"
40     done
41     if [ -n "$bad" ]; then
42         FAILED="$FAILED $name"
43         continue
44     fi
45     echo "   $name"
46 done
47 
48 echo
49 if [ -n "$FAILED" ]; then
50     echo "done, with failures:$FAILED"
51     exit 1
52 fi
53 echo "done: pre-commit and post-commit hooks installed in every repo"