-
Notifications
You must be signed in to change notification settings - Fork 12
/
pre-commit.careful
executable file
·25 lines (23 loc) · 1.08 KB
/
pre-commit.careful
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
#!/bin/bash
wtf_options='-q'
tmp=$(mktemp /tmp/wtf.XXXX) # BSD/OSX version of mktemp requires template
hh=$(git rev-parse -q --verify HEAD || echo 4b825dc642cb6eb9a060e54bf8d69288fbee4904) # empty tree
# Run Whitespace Total Fixer on index contents WITHOUT modifying working directory
git diff-index --raw -z --cached $hh --diff-filter=ACMRTU -- |
while read -d '' _ MODE _ SHA1 _ && read -d '' FILE
do
if [ "$MODE" == 120000 ]; then
true # ignore symlinks
elif git check-attr -a -- "$FILE" | grep -Eq ': (binary: set|text: unset)$'; then
# ignore files marked as binary or non-text; see gitattributes(5) and git-check-attr(1)
echo "No whitespace checking for file '$FILE'; marked +binary or -text"
elif ! ( git cat-file blob $SHA1 | wtf $wtf_options > $tmp ); then
if [ -s $tmp ]; then
git update-index --cacheinfo $MODE $(git hash-object -w $tmp) "$FILE"
echo "Fixed whitespace in $FILE" >&2
else
echo "Couldn't fix whitespace with wtf -- is it in your PATH?" >&2
fi
fi
done
rm -f $tmp 2> /dev/null