Skip to content

Commit 5b6bbdb

Browse files
committed
also check for missing newlines
1 parent d7d0d35 commit 5b6bbdb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pre-commit

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ if [ "${whitespace}" != "" ]; then
4545
exit 1
4646
fi
4747

48+
49+
# Files (not deleted) in the index
50+
files=$(git diff-index --cached --name-only --diff-filter=d "$against")
51+
result=0 # track exit code
52+
if [ -n "$files" ]; then
53+
for f in $files; do
54+
# Only report known text files...
55+
if [ -z "$(git diff-index --cached --stat=1 "$against" "$f" | grep -is '| Bin')" ]; then
56+
# Only match regular files (e.g. no symlinks)
57+
# See: https://stackoverflow.com/a/8347325
58+
if [ "$(git ls-files --stage "$f" | awk '{print $1}' | head -c2 - )" = "10" ]; then
59+
# Using staged version of file instead of working dir
60+
# See: http://stackoverflow.com/a/5032436/5343341
61+
if [ -n "$(git cat-file blob "$(git ls-files --stage "$f" | awk '{print $2}')" | tail -c1 - )" ]; then
62+
# Report error
63+
if [ "$result" -lt "1" ]; then
64+
echo "Error: The following files have no trailing newline:"
65+
fi
66+
echo "$f"
67+
exit 1
68+
fi
69+
fi
70+
fi
71+
done
72+
fi
73+
4874
# check for `#if 0` in the code
4975
ifzeros=$(git diff --cached HEAD *.c *.h *.cpp *.hpp *.sh | grep "^+" | grep "#if[[:space:]]\+0")
5076
echo "> Checking occurences of '#if 0'."

0 commit comments

Comments
 (0)