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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#!/bin/sh
#
# Copyright (c) 2014 SkySQL Ab
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 of
# the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#
# Configuration
# -------------
# hooks.postcommitrecipients (mandatory, error if not set)
# Comma-separated list of email addresses.
# hooks.postcommitbranches (if not configured, no email is sent)
# Space-separated list of branches. * wildcard is allowed
# hooks.postcommitsender (if not configured, user.email will be used if exists,
# otherwise a generated address)
# E-mail address from which the message will be sent.
# hooks.postcommitmailer (default /usr/sbin/sendmail)
# Tool to send the e-mail
#
# Command line arguments
# ----------------------
# post-commit [commit-hash]
#
# The script also takes an optional parameter that acts as a git hash.
# Ex:
# ./post-commit HEAD~1
# ./post-commit <commit-hash>
# The script will email the contents of that commit instead of HEAD.
create_email()
{
# Subject will have format <abbreviated revid>: <First line of commit comment>
subj=$(git log $1 --pretty="%h: %B" | head -1)
cat <<-EOF
To: $recipients
Subject: $subj
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
EOF
git show $1 --patch-with-stat --format="revision-id: %H ($(git describe $1 --tags --first-parent))%nparent(s): %P%nauthor: %aN%ncommitter: %cN%ntimestamp: %ci%nmessage:%n%n%B"
}
send_mail()
{
if [ -n "$sender" ]; then
${mailer:-/usr/sbin/sendmail} -t -f "$sender"
else
${mailer:-/usr/sbin/sendmail} -t
fi
}
#######################
# Main
#######################
if [ -n "$SKIP_COMMIT_EMAIL" ] ; then
echo "post-commit hook: SKIP_COMMIT_EMAIL set, not sending the commit notification" 1>&2
exit 0
fi
if [ -d `git rev-parse --show-toplevel`/.git/rebase-merge ] ; then
echo "post-commit hook: rebase-merge folder was found, assuming ongoing rebase or merge, not sending the commit notification." 1>&2
echo "Run the script manually if/when needed" 1>&1
exit 0
fi
recipients=$(git config hooks.postcommitrecipients)
branches=$(git config hooks.postcommitbranches)
if [ -z "$branches" ] ; then
echo "post-commit hook: no branches configured, not sending the commit notification" 1>&2
exit 0
fi
if [ -z "$recipients" ]; then
echo "post-commit hook: ERROR: recipient list is empty, not sending the commit notification" 1>&2
exit 1
fi
branch=$(git branch 2>/dev/null | grep '^*' | sed -e 's/* //')
# We do not want globbing here, because the list of branches might contain
# a wildcard, and we need it to remain the wildcard, not to be expanded
#SHELLOPTS_SAVE=$SHELLOPTS
set -f
# Checking if the current branch matches any value from the configured list
unset _branch_found
for b in $branches
do
case $branch in
$b) _branch_found=1 ; break ;;
*) ;;
esac
done
# Restore previous options
# (Commented because SHELLOPTS is read-only in bash)
#SHELLOPTS=$SHELLOPTS_SAVE
if [ -z "$_branch_found" ] ; then
echo "post-commit hook: branch $branch is not in the configured branch list, not sending the commit notification" 1>&2
exit 0
fi
sender=$(git config hooks.postcommitsender)
sender=${sender:-$(git config user.email)}
mailer=$(git config hooks.postcommitmailer)
create_email $1 | send_mail
|