Project.pm: do not unlink nonexistent file
[girocco.git] / toolbox / update-all-hooks.sh
blob813b01404d2230a06c157374ae25b87488f934be
1 #!/bin/sh
3 # Update all out-of-date hooks in all current projects and install missing ones
5 # If one or more project names are given, just update those instead
7 set -e
9 . @basedir@/shlib.sh
11 force=
12 dryrun=
13 [ "$1" != "--force" ] && [ "$1" != "-f" ] || { force=1; shift; }
14 [ "$1" != "--dry-run" ] && [ "$1" != "-n" ] || { dryrun=1; shift; }
15 case "$1" in -*) echo "Invalid options: $1" >&2; exit 1;; esac
17 if is_root; then
18 printf '%s\n' "$(basename "$0"): refusing to run as root -- bad things would happen"
19 exit 1
22 umask 002
24 base="${cfg_reporoot%/}"
25 globalhooks="$base/_global/hooks"
26 hookbin="$cfg_basedir/hooks"
27 cmd='cut -d : -f 1 <"$cfg_chroot/etc/group" | grep -v ^_repo'
28 [ $# -eq 0 ] || cmd='printf "%s\n" "$@"'
29 eval "$cmd" |
31 while read -r proj; do
32 proj="${proj#$base/}"
33 proj="${proj%.git}"
34 projdir="$base/$proj.git"
35 [ -d "$projdir" ] || { echo "$proj: does not exist -- skipping"; continue; }
36 ! [ -e "$projdir/.nohooks" ] || { echo "$proj: .nohooks found -- skipping"; continue; }
37 updates=
38 for hook in pre-auto-gc pre-receive post-commit post-receive update; do
39 doln=
40 if [ -f "$projdir/hooks/$hook" ]; then
42 ! [ -L "$projdir/hooks/$hook" ] ||
43 [ "$(readlink "$projdir/hooks/$hook")" != "$globalhooks/$hook" ]
44 then
45 doln=1
46 updates="$updates $hook"
48 elif ! [ -e "$projdir/hooks/$hook" ]; then
49 [ -n "$dryrun" ] || [ -d "$projdir/hooks" ] || mkdir "$projdir/hooks"
50 doln=1
51 updates="$updates +$hook"
53 if [ -z "$dryrun" ] && [ -n "$doln" ]; then
54 ln -sfn "$globalhooks/$hook" "$projdir/hooks/$hook"
56 done
57 for hook in post-update; do
58 if [ -e "$projdir/hooks/$hook" ]; then
59 [ -n "$dryrun" ] || rm -f "$projdir/hooks/$hook"
60 updates="$updates -$hook"
62 done
64 [ -d "$projdir/mob/hooks" ] &&
66 ! [ -L "$projdir/mob/hooks" ] ||
67 [ "$(readlink "$projdir/mob/hooks")" != "../hooks" ]
69 then
70 if [ -z "$dryrun" ]; then
71 [ -L "$projdir/mob/hooks" ] || rm -rf "$projdir/mob/hooks"
72 ln -sfn ../hooks "$projdir/mob/hooks"
74 updates="$updates mob/hooks@ -> ../hooks"
76 [ -z "$updates" ] || echo "$proj:$updates" ${dryrun:+(dryrun)}
77 done