Automatic run of checkstyle with the git hook help

 Static code analysis - This is a huge topic for investigation (long story short - it's a tool to check your source code if it suits to configured code style rules).

Each team/company often has it's own code style and its own rules of how developers should organize their code (new lines, whitespaces, variables and class namings, etc.).

But today I would like to present my minor improvement of how to configure checkstyle run automatically (but not on each letter change, how IDE is doing it).

My solution is - to use an old reliable git hook.

What we need:

  1. git repository
  2. file with rules (checkstyle file)
  3. analyzer tool (some program, which is used to analyze code)
How it will work - each time when we commit our changes, hook will trigger our pre-commit script and run validation.

How to configure:

1. Take prepared pre-commit script

--------------------------------------------------------------------------------------------------------

#!/usr/bin/perl

#
# Pre-commit hook for running checkstyle on changed Java sources
#
# To use this you need:
# 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]
# 4. Put this in your .git/hooks directory as pre-commit
#
# Now, when you commit, you will be disallowed from doing so
# until you pass your checkstyle checks.
$command "git-diff-index --cached HEAD 2>&1 | sed 's/^:.*     //' | uniq";
open (FILES,$command "|") || die "Cannot run '$command': $!\n";
$CONFIG_CHECK_FILE "checkstyle.checkfile";
$CONFIG_JAR "checkstyle.jar";
$CONFIG_JAVA "java.command";
$check_file = `git config --get $CONFIG_CHECK_FILE`;
$checkstyle_jar = `git config --get $CONFIG_JAR`;
$java_command = `git config --get $CONFIG_JAVA`;
if (!$check_file || !$checkstyle_jar)
{
   die "You must configure checkstyle in your git config:\n"
   "\t$CONFIG_CHECK_FILE - path to your checkstyle.xml file\n"
   "\t$CONFIG_JAR - path to your checkstyle jar file\n"
   "\t$CONFIG_JAVA - path to your java executable (optional)\n"
   ;
}
$java_command "java" if (!$java_command);
chomp $check_file;
chomp $checkstyle_jar;
chomp $java_command;
$command "$java_command -jar $checkstyle_jar -c $check_file";
@java_files = ();
foreach (<FILES>)
{
   chomp;
   next if (!(/\.java$/));
   push @java_files,$_;
   $command .= " ";
   $command .= $_;
}
if ($#java_files >= 0)
{
   if (&run_and_log_system ($command))
   {
       print STDERR "Commit aborted.\n";
       exit -1;
   }
}
exit 0;
sub run_and_log_system
{
   ($cmd) = @_;
   system $cmd;
}
--------------------------------------------------------------------------------------------------------
And place it into file in .git/hooks directory (on a pic. below)

2. Set git variables:

$ git config --add checkstyle.jar <location of jar>
[ex. git config --add checkstyle.jar "/d/git/checkstyle-8.14-all.jar"]
$ git config --add checkstyle.checkfile <location of checkfile>
[ex. git config --add checkstyle.checkfile "/d/git/checkstyle.xml"]
$ git config --add java.command <path to java executale>
[optional, defaults to assuming it's in your path]

And that's it! All what we need is to make new commit, and checkstyle hook will be triggered.
If any errors occur, it will be shown erro name with exact file names and line numbers where error occur.
In case any error occur, commit will be aborted.