stagit-hook

Maintain stagit post-update git hooks
Log | Files | Refs | README

stagit-hook (1542B)


      1 #!/bin/sh
      2 
      3 err() {
      4 	echo "$@" 1>&2
      5 	exit 1
      6 }
      7 
      8 # Lists repositories we want in the index, sorted by most recent
      9 # commit. While it's not the best way, any repository with the
     10 # post-update hook is listed. (So long as you haven't changed the
     11 # hooks directory.)
     12 hooked() (
     13 	find "$HOME" -maxdepth 3 -path '*/hooks/post-update' | sed s./hooks/post-update$.. |
     14 		while read dir
     15 	do (
     16 		cd "$dir"
     17 		git show -s --format="%ct	$dir" ||
     18 			err "$dir: Unable to get repo information"
     19 	) done | sort -rn | cut -f2-
     20 )
     21 
     22 index() {
     23 	hooked | xargs stagit-index > "$basedir"/index.html
     24 }
     25 
     26 repo() {
     27 	abs="$(readlink -f "$1")"
     28 	nam="$(basename "$abs")"
     29 	dir="$basedir"/"$nam" 
     30 	mkdir -p "$dir"
     31 	cd "$dir"
     32 	stagit -u "$baseurl" "$abs"
     33 }
     34 
     35 loadconfig() {
     36 	if test -f "$rc"
     37 	then
     38 		while read key val
     39 		do
     40 			case $key in
     41 			(basedir) basedir="$val" ;;
     42 			(baseurl) baseurl="$val" ;;
     43 			(*) err 'Invalid configuration'
     44 			esac
     45 		done < "$rc"
     46 	fi
     47 }
     48 
     49 rc=/etc/stagit-hook
     50 host="$(hostname)"
     51 basedir=/var/www/htdocs/"$host"/projects/
     52 baseurl=https://"$host"/projects/
     53 
     54 loadconfig
     55 
     56 if test $# -gt 0
     57 then
     58 	case "$1" in
     59 	(install)
     60 		shift
     61 		stagit-hook-install "$@"
     62 		"$0" reindex "$@"
     63 		exit ;;
     64 	(reindex)
     65 		shift
     66 		for d in "$@"
     67 		do
     68 			repo "$d"
     69 		done
     70 		index
     71 		exit ;;
     72 	(*)
     73 		err 'usage: stagit-hook [repo ...]'
     74 	esac
     75 fi
     76 
     77 printenv GIT_DIR >/dev/null ||
     78 	err 'error: Expected to be run as a git hook'
     79 
     80 err="$(mktemp -t stagit-hook-errXXXXXX)"
     81 trap 'rm -f "$err"' EXIT
     82 {
     83 	repo "$GIT_DIR"
     84 	index
     85 } > "$err" 2>&1 || {
     86 	logger -t stagit-hook -p error < "$err"
     87 	exit 1
     88 }