-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·108 lines (97 loc) · 3.95 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
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
#!/bin/sh
#
# An extended example 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.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# If you want to allow non-ascii filenames set this variable to true.
allownonascii=$(git config hooks.allownonascii)
sizelimit=52428800
# 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')"
then
echo "Attempt to add a non-ascii file name. Please choose a different name. Aborting."
exit 1
fi
whitefiles=$(git diff --cached --name-only | grep ' ')
if test -n "$whitefiles"; then
echo "Error: filename(s) $whitefiles contains whitespace. Aborting."
exit 1
fi
# this checks that no unimportant whitespace changes are committed
whitespace=$(git diff-index --check --cached $against --)
echo "> Checking for whitespace errors."
if [ "${whitespace}" != "" ]; then
echo "Your staged changes contain whitespace errors, please fix before committing. (See them with 'git diff --check --cached'.) Aborting."
exit 1
fi
# check for missing trailing newlines
# Files (not deleted) in the index
files=$(git diff-index --cached --name-only --diff-filter=d "$against")
result=0 # track exit code
if [ -n "${files}" ]; then
for f in ${files}; do
# Only report known text files...
if [ -z "$(git diff-index --cached --stat=1 "${against}" "$f" | grep -is '| Bin')" ]; then
# Only match regular files (e.g. no symlinks)
# See: https://stackoverflow.com/a/8347325
if [ "$(git ls-files --stage "$f" | awk '{print $1}' | head -c2 - )" = "10" ]; then
# Using staged version of file instead of working dir
# See: http://stackoverflow.com/a/5032436/5343341
if [ -n "$(git cat-file blob "$(git ls-files --stage "$f" | awk '{print $2}')" | tail -c1 - )" ]; then
# Report error
if [ "$result" -lt "1" ]; then
echo "Error: The following files have no trailing newline:"
fi
echo "$f"
result=1
fi
fi
fi
done
if [ "$result" -eq "1" ]; then
exit 1
fi
fi
# check that no files are introduced anymore containing the ZIB Academic License
zibacademic=$(git diff --cached HEAD | grep "^+" | grep "ZIB Academic Li")
if [ "${zibacademic}" != "" ]; then
echo "Your staged changes introduce the 'ZIB Academic License, although SCIP is licensed under the Apache 2.0 license."
refuse=true
fi
# check for `#if 0` in the code
ifzeros=$(git diff --cached HEAD *.c *.h *.cpp *.hpp *.sh | grep "^+" | grep "#if[[:space:]]\+0")
echo "> Checking occurences of '#if 0'."
if [ "${ifzeros}" != "" ]; then
echo "Your staged changes contain '#if 0'. Please use '#ifdef SCIP_DISABLED_CODE' along with an explanatory comment instead. Aborting."
exit 1
fi
# check the filesize of each file stays below sizelimit
# list new or modified files newfiles=$(git diff --staged --name-only )
currdir="$(pwd)"
for f in $(git diff --cached --name-only | sort | uniq ); do
file="${currdir}/${f}"
if ! test -f "$file"; then
continue
fi
filesize=$(du -b "${file}" | awk '{print $1}')
if [ "${filesize}" -ge "${sizelimit}" ]; then
echo "Error: hard size limit (${sizelimit}) exceeded for file ${f} (${filesize} bytes). Aborting."
exit 1
fi
done
echo "All fine, thank you."