-
Notifications
You must be signed in to change notification settings - Fork 13
/
pre-commit
executable file
·76 lines (67 loc) · 2.46 KB
/
pre-commit
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
#!/usr/bin/env bash
# Author: Boris Guéry <[email protected]>
# https://github.com/borisguery/git-keywords-checker
# Add or remove keywords here
KEYWORDS_REGEX="var_dump\(|die\(|Zend_Debug::|print_r\(|debug_print_var|console\.(debug|info|log|warn)\("
# Add extensions to check here
EXTENSIONS_REGEX="(.php$|.phtml$|.js$|.tpl$|.coffee$)"
ERRORS_BUFFER=""
TEXT_DEFAULT="\\033[0;39m"
TEXT_INFO="\\033[1;32m"
TEXT_ERROR="\\033[1;31m"
TEXT_UNDERLINE="\\0033[4m"
TEXT_BOLD="\\0033[1m"
VERBOSE=false
FILES=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
# Determine which version of sed to use BSD or GNU
TMP_FOO_FILE=$(mktemp /tmp/output.XXXXXXXXXX)
echo foo > $TMP_FOO_FILE
$VERBOSE && echo -n "Checking for GNU-style sed -i..."
sed -r 's/o/o/' $TMP_FOO_FILE &>/dev/null
if test $? -ne 0 ; then
$VERBOSE && echo " (no)"
$VERBOSE && echo -n "Checking for BSD-style sed -i..."
sed -E 's/o/o/' $TMP_FOO_FILE &>/dev/null
if test $? -eq 0 ; then
$VERBOSE && echo " (yes)"
SED_EXTENDED_CMD="sed -E"
else
$VERBOSE && echo " (no)"
echo -e "$TEXT_ERROR" "Unable to determine sed version" "$TEXT_DEFAULT"
fi
else
$VERBOSE && echo " (yes)"
SED_EXTENDED_CMD="sed -r"
fi
rm -f $TMP_FOO_FILE
echo -e "\\033[1;33m""Keywords checker - pre-commit hook" "$TEXT_DEFAULT"
echo
for FILE in $FILES; do
if [[ $FILE =~ $EXTENSIONS_REGEX ]]; then
ERRORS=""
while IFS=: read -ra RESULT; do
if [ "$RESULT" != "" ]; then
ERRORS="$ERRORS\n\tline $TEXT_BOLD${RESULT[1]}$TEXT_DEFAULT: "
ERRORS="$ERRORS"$(sed -n ${RESULT[1]}p $FILE | $SED_EXTENDED_CMD "s/($KEYWORDS_REGEX)/\\$TEXT_UNDERLINE\1\\$TEXT_DEFAULT/g")
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
fi
done < <(grep -sEnH $KEYWORDS_REGEX $FILE)
if [ "$ERRORS" != "" ]; then
ERRORS="$TEXT_ERROR Errors found in $TEXT_BOLD$FILE$TEXT_DEFAULT$ERRORS"
echo -e "$ERRORS"
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo -e "$TEXT_ERROR" "There were errors or warnings, commit aborted." "$TEXT_DEFAULT"
echo -e "$TEXT_INFO" "If you are sure you want to commit those files, use --no-verify option" "$TEXT_DEFAULT"
exit 1
else
echo -e "$TEXT_INFO" "All files are clean." "$TEXT_DEFAULT"
exit 0
fi