contrib / ciabot / ciabot.shon commit Merge branch 'tr/maint-apply-non-git-patch-parsefix' (ad76feb)
   1#!/bin/sh
   2# Distributed under the terms of the GNU General Public License v2
   3# Copyright (c) 2006 Fernando J. Pereda <ferdy@gentoo.org>
   4# Copyright (c) 2008 Natanael Copa <natanael.copa@gmail.com>
   5# Copyright (c) 2010 Eric S. Raymond <esr@thyrsus.com>
   6# Assistance and review by Petr Baudis, author of ciabot.pl,
   7# is gratefully acknowledged.
   8#
   9# This is a version 3.x of ciabot.sh; use -V to find the exact
  10# version.  Versions 1 and 2 were shipped in 2006 and 2008 and are not
  11# version-stamped.  The version 2 maintainer has passed the baton.
  12#
  13# Note: This script should be considered obsolete.
  14# There is a faster, better-documented rewrite in Python: find it as ciabot.py
  15# Use this only if your hosting site forbids Python hooks.
  16# It requires: git(1), hostname(1), cut(1), sendmail(1), and wget(1).
  17#
  18# Originally based on Git ciabot.pl by Petr Baudis.
  19# This script contains porcelain and porcelain byproducts.
  20#
  21# usage: ciabot.sh [-V] [-n] [-p projectname] [refname commit]
  22#
  23# This script is meant to be run either in a post-commit hook or in an
  24# update hook. Try it with -n to see the notification mail dumped to
  25# stdout and verify that it looks sane. With -V it dumps its version
  26# and exits.
  27#
  28# In post-commit, run it without arguments. It will query for
  29# current HEAD and the latest commit ID to get the information it
  30# needs.
  31#
  32# In update, you have to call it once per merged commit:
  33#
  34#       refname=$1
  35#       oldhead=$2
  36#       newhead=$3
  37#       for merged in $(git rev-list ${oldhead}..${newhead} | tac) ; do
  38#               /path/to/ciabot.sh ${refname} ${merged}
  39#       done
  40#
  41# The reason for the tac call is that git rev-list emits commits from
  42# most recent to least - better to ship notifactions from oldest to newest.
  43#
  44# Configuration variables affecting this script:
  45#
  46# ciabot.project = name of the project
  47# ciabot.repo = name of the project repo for gitweb/cgit purposes
  48# ciabot.revformat = format in which the revision is shown
  49#
  50# ciabot.project defaults to the directory name of the repository toplevel.
  51# ciabot.repo defaults to ciabot.project lowercased.
  52#
  53# This means that in the normal case you need not do any configuration at all,
  54# but setting the project name will speed it up slightly.
  55#
  56# The revformat variable may have the following values
  57# raw -> full hex ID of commit
  58# short -> first 12 chars of hex ID
  59# describe = -> describe relative to last tag, falling back to short
  60# The default is 'describe'.
  61#
  62# Note: the shell ancestors of this script used mail, not XML-RPC, in
  63# order to avoid stalling until timeout when the CIA XML-RPC server is
  64# down. It is unknown whether this is still an issue in 2010, but
  65# XML-RPC would be annoying to do from sh in any case. (XML-RPC does
  66# have the advantage that it guarantees notification of multiple commits
  67# shpped from an update in their actual order.)
  68#
  69
  70# The project as known to CIA. You can set this with a -p option,
  71# or let it default to the directory name of the repo toplevel.
  72project=$(git config --get ciabot.project)
  73
  74if [ -z $project ]
  75then
  76    here=`pwd`;
  77    while :; do
  78        if [ -d $here/.git ]
  79        then
  80            project=`basename $here`
  81            break
  82        elif [ $here = '/' ]
  83        then
  84            echo "ciabot.sh: no .git below root!"
  85            exit 1
  86        fi
  87        here=`dirname $here`
  88    done
  89fi
  90
  91# Name of the repo for gitweb/cgit purposes
  92repo=$(git config --get ciabot.repo)
  93[ -z $repo] && repo=$(echo "${project}" | tr '[A-Z]' '[a-z]')
  94
  95# What revision format do we want in the summary?
  96revformat=$(git config --get ciabot.revformat)
  97
  98# Fully qualified domain name of the repo host.  You can hardwire this
  99# to make the script faster. The -f option works under Linux and FreeBSD,
 100# but not OpenBSD and NetBSD. But under OpenBSD and NetBSD,
 101# hostname without options gives the FQDN.
 102if hostname -f >/dev/null 2>&1
 103then
 104    hostname=`hostname -f`
 105else
 106    hostname=`hostname`
 107fi
 108
 109# Changeset URL prefix for your repo: when the commit ID is appended
 110# to this, it should point at a CGI that will display the commit
 111# through gitweb or something similar. The defaults will probably
 112# work if you have a typical gitweb/cgit setup.
 113#urlprefix="http://${host}/cgi-bin/gitweb.cgi?p=${repo};a=commit;h="
 114urlprefix="http://${host}/cgi-bin/cgit.cgi/${repo}/commit/?id="
 115
 116#
 117# You probably will not need to change the following:
 118#
 119
 120# Identify the script. The 'generator' variable should change only
 121# when the script itself gets a new home and maintainer.
 122generator="http://www.catb.org/~esr/ciabot/ciabot.sh"
 123version=3.5
 124
 125# Addresses for the e-mail
 126from="CIABOT-NOREPLY@${hostname}"
 127to="cia@cia.vc"
 128
 129# SMTP client to use - may need to edit the absolute pathname for your system
 130sendmail="sendmail -t -f ${from}"
 131
 132#
 133# No user-serviceable parts below this line:
 134#
 135
 136# Should include all places sendmail is likely to lurk.
 137PATH="$PATH:/usr/sbin/"
 138
 139mode=mailit
 140while getopts pnV opt
 141do
 142    case $opt in
 143        p) project=$2; shift ; shift ;;
 144        n) mode=dumpit; shift ;;
 145        V) echo "ciabot.sh: version $version"; exit 0; shift ;;
 146    esac
 147done
 148
 149# Cough and die if user has not specified a project
 150if [ -z "$project" ]
 151then
 152    echo "ciabot.sh: no project specified, bailing out." >&2
 153    exit 1
 154fi
 155
 156if [ $# -eq 0 ] ; then
 157        refname=$(git symbolic-ref HEAD 2>/dev/null)
 158        merged=$(git rev-parse HEAD)
 159else
 160        refname=$1
 161        merged=$2
 162fi
 163
 164# This tries to turn your gitwebbish URL into a tinyurl so it will take up
 165# less space on the IRC notification line. Some repo sites (I'm looking at
 166# you, berlios.de!) forbid wget calls for security reasons.  On these,
 167# the code will fall back to the full un-tinyfied URL.
 168longurl=${urlprefix}${merged}
 169url=$(wget -O - -q http://tinyurl.com/api-create.php?url=${longurl} 2>/dev/null)
 170if [ -z "$url" ]; then
 171        url="${longurl}"
 172fi
 173
 174refname=${refname##refs/heads/}
 175
 176case $revformat in
 177raw) rev=$merged ;;
 178short) rev='' ;;
 179*) rev=$(git describe ${merged} 2>/dev/null) ;;
 180esac
 181[ -z ${rev} ] && rev=$(echo "$merged" | cut -c 1-12)
 182
 183# We discard the part of the author's address after @.
 184# Might be nice to ship the full email address, if not
 185# for spammers' address harvesters - getting this wrong
 186# would make the freenode #commits channel into harvester heaven.
 187author=$(git log -1 '--pretty=format:%an <%ae>' $merged)
 188author=$(echo "$author" | sed -n -e '/^.*<\([^@]*\).*$/s--\1-p')
 189
 190logmessage=$(git log -1 '--pretty=format:%s' $merged)
 191ts=$(git log -1 '--pretty=format:%at' $merged)
 192files=$(git diff-tree -r --name-only ${merged} | sed -e '1d' -e 's-.*-<file>&</file>-')
 193
 194out="
 195<message>
 196  <generator>
 197    <name>CIA Shell client for Git</name>
 198    <version>${version}</version>
 199    <url>${generator}</url>
 200  </generator>
 201  <source>
 202    <project>${project}</project>
 203    <branch>$repo:${refname}</branch>
 204  </source>
 205  <timestamp>${ts}</timestamp>
 206  <body>
 207    <commit>
 208      <author>${author}</author>
 209      <revision>${rev}</revision>
 210      <files>
 211        ${files}
 212      </files>
 213      <log>${logmessage} ${url}</log>
 214      <url>${url}</url>
 215    </commit>
 216  </body>
 217</message>"
 218
 219if [ "$mode" = "dumpit" ]
 220then
 221    sendmail=cat
 222fi
 223
 224${sendmail} << EOM
 225Message-ID: <${merged}.${author}@${project}>
 226From: ${from}
 227To: ${to}
 228Content-type: text/xml
 229Subject: DeliverXML
 230${out}
 231EOM
 232
 233# vim: set tw=70 :