add checkstyle as pre-commit git hook
Okay seems as i found a solution finally. I added some comments to the instructions how i made it work.
1. checkstyle's jar file somewhere
2. a checkstyle XML check file somewhere
3. To configure git:
* git config --add checkstyle.jar <location of jar>
* git config --add checkstyle.checkfile <location of checkfile>
* git config --add java.command <path to java executale> [optional
defaults to assuming it's in your path]
I checked my config (can be found in the .git directory of your git reposirtory) and it looked like this:
...
[checkstyle]
checkfile = C:\\Users\\schuster\\Desktop\\checkstyle
jar = C:\\Users\\schuster\\Desktop\\checkstyle
...
So since im Working on Windows i changed it to:
...
[checkstyle]
checkfile = C:/Users/schuster/Desktop/checkstyle/google_checks.xml
jar = C:/Users/schuster/Desktop/checkstyle/checkstyle.jar
...
.
4. Put this in your .git/hooks directory as pre-commit
'This' is the file i linked when i stated my problem. So this file needs to be in the /hooks directory. But it has to be renamed as one of the existing samples which are already in there. Since my hook is a pre-commit hook i took the "pre-commit" filename. Next this file has to become an executable. To do that type in chmod +x pre-commit
in the /hooks directory of your git repository. If you work with Windows do that using the Git Bash.
EDIT :
In case someone want to use this script and is wondering why it doesnt abort even if checks fail - here is how to fix it.
in line 58if (&run_and_log_system ($command))
has to be chanted toif (!&run_and_log_system ($command))
After configure checkstyle.jar and checkstyle.checkfile. If it does not abort on checks fail here is the fix.
At line 58
if (&run_and_log_system ($command))
Replace with below
$command .= " | grep WARN ";
if (!&run_and_log_system ($command))