/
gitnotify
executable file
·258 lines (227 loc) · 7.5 KB
/
gitnotify
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
#
# gitnotify or diff2html
# Copyright (C) by Vladislav Moskovets (github at vlad.org.ua)
# html/css based on svnnotify
# and diff2html shell script
#
# 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/>.
read oldrev newrev refname
tempfile="/tmp/.gitnotify-$$-`date +%N`.html"
rm -f $tempfile
LOG="(none)"
TITLE="TITLE"
REV="(none)"
AUTHOR="none <none@none>"
DATE="`date`"
usage() {
echo "usage: $0 [-g] [-m] [-s] [-a] [-r]"
exit 1
}
start_html() {
cat <<END
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>$TITLE</title>
</head>
<body style="font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;">
END
}
htmlizediff() {
echo '<div id="patch">
<h3 style="font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold;">Diff</h3> '
OIFS=$IFS
IFS='
'
# The -r option keeps the backslash from being an escape char.
read -r s
while [[ $? -eq 0 ]]; do
# Get beginning of line to determine what type
# of diff line it is.
t1=${s:0:1}
t2=${s:0:2}
t3=${s:0:3}
t4=${s:0:4}
t7=${s:0:7}
# Convert &, <, > to HTML entities.
s=$(sed -e 's/\&/\&/g' -e 's/</\</g' -e 's/>/\>/g' <<<"$s")
if [[ $first -eq 1 ]]; then
first=0
fi
# Determine HTML class to use.
if [[ "$t7" == 'Only in' ]]; then
cls='only'
#if [[ $diffseen -eq 0 ]]; then
# diffseen=1
# echo -n '</span>'
#else
# if [[ $lastonly -eq 0 ]]; then
# echo "</pre>"
# echo "</div>"
# fi
#fi
#if [[ $lastonly -eq 0 ]]; then
# echo "<div class='modfile'>"
# echo "<pre class='diff'>"
#fi
lastonly=1
elif [[ "$t4" == 'diff' ]]; then
cls='diff'
if [[ $diffseen -eq 0 ]]; then
diffseen=1
else
echo "</pre>"
echo "</div>"
fi
id="`echo ${s:5}|sed -e 's/--git//g' -e's/[\/ ]/_/g'`"
echo "<a name='$id'/>"
echo '<div style="{border:1px solid #ccc;margin:10px 0;}">'
echo '<h4 style="{font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;">'$s'</h4>'
echo '<pre style="width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;">'
lastonly=0
elif [[ "$t3" == '+++' ]]; then
echo -ne '<span style="color:#888;background:#fff">'"$s\n"'</span>'
lastonly=0
elif [[ "$t3" == '---' ]]; then
echo -ne '<span style="color:#888;background:#fff">'"$s\n"'</span>'
lastonly=0
elif [[ "$t2" == '@@' ]]; then
echo -ne '<span style="color:#888;background:#fff">'"$s\n"'</span>'
lastonly=0
elif [[ "$t1" == '+' ]]; then
echo -n '<ins style="background:#dfd;text-decoration:none;display:block;padding:0 0px;">'"$s"
echo -ne "\n</ins>"
lastonly=0
elif [[ "$t1" == '-' ]]; then
echo -n '<del style="background:#fdd;text-decoration:none;display:block;padding:0 0px;">'"$s"
echo -ne "\n</del>"
lastonly=0
else
echo -ne '<span class="cx">'"$s\n"'</span>'
lastonly=0
fi
read -r s
done
IFS=$OIFS
if [[ $diffseen -eq 0 && $onlyseen -eq 0 ]]; then
echo -n '</span q1>'
else
echo "</div>"
fi
echo
}
end_html() {
cat <<END
</pre>
</div>
</div>
</body>
</html>
END
}
summary() {
echo '<div id="msg">'
echo '<dl style="border: 1px #006 solid; background: #369; padding: 6px; color: #fff;">'
if [ $gitmode = 1 ]; then
git log $1 -1 --pretty=medium --shortstat | sed -e 's/^commit\(.*\)/<dt style="float: left; width: 6em; font-weight: bold;">Commit<\/dt> <dd>\1<\/dd>/' \
-e 's/^Author:\(.*\)/<dt style="float: left; width: 6em; font-weight: bold;">Author<\/dt> <dd>\1<\/dd>/' \
-e 's/^Date:\(.*\)/<dt style="float: left; width: 6em; font-weight: bold;">Date<\/dt> <dd>\1<\/dd><\/dl><h3>Log Message<\/h3><pre style="overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px;">/'
echo '</pre>'
#echo '<h3>Modified Paths</h3>'
#echo '<ul>'
#echo '<li><a href="#"></a></li>'
#echo '</ul>'
else
echo '<dt style="float: left; width: 6em; font-weight: bold;">Author</dt> <dd>'$AUTHOR'</dd>'
echo '<dt style="float: left; width: 6em; font-weight: bold;">Date</dt> <dd>'
date
echo '</dd></dl>'
fi
echo '</div>'
}
prepare_mail() {
exec 6>&1
exec > $tempfile
}
send_mail() {
exec 1>&6 6>&-
subject="${SUBJECT:-no subject}"
if [ -x `which bsd-mailx` ]; then
bsd-mailx -a 'Content-Type: text/html; charset=UTF-8' -a 'Content-Transfer-Encoding: 8bit' -a 'X-Mailer: GITNotify: http://github.com/slayer/gitnotify' -s "$subject" $MAILTO < $tempfile
else
git diff -p -M --cached $1^ $1 -- | mail -s "$subject" -a $tempfile $MAILTO
fi
rm $tempfile
}
prepare_git() {
[ -x `which tempfile` ] && gitdiff=`tempfile` || gitdiff="/tmp/.gitnotify-gitdiff-$$-`date +%N`.diff"
git rev-parse --verify $1 >/dev/null 2>&1 || return 1
git diff -M --cached $1^ $1 -- >$gitdiff
exec 7<&0
exec < $gitdiff
}
post_git() {
exec 0<&7 7<&-
rm $gitdiff
}
[ `basename $0` = 'gitnotify' ] && gitmode=1 || gitmode=0
[ `basename $0` = 'diff2html' ] && gitmode=0
while getopts "gr:a:m:s:" opt; do
case $opt in
r) REV=${OPTARG} ;;
a) AUTHOR=${OPTARG} ;;
m) MAILTO="$MAILTO ${OPTARG}"; mailmode=1 ;;
s) SUBJECT="${OPTARG}" ;;
g) gitmode=1 ;;
*) usage
;;
\? ) usage
;;
esac
done
for rev in `git rev-list $oldrev..$newrev | tac`; do
[ $mailmode ] && prepare_mail $rev
[ $gitmode ] && prepare_git $rev || usage
start_html $rev
summary $rev
htmlizediff
end_html
[ $gitmode ] && post_git $rev
[ $mailmode ] && send_mail $rev
done