]> git.madduck.net Git - code/fbi-announce.git/blob - fbi-announce.sh

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

trim message in case url is empty
[code/fbi-announce.git] / fbi-announce.sh
1 #!/bin/sh
2 #
3 # fbi-announce — announce Git commits to the FBI mail-to-IRC gateway
4 #
5 # Copyright (c) 2006 Fernando J. Pereda <ferdy@gentoo.org>
6 # Copyright (c) 2008 Joerg Jaspert <joerg@debian.org>
7 # Copyright © 2009–2010 martin f. krafft <madduck@debian.org>
8 #
9 # Distributed under the terms of the GNU General Public License v2
10 #
11 # This script is based on Git ciabot.pl by Petr Baudis.
12 #
13 #
14 #
15 # The script is meant to be configured using git-config with the following keys:
16 #   hooks.fbi-project         the project name known to FBI
17 #   hooks.fbi-component       an optional component name
18 #   hooks.fbi-url-template    the template to the gitweb URL (containing @@sha1@@)
19 #   hooks.fbi-emailsender     the sender address to use in the email
20 #   hooks.fbi-emailcc         carbon-copy recipients (comma-separated) if the message
21 #   hooks.fbi-refwidth        the width of references to show (default 7)
22 #   hooks.fbi-noisy           include the entire log message in the announcement
23 #   hooks.fbi-msg-template    the template for the message to be sent (default:
24 #                               {@@component@@/}@@project@@[@@ref@@]: @@author@@: \
25 #                               @@logmessage@@ [@@url@@])
26 #
27 # The hook is meant ot be run from the Git post-receive hook.
28 #
29 set -eu
30
31 # we need to ensure we know $GIT_DIR for the git-config calls
32 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
33 if [ -z "$GIT_DIR" ]; then
34   echo >&2 "E: post-receive: GIT_DIR not set"
35   exit 1
36 fi
37 export GIT_DIR
38
39 project=$(git config hooks.fbi-project 2>/dev/null) || {
40   echo >&2 "E: FBI project not set (hooks.fbi-project)"
41   exit 1
42 }
43 component=$(git config hooks.fbi-component 2>/dev/null ||:)
44 url=$(git config hooks.fbi-url-template 2>/dev/null ||:)
45 noisy=$(git config hooks.fbi-noisy 2>/dev/null || echo false)
46 from=$(git config hooks.fbi-emailsender 2>/dev/null ||:)
47 to="commit@commit.ganneff.de" #TODO: possibly factor out to config
48 cc=$(git config hooks.fbi-emailcc 2>/dev/null ||:)
49
50 # mail submission client to use
51 if [ -n "${FBI_ANNOUNCE_TO_STDOUT:-}" ]; then
52   sendmail="cat"
53   echo >&2 'I: outputting to stdout since $FBI_ANNOUNCE_TO_STDOUT is set.'
54 else
55   sendmail="/usr/sbin/sendmail -t"
56 fi
57
58 if [ $# -eq 0 ] ; then
59   # figure out refs from HEAD since they aren't passed
60   refname=$(git symbolic-ref HEAD 2>/dev/null)
61   merged=$(git rev-parse HEAD)
62 else
63   refname=$1
64   merged=$(git rev-parse $2)
65 fi
66 refname=${refname##refs/heads/}
67 refwidth=$(git config hooks.fbi-refwidth 2>/dev/null || echo 7)
68 ref=$(git describe ${merged} 2>/dev/null || echo ${merged} | cut -c1-${refwidth})
69
70 url=$(echo "$url" | sed -e "s,@@sha1@@,${merged},")
71
72 rawcommit=$(git cat-file commit ${merged})
73 author=$(echo "${rawcommit}" | sed -rne 's,^author (.+) <.*,\1,p')
74 logmessage=$(echo "${rawcommit}" | sed -e '1,/^$/d')
75 ${noisy} || logmessage=$(echo "${logmessage}" | head -n2)
76
77 ts=$(echo "${rawcommit}" | sed -n -e '/^author .*> \([0-9]\+\).*$/s--\1-p')
78 ts=$(date -d "@${ts}")
79
80 msg=$(git config hooks.fbi-msg-template 2>/dev/null ||:)
81 if [ -z "${msg}" ]; then
82   msg="${component:+$component/}${refname}[${ref}]: ${author}: ${logmessage} [${url}]"
83 else
84   msg=$(echo "$msg" | sed -re 's,@@([^@]+)@@,${\1},g')
85   eval "msg=\"$msg\""
86 fi
87
88 # trim $msg of empty []'s in case no URL was provided, might be better done in
89 # the above by making the generation conditional, but this will also do for
90 # now.
91 msg="${msg% []}"
92
93 ${sendmail} <<_eom
94 ${from:+From: ${from}
95 }To: ${to}
96 ${cc:+Cc: ${cc}
97 }Subject: Announce ${project}
98 Message-Id: <fbi-announce.${project}.${ref}@$(hostname --fqdn)>
99 Content-type: text/plain; charset=utf-8
100
101 ${msg}
102 _eom