Guest User

Untitled

a guest
Mar 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # Pre-commit hook for running checkstyle on changed Java sources
  4. #
  5. # To use this you need:
  6. # 1. checkstyle's jar file somewhere
  7. # 2. a checkstyle XML check file somewhere
  8. # 3. To configure git:
  9. # * git config --add checkstyle.jar <location of jar>
  10. # * git config --add checkstyle.checkfile <location of checkfile>
  11. # * git config --add checkstyle.basedir <project's basedir>
  12. # * git config --add checkstyle.defaultseverity <default severity level>
  13. # * git config --add checkstyle.configloc <Path on the config file>
  14. # * git config --add java.command <path to java executale> [optional
  15. # defaults to assuming it's in your path]
  16. # 4. Put this in your .git/hooks directory as pre-commit
  17. #
  18. # Now, when you commit, you will be disallowed from doing so
  19. # until you pass your checkstyle checks.
  20.  
  21. # Get files names located in index
  22. $command = "git diff-index --cached --name-only HEAD";
  23. #open (FILES,$command . "|") || die "Cannot run '$command': $!\n";
  24. open (FILES,$command . "|") || die "Cannot run '$command': $!\n";
  25.  
  26. $CONFIG_CHECK_FILE = "checkstyle.checkfile";
  27. $CONFIG_JAR = "checkstyle.jar";
  28. $CONFIG_JAVA = "java.command";
  29. $CONFIG_BASEDIR ="checkstyle.basedir";
  30. $CONFIG_DEFAULT_SEVERITY ="checkstyle.defaultseverity";
  31. $CONFIG_CONFIG_LOC ="checkstyle.configloc";
  32.  
  33. $check_file = `git config --get $CONFIG_CHECK_FILE`;
  34. $checkstyle_jar = `git config --get $CONFIG_JAR`;
  35. $java_command = `git config --get $CONFIG_JAVA`;
  36. $basedir = `git config --get $CONFIG_BASEDIR`;
  37. $defaultseverity = `git config --get $CONFIG_DEFAULT_SEVERITY`;
  38. $configloc = `git config --get $CONFIG_CONFIG_LOC`;
  39.  
  40.  
  41. if (!$check_file || !$checkstyle_jar)
  42. {
  43. die "You must configure checkstyle in your git config:\n"
  44. . "\t$CONFIG_CHECK_FILE - path to your checkstyle.xml file\n"
  45. . "\t$CONFIG_JAR - path to your checkstyle jar file\n"
  46. . "\t$CONFIG_JAVA - path to your java executable (optional)\n"
  47. ;
  48. }
  49.  
  50. # Stash files to have only check indexed files
  51. $command = "git stash --keep-index -m'WIP pre-commit hook'";
  52. @stashCommand = qx/$command/;
  53. print "stashCommand: @stashCommand\n";
  54.  
  55. $java_command = "java" if (!$java_command);
  56.  
  57. chomp $check_file;
  58. chomp $checkstyle_jar;
  59. chomp $java_command;
  60. chomp $basedir;
  61. chomp $defaultseverity;
  62. chomp $configloc;
  63.  
  64. $command = "$java_command -Dbasedir=$basedir -Ddefault_severity=$defaultseverity -Dconfig_loc=$configloc -jar $checkstyle_jar -c $check_file ";
  65.  
  66. @java_files = ();
  67.  
  68. foreach (<FILES>)
  69. {
  70. chomp;
  71. next if (!(/\.java$/));
  72. push @java_files,$_;
  73. $command .= " ";
  74. $command .= $_;
  75. }
  76.  
  77.  
  78. my $javaFileSize = @java_files;
  79.  
  80. if ($javaFileSize >= 1)
  81. {
  82. print "[Checkstyle] List of Java files (" . $javaFileSize . "):\n[Checkstyle]\t" . join("\n[Checkstyle]\t", @java_files) . "\n[Checkstyle]\n";
  83.  
  84. if (&run_and_log_system ($command))
  85. {
  86. $command = "git stash pop";
  87. @stashPopCommand = qx/$command/;
  88. print "stashPopCommand 1 : @stashPopCommand\n";
  89. print "[Checkstyle] GIT COMMIT ABORTED.\n";
  90. exit -1;
  91. }
  92. }
  93. else
  94. {
  95. print "[Checkstyle] No Java file in index, checkstyle verifications ignored.\n";
  96. exit 0;
  97. }
  98.  
  99. $command = "git stash pop";
  100. @stashPopCommand = qx/$command/;
  101. print "stashPopCommand 2 : @stashPopCommand\n";
  102.  
  103.  
  104. exit 0;
  105.  
  106. sub run_and_log_system
  107. {
  108. ($cmd) = @_;
  109. @result = qx/$cmd/;
  110. my $size = $#result;
  111. print "[Checkstyle] " . join("[Checkstyle]\t", @result[0..$size-1]);
  112. print "[Checkstyle] " . @result[$size] . "[Checkstyle]\n";
  113. print "[Checkstyle] Violations: " . ($size - 1) . "\n[Checkstyle]\n";
  114. return $#result > 1 ? -1 : 0;
  115. }
Add Comment
Please, Sign In to add comment