]> git.ozlabs.org Git - patchwork/blob - tools/post-receive.hook
tools: Add a simple sample Git post-receive hook
[patchwork] / tools / post-receive.hook
1 #!/bin/bash
2 #
3 # Git post-receive hook to update Patchwork patches after Git pushes
4 #
5 # Copyright © 2010 martin f. krafft <madduck@madduck.net>
6 # Released under the terms of the Artistic Licence 2.0
7 #
8 set -eu
9
10 #TODO: the state map should really live in the repo's git-config
11 STATE_MAP="refs/heads/master:Accepted"
12
13 PWDIR=/srv/patchwork/apps/patchwork
14
15 do_exit=0
16 trap "do_exit=1" INT
17
18 get_patchwork_hash()
19 {
20   local hash
21   hash=$(git show $1 | python $PWDIR/parser.py --hash)
22   echo $hash
23   test -n "$hash"
24 }
25
26 get_patch_id()
27 {
28   local id
29   id=$($PWDIR/bin/pwclient view -h $1 2>/dev/null \
30     | sed -rne 's,X-Patchwork-Id: ,,p')
31   echo $id
32   test -n "$id"
33 }
34
35 set_patch_state()
36 {
37   $PWDIR/bin/pwclient update -s $2 -c $3 $1 2>&1
38 }
39
40 update_patches()
41 {
42   local cnt; cnt=0
43   for rev in $(git rev-list --no-merges --reverse ${1}^..${2}); do
44     if [ "$do_exit" = 1 ]; then
45       echo "I: exiting..." >&2
46       break
47     fi
48     hash=$(get_patchwork_hash $rev) \
49       || { echo "E: failed to hash rev $rev." >&2; continue; }
50     id=$(get_patch_id $hash) \
51       || { echo "E: failed to find patch for rev $rev." >&2; continue; }
52     reason="$(set_patch_state $id $3 $rev)" \
53       || { echo "E: failed to update patch #$id${reason:+: $reason}." >&2; continue; }
54     echo "I: patch #$id updated using rev $rev." >&2
55     cnt=$(($cnt + 1))
56   done
57   echo "I: $cnt patch(es) updated to state $3." >&2
58 }
59
60 while read oldrev newrev refname; do
61   found=0
62   for i in $STATE_MAP; do
63     key="${i%:*}"
64     if [ "$key" = "$refname" ]; then
65       update_patches $oldrev $newrev ${i#*:}
66       found=1
67       break
68     fi
69   done
70   if [ $found -eq 0 ]; then
71     echo "E: no mapping for refname $key" >&2
72   fi
73 done