summaryrefslogtreecommitdiffstats
path: root/robot.sh
blob: dee4bf1a6ae2649d09b434230035052bd8cb4fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash -xe

#MAILTO='tip commits list <linux-tip-commits@vger.kernel.org>'
MAILTO='linux-tip-commits@vger.kernel.org'
SENDER='tipbot@zytor.com'

# /usr/sbin needed for sendmail
export PATH=/bin:/usr/bin:/usr/local/bin:/usr/sbin:$HOME/bin

topdir="$HOME/tip"

tmpdir="/tmp/$USER/tipmail"
master="$topdir/mirrors/tip"
slave="$topdir/tip.git"
date=$(date -uR)

max_mails=200

(
    flock -nx 8 || exit 0

    cd "$topdir"

    if [ -f kill/kill ]; then
	exit 0
    fi

    for m in linus tip; do
	GIT_DIR="$topdir/mirrors/$m" git fetch -f || exit 0
    done

    mkdir -m 0700 -p $tmpdir

    rm -rf .tip
    GIT_DIR="$master" git archive --format=tar tip .tip/auto-branches .tip/bot | tar xf -
    cat .tip/auto-branches/* .tip/bot/extra-branches | \
	perl .tip/bot/filter-branches.pl | sort | uniq > branches
    (
	while read branch; do
	    GIT_DIR="$master" git rev-parse --revs-only "$branch"'^{}'
	done
    ) < branches > branches.heads
    sha1sum branches.heads > branches.id.new

    if [ "$(cat branches.id)" != "$(cat branches.id.new)" ]; then
	cd "$slave"
	git checkout mail-merge
	OLD_HEAD=$(git rev-parse HEAD)
	git fetch -f
	git fetch -f linus
	git merge -s ours -m "Mail run on $date" $(cat "$topdir"/branches.heads)
	NEW_HEAD=$(git rev-parse HEAD)

	# Filter out the branches that should be considered baselines and *never* receive
	# announcements.  This may eventually turn into a separate script.
	baselines=$(sed -ne 's:^tree/:refs/remotes/origin/:p' < "$topdir"/branches)

	if [ x"$NEW_HEAD" != x"$OLD_HEAD" ]; then
	    git rev-list --reverse --no-merges ^linus/master ^linus/next $baselines ^$OLD_HEAD $NEW_HEAD \
		> "$tmpdir"/revlist.$$
	    count=$(wc -l < "$tmpdir"/revlist.$$)
	    if [ $count -gt $max_mails ]; then
		# Failsafe!
		LIST_HASH=$(sha256sum < "$tmpdir"/revlist.$$ | awk '{ print $1; }')
		git reset --hard $OLD_HEAD
		LAST_FAILSAFE=$(cat "$topdir"/last_failsafe 2>/dev/null || true)
		if [ x"$LIST_HASH" != x"$LAST_FAILSAFE" ]; then
		    echo "$LIST_HASH" > "$topdir"/last_failsafe
		    # A mostly arbitrary string as long as it doesn't occur anywhere...
		    boundary='----------73zc71qfWjUfy1i6'
		    cat > "$tmpdir"/failsafe.$$ <<EOF
From: tip-bot alert <$SENDER>
To: H. Peter Anvin <hpa@zytor.com>, Ingo Molnar <mingo@kernel.org>, Thomas Gleixner <tglx@linutronix.de>
Subject: tip-bot failsafe: $count emails
User-Agent: tip-bot failsafe
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="$boundary"

--$boundary
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Trying to send $count emails, reverting to $OLD_HEAD

EOF
		    git shortlog --no-walk --format='%h %s' `cat "$tmpdir"/revlist.$$` \
			>> "$tmpdir"/failsafe.$$
		    cat >> "$tmpdir"/failsafe.$$ <<EOF
--$boundary
Content-Type: text/plain; charset=UTF-8; name="commit.lst"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="commit.lst"

EOF
		    cat "$tmpdir"/revlist.$$ >> "$tmpdir"/failsafe.$$
		    cat >> "$tmpdir"/failsafe.$$ <<EOF
--$boundary--
EOF
		    sendmail -i -t < "$tmpdir"/failsafe.$$
		    rm -f "$tmpdir"/failsafe.$$
		fi
		exit 1
	    fi
	    (	
		while read rev; do
		    "$topdir"/robotfmt.pl $rev "$topdir"/branches
		done
	    ) < "$tmpdir"/revlist.$$
	    rm -f "$tmpdir"/revlist.$$
	fi
	cd "$topdir"

	mv -f branches.id.new branches.id
    fi
) 8< "$0"