githooks

My git server hooks

git clone git://git.lin.moe/githooks.git

 1#!/bin/sh
 2
 3: ${SOFT_SERVE_GIT_PUBLIC_URL="git://git.lin.moe"}
 4: ${SOFT_SERVE_DATA_PATH="/var/lib/soft-serve"}
 5: ${WEBROOT="/srv/http/git"}
 6
 7function msg() {
 8	local color=${2:-green}
 9	case "$color" in
10		red) color="31";;
11		green) color="32";;
12		yellow) color="33";;
13		blue) color="34";;
14		gray) color="37";;
15		*) color="32";;
16	esac
17	printf "\033[1;%sm>>>\033[1;0m %s\n" "$color" "$1" | xargs >&2
18}
19
20reponames=""
21for export_file in $(find "$SOFT_SERVE_DATA_PATH/repos" -type f -name git-daemon-export-ok); do
22	repo_dir=$(dirname "$export_file")
23	reponame=${repo_dir#"$SOFT_SERVE_DATA_PATH/repos/"}
24	reponames="$reponames $reponame"
25done
26
27for reponame in $reponames; do
28	msg "generate $reponame"
29	WEBROOT="$WEBROOT" \
30		SOFT_SERVE_DATA_PATH="$SOFT_SERVE_DATA_PATH" \
31		SOFT_SERVE_REPO_PATH="$SOFT_SERVE_DATA_PATH/repos/$reponame" \
32		SOFT_SERVE_GIT_PUBLIC_URL="$SOFT_SERVE_GIT_PUBLIC_URL" \
33		SOFT_SERVE_REPO_NAME="${reponame%'.git'}" \
34		$(dirname $0)/post-receive
35done
36
37for state_file in $(find "$WEBROOT" -type f -name .tree); do
38	repo_dir=$(dirname "$state_file")
39	reponame=${repo_dir#"$WEBROOT/"}
40	case " $reponames " in
41		*" $reponame.git "*)
42		# Repo is active, do nothing
43		;;
44
45		*)
46		# Repo is stale, safe cleanup
47		msg "Removing stale webroot repo: $reponame" yellow
48		if [ -n "$repo_dir" ] && [ "$repo_dir" != "/" ] && [ "$repo_dir" != "$WEBROOT" ]; then
49			rm -rf "$repo_dir"
50		fi
51		;;
52	esac
53done