]> git.ozlabs.org Git - patchwork/blob - tools/post-receive.hook
Make the post-receive hook more efficient (using pwclient info, not view)
[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 GNU General Public License v2 or later.
7 set -eu
8
9 #TODO: the state map should really live in the repo's git-config
10 STATE_MAP="refs/heads/master:Accepted"
11
12 PWDIR=/srv/patchwork/apps/patchwork
13
14 do_exit=0
15 trap "do_exit=1" INT
16
17 get_patchwork_hash()
18 {
19   local hash
20   hash=$(git show $1 | python $PWDIR/parser.py --hash)
21   echo $hash
22   test -n "$hash"
23 }
24
25 get_patch_id()
26 {
27   local id
28   id=$($PWDIR/bin/pwclient info -h $1 2>/dev/null \
29     | sed -rne 's,- id[[:space:]]*: ,,p')
30   echo $id
31   test -n "$id"
32 }
33
34 set_patch_state()
35 {
36   $PWDIR/bin/pwclient update -s $2 -c $3 $1 2>&1
37 }
38
39 update_patches()
40 {
41   local cnt; cnt=0
42   for rev in $(git rev-list --no-merges --reverse ${1}..${2}); do
43     if [ "$do_exit" = 1 ]; then
44       echo "I: exiting..." >&2
45       break
46     fi
47     hash=$(get_patchwork_hash $rev) \
48       || { echo "E: failed to hash rev $rev." >&2; continue; }
49     id=$(get_patch_id $hash) \
50       || { echo "E: failed to find patch for rev $rev." >&2; continue; }
51     reason="$(set_patch_state $id $3 $rev)" \
52       || { echo "E: failed to update patch #$id${reason:+: $reason}." >&2; continue; }
53     echo "I: patch #$id updated using rev $rev." >&2
54     cnt=$(($cnt + 1))
55   done
56   echo "I: $cnt patch(es) updated to state $3." >&2
57 }
58
59 while read oldrev newrev refname; do
60   found=0
61   for i in $STATE_MAP; do
62     key="${i%:*}"
63     if [ "$key" = "$refname" ]; then
64       update_patches $oldrev $newrev ${i#*:}
65       found=1
66       break
67     fi
68   done
69   if [ $found -eq 0 ]; then
70     echo "E: no mapping for refname $key" >&2
71   fi
72 done