1#!/bin/sh
2#
3# INDEX build tinderbox script.  Build an INDEX for all supported MidnightBSD branches
4# using the latest value of OSVERSION according to the src trees.  If the build
5# fails, yowl about it on ${REPORT_ADDRESS}  If not, copy the index to www.freebsd.org so
6# that 'make fetchindex' sees it.
7#
8# When INDEX is broken, assemble the list of committers who touched files
9# on the most recent 'git pull', and put those committers "on the hook".
10# These committers all stay on the hook until INDEX is buildable again.
11
12# --------------------------------------------------------
13# Change these!
14
15GIT=/usr/local/bin/git
16SVN=/usr/local/bin/svn
17
18# Address for success/failure reports
19REPORT_ADDRESS=root@localhost
20
21# Address for script errors
22ERROR_ADDRESS=root@localhost
23
24# Location of ports tree and source trees
25export BASEDIR=/home/tindex
26export PORTSDIR=${BASEDIR}/mports
27export SRCDIR1=${BASEDIR}/src.1
28export SRCDIR2=${BASEDIR}/src.2
29export SRCDIR3=${BASEDIR}/src.3
30export OUTDIR=${BASEDIR}/out
31
32# Target architecture if not set in the environment
33if [ "${ARCH}" = "" ]; then
34	export ARCH=i386
35	export MACHINE_ARCH=i386
36fi
37
38# --------------------------------------------------------
39
40blame() {
41  # Find out who is responsible for current version of file $1
42
43  ${GIT} log --no-patch --max-count=1 --format='%ce' -- $1
44}
45
46indexfail() {
47  BRANCH=$1
48
49  # Leave a cookie behind so that we know when the index is fixed
50  touch ${PORTSDIR}/broken.${BRANCH}
51
52  (
53    echo "INDEX build failed with errors:";
54    len=$(wc -l index.out | awk '{print $1}')
55    if [ "$len" -gt "40" ]; then
56      head -20 index.out
57      echo "[...]"
58      tail -20 index.out
59    else
60      cat index.out
61    fi
62
63    len=$(wc -l index.err | awk '{print $1}')
64    if [ "$len" -gt "40" ]; then
65      head -20 index.err
66      echo "[...]"
67      tail -20 index.err
68    else
69      cat index.err
70    fi
71
72    # Find out which committers are on the hook
73
74    commits=$(${GIT} diff --name-only ${OLD_HEAD})
75    for i in ${commits}; do
76	blame $i >> ${PORTSDIR}/hook
77    done
78    sort -u ${PORTSDIR}/hook > ${PORTSDIR}/hook.new
79    mv ${PORTSDIR}/hook.new ${PORTSDIR}/hook
80    echo
81    echo "Committers on the hook:"
82    tr -s '\n' ' ' < ${PORTSDIR}/hook
83    echo
84    echo
85    echo "Most recent Git update was:";
86    (IFS=""; echo ${commits})
87  ) | mail -s "INDEX build failed for ${BRANCH}" ${REPORT_ADDRESS}
88  exit 1
89}
90
91checkfixed() {
92  BRANCH=$1
93
94  # If the cookie exists that means that this is the first build for which the
95  # INDEX succeeded, so announce this.
96  if [ -e ${PORTSDIR}/broken.${BRANCH} ]; then
97    rm -f ${PORTSDIR}/broken.${BRANCH}
98    mail -s "INDEX now builds successfully on ${BRANCH}" ${REPORT_ADDRESS} < /dev/null
99  fi
100}
101
102# Sanitize the environment so that the indexes aren't customized by the
103# local machine settinge
104export __MAKE_CONF=/dev/null
105export PORT_DBDIR=/nonexistent
106export PKG_DBDIR=/nonexistent
107export LOCALBASE=/nonexistent
108export INDEX_PRISTINE=1
109export INDEX_JOBS=3
110export INDEX_QUIET=1
111
112# First update the source trees to get current OSVERSION
113${GIT} -C ${SRCDIR1} pull --rebase -q
114OSVERSION1=$(awk '/^#define[[:blank:]]__MidnightBSD_version/ {print $3}' < ${SRCDIR1}/sys/sys/param.h)
115
116${GIT} -C ${SRCDIR2} pull --rebase -q
117OSVERSION2=$(awk '/^#define[[:blank:]]__MidnightBSD_version/ {print $3}' < ${SRCDIR2}/sys/sys/param.h)
118
119cd ${PORTSDIR}
120rm -f INDEX-1 INDEX-1.bz2 INDEX-2 INDEX-2.bz2 INDEX-3 INDEX-3.bz2
121OLD_HEAD=$(${GIT} rev-parse HEAD)
122if ! ${GIT} pull --ff-only > git.log 2>&1 ; then
123  (echo "Git update failed with conflicts:";
124    cat git.log) | mail -s "Ports Git update failed" ${ERROR_ADDRESS}
125    exit 1
126fi
127
128for branch in 1.x 2.x 3.x; do
129    release=$(echo $branch | sed -e 's,.x,,')
130
131    eval _osver=\$OSVERSION${release}
132    eval _uname_r="$(( ${_osver} / 100000 )).0-RELEASE"
133    export OSVERSION=${_osver}
134    export UNAME_r=${_uname_r}
135
136    echo "Building INDEX for ${branch} with OSVERSION=${OSVERSION}"
137    cd ${PORTSDIR}
138    ( (make index 2> index.err) > index.out) || indexfail ${branch}
139    if [ -s index.err ]; then
140        indexfail ${branch}
141    fi
142    checkfixed ${branch}
143
144    bzip2 -kf ${PORTSDIR}/INDEX-${release}
145    mv ${PORTSDIR}/INDEX-${release} ${PORTSDIR}/INDEX-${release}.bz2 ${OUTDIR}
146done
147
148# All indexes built successfully, clear the hook
149rm -f ${PORTSDIR}/hook
150