summaryrefslogtreecommitdiffstats
path: root/src/gitblag.sh
blob: 4f00bb2694fc0ebd83a233967da5c3478a8699f6 (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh
#
# Copyright 2013 rsiddharth <rsiddharth@ninthfloor.org>
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see
# <http://www.gnu.org/licenses/>.
#
# This program is based on the post-receive-email hook script in the
# contrib/hooks directory in the Git SCM's source.
#
# The post-receive-mail hook script is Copyright (c) 2007 Andy Parkins
#
# Config
# ------
# hooks.mailinglist
#   This is the list that all pushes will go to; leave it blank to not send
#   emails for every ref update.
# hooks.recipientlist
#   Set the path to the file which contains a list of recipients (one
#   recipient per line).
# hooks.envelopesender
#   If set then the -f option is passed to sendmail to allow the envelope
#   sender address to be set
# hooks.emailprefix
#
#   All emails have their subjects prefixed with this prefix, or
#   "[Mailing List Prefix]" if emailprefix is unset, to aid filtering
#
# Notes
# -----
# All emails include the headers "X-Git-Refname", "X-Git-Oldrev",
# "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and
# give information for debugging.
#

# ---------------------------- Functions

# Function to prepare for email generation. This whether an email
# should even be generated.


prep_for_email()
{
	# --- Arguments
	oldrev=$(git rev-parse $1)
	newrev=$(git rev-parse $2)
	refname="$3"

    # --- Interpret
	# 0000->1234 (create)
	# 1234->2345 (update)
	# 2345->0000 (delete)
	
	if expr "$newrev" : '0*$' >/dev/null
	then
		# change_type is delete
		# Don't have to send an email.
		return 1
	fi
	
	# --- Get the revision type
	newrev_type=$(git cat-file -t $newrev 2> /dev/null)
	
	if [ $newrev_type = "commit" ]; then
		
		if [ $refname = "refs/heads/master" ]; then

			# iterate through all the new commits introduced by the
			# git push
			for rev in $(git rev-list $oldrev..$newrev)
			do
				commit_msg=$(git cat-file -p $rev | sed '1,/^$/d')
				
				if expr "$commit_msg" : "^\[NEW POST\].*$" >/dev/null
				then
				# Send email.
					return 0
				fi
			done
			
		fi

	fi
	
	# Don't have to send email.
	return 1
}


process_recipient_list()
{
	# pick each recipient from list and send the email.
	for recipient in $(cat $recipientlist)
	do
		generate_email $recipient | send_mail
	done
}


generate_email()
{
	recipient=$1

	generate_email_header
		
	generate_email_body

	generate_email_footer
}


generate_email_header()
{

	# strip off the [NEW POST] from the message:
	commit_msg=$(echo "$commit_msg" | sed 's/\[NEW POST\]//g')

	subject=$(echo "$commit_msg" | sed '1q')

	# --- Email (all stdout will be the email)
	# Generate header
	cat <<-EOF
	To: $recipient
	Subject: ${emailprefix} $subject
	X-Git-Refname: $refname
	X-Git-Reftype: $refname_type
	X-Git-Oldrev: $oldrev
	X-Git-Newrev: $newrev
	Auto-Submitted: auto-generated

	EOF
}


generate_email_footer()
{
	SPACE=" "
	cat <<-EOF


	--${SPACE}
	${listfooter}
	EOF
}


generate_email_body()
{
	body=$(echo "$commit_msg" | sed '1,/^$/d')
	
	echo "$body"
}


send_mail()
{
	if [ -n "$envelopesender" ]; then
		/usr/sbin/sendmail -t -f "$envelopesender"
	else
		/usr/sbin/sendmail -t
	fi
}


# ---------------------------- main()

# --- Config
# Set GIT_DIR either from the working directory, or from the environment
# variable.
GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
if [ -z "$GIT_DIR" ]; then
	echo >&2 "fatal: post-receive: GIT_DIR not set"
	exit 1
fi

listfooter=$(sed -ne '1p' "$GIT_DIR/description" 2>/dev/null)
# Check if the description is unchanged from it's default, and shorten it to
# a more manageable length if it is
if expr "$listfooter" : "Unnamed repository.*$" >/dev/null
then
	listfooter="Mailing list footer. To change modify .git/description at remote repo."
fi

recipients=$(git config hooks.mailinglist)
recipientlist=$(git config hooks.recipientlist)
envelopesender=$(git config hooks.envelopesender)
emailprefix=$(git config hooks.emailprefix || echo '[Mailing List Prefix]')

# --- Main loop
# Allow dual mode: run from the command line just like the update hook, or
# if no arguments are given then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
	# Output to the terminal in command line mode - if someone wanted to
	# resend an email; they could redirect the output to sendmail
	# themselves
	prep_for_email $2 $3 $1 && PAGER= generate_email
else
	while read oldrev newrev refname
	do
		prep_for_email $oldrev $newrev $refname || continue

		if [ -n "$recipients" ]; then
			# send to mailinglist.
			generate_email $recipients | send_mail
		fi

		if [ -n "$recipientlist" ]; then
			# send each email ID listed in recipient list file.
			process_recipient_list
		fi
	done
fi

GIT_WORK_TREE=/absolute/path/to/the/website git checkout -f