-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·53 lines (39 loc) · 1.15 KB
/
pre-commit
File metadata and controls
executable file
·53 lines (39 loc) · 1.15 KB
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
#!/bin/bash
if [ $(uname) == "Darwin" ]; then
EXEC="-perm +0111"
else
EXEC="-executable"
fi
root="$(git rev-parse --show-toplevel)"
[ -d "$root" ] || exit 1
owndir="$(cd "$(dirname "$0")"; pwd -P)"
find "$owndir"/autoformat -type f $EXEC | {
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 "$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
}