]> git.ozlabs.org Git - patchwork/blob - tools/post-receive.hook
Move to a more recent django project structure
[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 # ignore all commits already present in these refs
13 # e.g.,
14 #   EXCLUDE="refs/heads/upstream refs/heads/other-project"
15 #
16 EXCLUDE=""
17
18 PWDIR=/srv/patchwork/patchwork
19
20 do_exit=0
21 trap "do_exit=1" INT
22
23 get_patchwork_hash()
24 {
25   local hash
26   hash=$(git show $1 | python $PWDIR/parser.py --hash)
27   echo $hash
28   test -n "$hash"
29 }
30
31 get_patch_id()
32 {
33   local id
34   id=$($PWDIR/bin/pwclient info -h $1 2>/dev/null \
35     | sed -rne 's,- id[[:space:]]*: ,,p')
36   echo $id
37   test -n "$id"
38 }
39
40 set_patch_state()
41 {
42   $PWDIR/bin/pwclient update -s $2 -c $3 $1 2>&1
43 }
44
45 update_patches()
46 {
47   local cnt; cnt=0
48   for rev in $(git rev-parse --not ${EXCLUDE} |
49                git rev-list --stdin --no-merges --reverse ${1}..${2}); do
50     if [ "$do_exit" = 1 ]; then
51       echo "I: exiting..." >&2
52       break
53     fi
54     hash=$(get_patchwork_hash $rev) \
55       || { echo "E: failed to hash rev $rev." >&2; continue; }
56     id=$(get_patch_id $hash) \
57       || { echo "E: failed to find patch for rev $rev." >&2; continue; }
58     reason="$(set_patch_state $id $3 $rev)" \
59       || { echo "E: failed to update patch #$id${reason:+: $reason}." >&2; continue; }
60     echo "I: patch #$id updated using rev $rev." >&2
61     cnt=$(($cnt + 1))
62   done
63   echo "I: $cnt patch(es) updated to state $3." >&2
64 }
65
66 while read oldrev newrev refname; do
67   found=0
68   for i in $STATE_MAP; do
69     key="${i%:*}"
70     if [ "$key" = "$refname" ]; then
71       update_patches $oldrev $newrev ${i#*:}
72       found=1
73       break
74     fi
75   done
76   if [ $found -eq 0 ]; then
77     echo "E: STATE_MAP has no mapping for branch $refname" >&2
78   fi
79 done