Skip to content

Commit ac75fc7

Browse files
committed
Rewrite post-commit script; add ability to suppress emails if files match a particular pattern.
1 parent 798c4b0 commit ac75fc7

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

config-tracker

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ init() {
3838
reconfigure() {
3939

4040
# Set the email address for notifications to go to
41-
git config hooks.mailinglist "$notification_emails"
41+
[ -z "$notification_emails" ] && git config hooks.mailinglist "$notification_emails"
4242

4343
# Set the email subject prefix
44-
git config hooks.emailprefix "$notification_emails_prefix"
44+
[ -z "$notification_emails_prefix" ] && git config hooks.emailprefix "$notification_emails_prefix"
45+
46+
# Set filter pattern option
47+
[ -z "$notification_emails_filter" ] && git config git config hooks.emailfilefilter "$notification_emails_filter"
4548
}
4649

4750
#

config-tracker.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ notification_emails="[email protected]"
77
# Email prefix to aid filtering
88
notification_emails_prefix="[config tracker]"
99

10+
# Optional bash regex pattern: any files that are committed and match this
11+
# pattern will suppress notification emails from being sent.
12+
notification_emails_filter="(shadow|htpasswd|.key)"
13+
1014
# Path to include/exclude files
1115
include_file="${0%/*}/include.conf"
1216
exclude_file="${0%/*}/exclude.conf"

contrib/post-commit

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,51 @@
33
# Post-commit email hook for git.
44
#
55
6-
# 2011-08-12
6+
# 2011-08-16
77
#
88

9-
mailto="$(git config hooks.mailinglist)"
10-
subject="$(git config hooks.emailprefix) $(hostname) commit: $(git rev-parse --short HEAD) - $(git show --pretty='format:' --name-only |tr -d '\n')"
11-
message="$(git log -1 -p)"
12-
echo "$message" |mail -s "$subject" "$mailto"
9+
IFS=$'\n'
10+
11+
12+
files=$(git diff --name-only HEAD^1)
13+
14+
#
15+
# Generates the email to send
16+
#
17+
generate_email() {
18+
19+
email_header_to="$(git config hooks.mailinglist)"
20+
email_header_subject="$(git config hooks.emailprefix) $(hostname) commit: $(git rev-parse --short HEAD) - $files"
21+
email_body="$(git log -1 -p)"
22+
23+
cat <<-EOF
24+
To: $email_header_to
25+
Subject: $email_header_subject
26+
27+
$email_body
28+
EOF
29+
}
30+
31+
#
32+
# Checks if any of the committed files matched the file filter pattern
33+
#
34+
check_file_filter() {
35+
36+
filterpattern="$(git config hooks.emailfilefilter)"
37+
[ -z "$filterpattern" ] && return 0
38+
39+
for file in $files; do
40+
41+
# Abort if file matches filter pattern
42+
[[ "$file" =~ "$filterpattern" ]] && return 1
43+
44+
done
45+
46+
return 0
47+
}
48+
49+
# Silently exit if a filename matched the filter pattern
50+
check_file_filter || exit
51+
52+
# Send email
53+
generate_email | /usr/sbin/sendmail -t

0 commit comments

Comments
 (0)