Fixes to invirtibuilder for case of missing package in super-repo, missing
[invirt/packages/invirt-dev.git] / git-hooks / sub / zephyr-post-receive
1 #!/bin/bash
2 #
3 # This script is run after receive-pack has accepted a pack and the
4 # repository has been updated.  It is passed arguments in through stdin
5 # in the form
6 #  <oldrev> <newrev> <refname>
7 # For example:
8 #  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
9
10 base=build.hooks.post_commit.zephyr
11
12 class=$(invirt-getconf "$base.class" 2>/dev/null)
13 instance=$(invirt-getconf "$base.instance" 2>/dev/null)
14 zsig=$(invirt-getconf "$base.zsig" 2>/dev/null)
15 color=$(invirt-getconf "$base.color" 2>/dev/null)
16 maxlines=`git config --int zephyr.maxlines 2>/dev/null || echo 50`
17
18 # Git hooks are always called with PWD == GIT_DIR.
19 repo=${PWD#/srv/git/}
20 repo=${repo%/.git}
21
22 if [ "${color:-true}" = "true" ]; then
23     usecolor="--color"
24 else
25     usecolor=""
26 fi
27
28 if [ -z "$zsig" ]; then
29     if [ -e "$GIT_DIR/description" ]; then
30         zsig=`cat "$GIT_DIR/description"`
31     fi
32     if [ -z "$zsig" ] || \
33         [ "$zsig" = "Unnamed repository; edit this file to name it for gitweb." ] || \
34         [ "$zsig" = "Unnamed repository; edit this file 'description' to name the repository." ]; then
35         zsig=$(basename "$(cd "$GIT_DIR" && pwd)")
36         if [ "$zsig" = ".git" ]; then
37             zsig=$(basename "$(cd "$GIT_DIR/.." && pwd)")
38         fi
39     fi
40 fi
41
42 if [ -z "$class" ]; then
43   echo "I don't know where to send a commit zephyr!" >&2
44   echo "Please provide a value for $base.class in" >&2
45   echo "your invirt config file." >&2
46   exit 1
47 fi
48
49 let max=10
50 check_max () {
51   if ! let --max; then
52     zwrite -c "$class" -i "${instance:-git}" -s "Aperture Science Emergency Intelligence Incinerator" -d \
53       -m 'Aborting zephyr hook to prevent zwrite flood.'
54     exit 0
55   fi
56 }
57
58 while read oldrev newrev refname; do
59   if [ "$oldrev" = "0000000000000000000000000000000000000000" ]; then
60     check_max
61     # dammit git
62     zwrite -c "$class" -i "$repo" -s "${zsig:-Git}: $refname" -d \
63       -m "New branch ${refname#refs/heads/} created, currently at $newrev."
64     continue
65   fi
66   while read rev; do
67     check_max
68     shortrev=`git log -1 --pretty=format:%h "$rev"`
69     lines=`git show -M "$rev" | wc -l`
70     if [ $lines -lt $maxlines ]; then
71         stat=""
72     else
73         stat="--stat"
74     fi
75     (git show -M $stat $usecolor "$rev" |
76      sed -e 's/@/@@/g' \
77          -e 's/}/@(})/g' \
78          -e 's/\e\[m/}@{/g' \
79          -e 's/\e\[1m/}@b{/g' \
80          -e 's/\e\[33m/@color(yellow)/g' \
81          -e 's/\e\[31m/@color(red)/g' \
82          -e 's/\e\[32m/@color(green)/g' \
83          -e 's/\e\[36m/@color(cyan)/g' \
84          -e '1s/^/@{/' \
85          -e '$s/$/}/') |
86     zwrite -c "$class" -i "${instance:-$repo/$shortrev}" -s "${zsig:-Git}: $refname" -d
87   done < <(git rev-list --first-parent --reverse "$oldrev..$newrev")
88 done