[89727a]: contrib / hooks / git-pre-commit-hook.pl Maximize Restore History

Download this file

git-pre-commit-hook.pl    72 lines (60 with data), 1.8 kB

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/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;
}