Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 checkstyle.basedir <project's basedir>
- # * git config --add checkstyle.defaultseverity <default severity level>
- # * git config --add checkstyle.configloc <Path on the config file>
- # * 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.
- # Get files names located in index
- $command = "git diff-index --cached --name-only HEAD";
- #open (FILES,$command . "|") || die "Cannot run '$command': $!\n";
- open (FILES,$command . "|") || die "Cannot run '$command': $!\n";
- $CONFIG_CHECK_FILE = "checkstyle.checkfile";
- $CONFIG_JAR = "checkstyle.jar";
- $CONFIG_JAVA = "java.command";
- $CONFIG_BASEDIR ="checkstyle.basedir";
- $CONFIG_DEFAULT_SEVERITY ="checkstyle.defaultseverity";
- $CONFIG_CONFIG_LOC ="checkstyle.configloc";
- $check_file = `git config --get $CONFIG_CHECK_FILE`;
- $checkstyle_jar = `git config --get $CONFIG_JAR`;
- $java_command = `git config --get $CONFIG_JAVA`;
- $basedir = `git config --get $CONFIG_BASEDIR`;
- $defaultseverity = `git config --get $CONFIG_DEFAULT_SEVERITY`;
- $configloc = `git config --get $CONFIG_CONFIG_LOC`;
- 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"
- ;
- }
- # Stash files to have only check indexed files
- $command = "git stash --keep-index -m'WIP pre-commit hook'";
- @stashCommand = qx/$command/;
- print "stashCommand: @stashCommand\n";
- $java_command = "java" if (!$java_command);
- chomp $check_file;
- chomp $checkstyle_jar;
- chomp $java_command;
- chomp $basedir;
- chomp $defaultseverity;
- chomp $configloc;
- $command = "$java_command -Dbasedir=$basedir -Ddefault_severity=$defaultseverity -Dconfig_loc=$configloc -jar $checkstyle_jar -c $check_file ";
- @java_files = ();
- foreach (<FILES>)
- {
- chomp;
- next if (!(/\.java$/));
- push @java_files,$_;
- $command .= " ";
- $command .= $_;
- }
- my $javaFileSize = @java_files;
- if ($javaFileSize >= 1)
- {
- print "[Checkstyle] List of Java files (" . $javaFileSize . "):\n[Checkstyle]\t" . join("\n[Checkstyle]\t", @java_files) . "\n[Checkstyle]\n";
- if (&run_and_log_system ($command))
- {
- $command = "git stash pop";
- @stashPopCommand = qx/$command/;
- print "stashPopCommand 1 : @stashPopCommand\n";
- print "[Checkstyle] GIT COMMIT ABORTED.\n";
- exit -1;
- }
- }
- else
- {
- print "[Checkstyle] No Java file in index, checkstyle verifications ignored.\n";
- exit 0;
- }
- $command = "git stash pop";
- @stashPopCommand = qx/$command/;
- print "stashPopCommand 2 : @stashPopCommand\n";
- exit 0;
- sub run_and_log_system
- {
- ($cmd) = @_;
- @result = qx/$cmd/;
- my $size = $#result;
- print "[Checkstyle] " . join("[Checkstyle]\t", @result[0..$size-1]);
- print "[Checkstyle] " . @result[$size] . "[Checkstyle]\n";
- print "[Checkstyle] Violations: " . ($size - 1) . "\n[Checkstyle]\n";
- return $#result > 1 ? -1 : 0;
- }
Add Comment
Please, Sign In to add comment