summaryrefslogtreecommitdiffstats
path: root/hooks/pre-commit
blob: 884f8246c5065ef64d6f50ca16507b7f50fd3f94 (plain)
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
#!/bin/bash
#
# A hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.

#
# Checks provided by the hook template
#

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=$(git hash-object -t tree /dev/null)
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
	# Note that the use of brackets around a tr range is ok here, (it's
	# even required, for portability to Solaris 10's /usr/bin/tr), since
	# the square bracket bytes happen to fall in the designated range.
	test $(git diff --cached --name-only --diff-filter=A -z $against |
	  LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
	cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
EOF
	exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $against --

#
# Check formating of the commited .po files
#

# Get the list of updated po files
po_files=`git diff --cached --name-only --diff-filter=ACM | grep '\.po$'`
# Run 'msgfmt -vc' on each po file
# Warn about each malformed file and abort the commit if needed
failure=0
for po_file in $po_files
do
	msgfmt -vc $po_file > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "Error: Malformed file $po_file"
		echo "\tPlease run 'msgfmt -vc $po_file' and fix the reported errors."
		failure=1
	fi
done

# Exit only in case of error.
if [ $failure -eq 1 ]; then
	exit $failure
fi

#
# Sending of [DONE] messages
#

CONFIG_FILE=hooks/pre-commit.conf
# Bump this version number whenever a new, non-retrocompatible change
# is introduced in the configuration.
PRECOMMIT_SCRIPT_VERSION=1
if [ -f "$CONFIG_FILE" ]; then
	. "$CONFIG_FILE"
	if [ $PRECOMMIT_SCRIPT_VERSION -ne $PRECOMMIT_CONFIG_VERSION ]; then
		echo "Mismatch between the script and your configuration file."
		echo "Please update your configuration according to the template and update the value of \$PRECOMMIT_CONFIG_VERSION to $PRECOMMIT_SCRIPT_VERSION."
		exit 1
	fi

	# Prepare [DONE] messages
	# First, collect the message-ids of the [RFR] or [LCFC] messages matching the files.
	# Note: One message can be about several files, so we have to gather them.
	content=$(curl -L --silent $COORDINATION_URL)
	declare -A messages
	for po_file in $po_files
	do
		name=$(echo "$po_file" | sed -E 's#po/.+/##' | sed -E 's/\..*//')
		msgurl=$(echo "$content" | grep -A 6 "<td>manpages" | grep -A 4 "<td>$name/" | grep -o "https.*html")
		if [ -z "$msgurl" ]
		then
			echo "$po_file has not been found on $COORDINATION_URL"
		else
			msg=$(curl -L --silent $msgurl)
			msgid=$(echo "$msg" | grep "Message-id" | grep -oP '"/msgid-search/.*?"' | sed 's#"/msgid-search/\(.*\)"#\1#')
			title=$(echo "$msg" | grep h1 | sed -E 's/<.?h1>//g' | sed -E 's/\[.*\]//')
			messages[$msgid]=$title
		fi
	done

	# Now, for each collected message, send a [DONE] reply to the l10n list.
	for msgid in "${!messages[@]}"
	do
		case $MUA in
		mailto)
			# Escape line endings so they can be correctly interpreted by the MUA.
			escaped_done_msg=$(echo "$DONE_MSG" | sed ':a;N;$!ba;s/\n/%0D%0A/g')
			echo -e 'Ctrl+click on \e]8;;mailto:'"${LIST_ADDRESS}"'?subject=[DONE] '"${messages[$msgid]}"'&in-reply-to=<'"$msgid"'>&body='"$escaped_done_msg"'\e\\this link\e]8;;\e\\ to send your message.'
			;;
		thunderbird)
			thunderbird -compose "to='${LIST_ADDRESS}',subject='[DONE] ${messages[$msgid]}',body='$DONE_MSG'"
			;;
		*)
			echo "Unknown Mail User Agent, you can use 'mailto' for the default behavior."
			;;
		esac
	done
fi

exit 0