]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/conflict-watch.sh
63f67c545c0a83cd65a937b78c1c6b07dda6b825
[xonotic/xonotic.git] / misc / tools / conflict-watch.sh
1 #!/bin/sh
2
3 set -e
4
5 action=$1
6 outdir=$2
7 repodir=$3
8
9 branches()
10 {
11         git for-each-ref 'refs/remotes' | grep -vE '    refs/remotes/([^/]*/HEAD|.*/archived/.*)$'
12 }
13
14 escape_html()
15 {
16         sed -e 's/&/\&amp;/g; s/</&lt;/g; s/>/&gt;/g'
17 }
18
19 to_rss()
20 {
21         outdir=$1
22         name=$2
23         masterhash=$3
24         masterbranch=$4
25         hash=$5
26         branch=$6
27         repo=$7
28
29         filename=`echo -n "$name" | tr -c 'A-Za-z0-9' '_'`.rss
30         outfilename="$outdir/$filename"
31         masterbranch=`echo -n "$masterbranch" | escape_html`
32         branch=`echo -n "$branch" | escape_html`
33         repo=`echo -n "$repo" | escape_html`
34         if [ -n "$repo" ]; then
35                 repotxt=" in $repo"
36         else
37                 repotxt=
38         fi
39
40         if ! [ -f "$outfilename" ]; then
41                 datetime=`date --rfc-2822`
42                 cat >"$outfilename" <<EOF
43 <?xml version="1.0" encoding="UTF-8" ?>
44 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
45 <channel>
46         <title>XonCW: $name</title>
47         <link>http://git.xonotic.org/</link>
48         <description>Xonotic Conflict Watch for branches by $name</description>
49         <ttl>10800</ttl>
50         <atom:link href="http://nl.git.xonotic.org/xoncw/$filename" rel="self" type="application/rss+xml" />
51         <lastBuildDate>$datetime</lastBuildDate>
52 EOF
53         fi
54         cat >>"$outfilename" <<EOF
55         <item>
56                 <title>$branch$repotxt</title>
57                 <link>http://git.xonotic.org/?p=$repo;a=shortlog;h=refs/heads/$branch</link>
58                 <guid isPermaLink="false">http://nl.git.xonotic.org/xoncw/$filename#$hash</guid>
59                 <description><![CDATA[
60                 Conflicts of $branch at $hash against $masterbranch at $masterhash:
61 EOF
62  
63         echo -n "<pre>" >>"$outfilename"
64         escape_html >>"$outfilename"
65         echo "</pre>" >>"$outfilename"
66
67         cat >>"$outfilename" <<EOF
68                 ]]></description>
69         </item>
70 EOF
71 }
72
73 clear_rss()
74 {
75         datetime=`date --rfc-2822`
76         sed -i -e '/<lastBuildDate>/,$d' "$1"
77         cat <<EOF >>"$1"
78         <lastBuildDate>$datetime</lastBuildDate>
79 EOF
80 }
81
82 finish_rss()
83 {
84         cat <<EOF >>"$1"
85 </channel>
86 </rss>
87 EOF
88 }
89
90 if [ -z "$outdir" ]; then
91         set --
92 fi
93
94 repo=$(
95         (
96                 if [ -n "$repodir" ]; then
97                         cd "$repodir"
98                 fi
99                 git config remote.origin.url | cut -d / -f 4-
100         )
101 )
102
103 case "$action" in
104         --init)
105                 mkdir -p "$outdir"
106                 for f in "$outdir"/*; do
107                         [ -f "$f" ] || continue
108                         clear_rss "$f"
109                 done
110                 ;;
111         --finish)
112                 for f in "$outdir"/*; do
113                         [ -f "$f" ] || continue
114                         finish_rss "$f"
115                 done
116                 ;;
117         --add)
118                 masterhash=$(
119                         (
120                                 if [ -n "$repodir" ]; then
121                                         cd "$repodir"
122                                 fi
123                                 git rev-parse HEAD
124                         )
125                 )
126                 masterbranch=$(
127                         (
128                                 if [ -n "$repodir" ]; then
129                                         cd "$repodir"
130                                 fi
131                                 git symbolic-ref HEAD
132                         )
133                 )
134                 masterbranch=${masterbranch#refs/heads/}
135                 (
136                         if [ -n "$repodir" ]; then
137                                 cd "$repodir"
138                         fi
139                         branches
140                 ) | while read -r HASH TYPE REFNAME; do
141                         echo >&2 -n "$repo $REFNAME..."
142                         out=$(
143                                 (
144                                         if [ -n "$repodir" ]; then
145                                                 cd "$repodir"
146                                         fi
147                                         git reset --hard "$masterhash" >/dev/null 2>&1
148                                         if out=`git merge --no-commit -- "$REFNAME" 2>&1`; then
149                                                 good=true
150                                         else
151                                                 good=false
152                                                 echo "$out"
153                                         fi
154                                         git reset --hard "$masterhash" >/dev/null 2>&1
155                                 )
156                         )
157                         if [ -n "$out" ]; then
158                                 b=${REFNAME#refs/remotes/[!/]*/}
159                                 case "$b" in
160                                         */*)
161                                                 n=${b%%/*}
162                                                 ;;
163                                         *)
164                                                 n=divVerent
165                                                 ;;
166                                 esac
167                                 echo "$out" | to_rss "$outdir" "$n" "$masterhash" "$masterbranch" "$HASH" "$b" "$repo"
168                                 echo >&2 " CONFLICT"
169                         else
170                                 echo >&2 " ok"
171                         fi
172                 done
173                 ;;
174         *)
175                 echo "Usage: $0 --init OUTDIR"
176                 echo "       $0 --add OUTDIR [REPODIR]"
177                 echo "       $0 --finish OUTDIR"
178                 exit 1
179                 ;;
180 esac