git-post-receive-irc.sh - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       git-post-receive-irc.sh (1579B)
       ---
            1 #!/bin/sh
            2 # generic git post-receive hook.
            3 # change the config options below and call this script in your post-receive
            4 # hook or symlink it.
            5 #
            6 # usage: $0 [name]
            7 #
            8 # if name is not set the basename of the current directory is used,
            9 # this is the directory of the repo when called from the post-receive script.
           10 
           11 set -x
           12 
           13 name="$1"
           14 if test "${name}" = ""; then
           15         name="$(basename $(pwd))"
           16 fi
           17 
           18 # config
           19 # paths must be absolute.
           20 reposdir="/home/src/src"
           21 dir="${reposdir}/${name}"
           22 infile='/tmp/annna/irc.freenode.net/#bitreich-scm/in'
           23 # /config
           24 
           25 if ! test -d "${dir}"; then
           26         echo "${dir} does not exist" >&2
           27         exit 1
           28 fi
           29 cd "${dir}" || exit 1
           30 
           31 # portable reverse lines: Linux: tac, BSD: tail -r.
           32 reverse() {
           33         awk 'END { for (i--; i >= 0; i--) { print l[i]; } } { l[i++] = $0; }'
           34 }
           35 
           36 tmpfile="$(mktemp)"
           37 chmod 666 "${tmpfile}"
           38 
           39 # detect git push -f
           40 force=0
           41 while read -r old new ref; do
           42         test "${old}" = "0000000000000000000000000000000000000000" && continue
           43 
           44         hasrevs="$(git rev-list "${old}" "^${new}" | sed 1q)"
           45         if test -n "${hasrevs}"; then
           46                 force=1
           47                 break
           48         fi
           49         git show -q \
           50                 --pretty="format:[${name}] %s by %an <%ae>: gopher://git.codemadness.org/1/${name}/commit/%H.gph%n" \
           51                 "${old}...${new}" >> "${tmpfile}"
           52 done
           53 
           54 # if not git push -f, print the new commits line by line in the channel.
           55 if test "${force}" = 1; then
           56         printf '[%s] %s did a git push -f, bad bad %s!: gopher://git.codemadness.org/1/%s\n' \
           57                 "${name}" "${USER}" "${USER}" "${name}" >> "${infile}"
           58 else
           59         # reverse order (old to new, top to bottom):
           60         reverse < "${tmpfile}" >> "${infile}"
           61 fi
           62 
           63 rm -f "${tmpfile}"