-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit-checkstyle
41 lines (34 loc) · 1.28 KB
/
pre-commit-checkstyle
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
#!/bin/bash
#
# 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".
# /home/khc/.m2/repository/com/puppycrawl/tools/checkstyle/6.3/checkstyle-6.3.jar:/home/khc/.m2/repository/commons-cli/commons-cli/1.2/commons-cli-1.2.jar
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
root=$(git rev-parse --show-toplevel)
config=$root/$(git ls-files --full-name | grep checkstyle.xml$)
if [ ! -f $config ]; then
echo "cannot find checkstyle config!"
exit 1
fi
copyright=$root/$(git ls-files --full-name | grep copyright_header.txt$)
echo "checking " $(git diff-index --cached --name-only $against -- | grep .java$)
cd $root && java -cp /home/khc/Downloads/checkstyle-6.3-all.jar \
-Dcheckstyle.header.file=$copyright \
com.puppycrawl.tools.checkstyle.Main \
-f plain \
-c $config \
$(git diff-index --cached --name-only $against -- | grep .java$) | grep ^/
# grep returns 0 if it found something
if [ $? == 0 ]; then
exit 1
fi