-
Notifications
You must be signed in to change notification settings - Fork 26
/
pre-commit
executable file
·50 lines (43 loc) · 1.4 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
#!/bin/bash -eu
#
# An 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.
#
# To enable this hook, rename this file to "pre-commit".
function exit_print_error(){
if [[ -z $clang_format ]];then
echo "Please install 'clang-format' and put it in system PATH," > /dev/stderr
echo "or specify it in 'git config hooks.indent'" > /dev/stderr
exit 1
fi
}
trap exit_print_error EXIT
clang_format="$(git config hooks.indent)" || clang_format=$(command -v clang-format 2>/dev/null)
if git rev-parse --verify HEAD >/dev/null 2>&1; then
Against=HEAD
else
# Initial commit: diff against an empty tree object
Against=$(git hash-object -t tree /dev/null)
fi
## Loop on modified files
while read File; do
if [[ ! -r $File ]];then
continue
fi
if [[ ! -f $File ]];then
continue
fi
## Assume we do not have files named "c" or "h"
Ext=${File##*.}
case $Ext in
c|h)
echo "clang-format " $File > /dev/stderr
$clang_format -i $File
;;
esac
git add "$File"
done < <(git diff --cached --name-only $Against)
# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $Against -- > /dev/stderr