-
Notifications
You must be signed in to change notification settings - Fork 24
/
pre-commit
executable file
·55 lines (40 loc) · 1.18 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
#!/bin/sh
root="$(git rev-parse --show-toplevel)"
[ -d "$root" ] || exit 1
owndir="$(cd "$(dirname "$0")"; pwd -P)"
OS_NAME=$(uname)
if [ "$OS_NAME" = "Darwin" ]; then
FIND_ARGS="-perm +0111"
else
FIND_ARGS="-executable"
fi
find "$owndir"/autoformat -type f $FIND_ARGS | {
abort=0
while read formatter ; do
magic="$formatter".magic
patterns="$formatter".patterns
[ -f "$patterns" -o -f "$magic" ] || continue
git diff --name-only --cached | {
labort=0
while IFS= read -r orig ; do
orig="${root}/${orig}"
# file is getting deleted, ignore
[ -f "$orig" ] || continue
# file matches one of the patterns
match_pattern=''
[ -f "$patterns" ] && echo "$orig" | grep -Eqif "$patterns" && match_pattern='1'
# file’s libmagic output matches
match_magic=''
[ $match_pattern ] || {
[ -f "$magic" ] && file -b "$orig" | grep -Eqif "$magic" && match_magic='1'
}
# if none, ignore
[ "$match_pattern" -o "$match_magic" ] || continue
"$formatter" "$orig" || labort=1
git add "$orig"
done
exit $labort
} || abort=1
done
exit $abort
}