-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·81 lines (71 loc) · 2.45 KB
/
pre-commit
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
73
74
75
76
77
78
79
80
#!/usr/bin/env perl
use strict;
use warnings;
use autodie qw{open};
use Cwd qw{getcwd chdir};
my $dir = getcwd;
my $rakudodir = $1 if $dir =~ m!^(.+)/t/spec.*$!;
my @git_status_lines = `git status`;
my @changed_files;
for my $statuslines (@git_status_lines) {
push @changed_files, $1 if $statuslines =~ /^\s*modified:\s+(.+)$/;
}
chdir $rakudodir;
my @outputs;
my %plans_wrong;
for my $file (@changed_files) {
my @output = `make t/spec/$file`;
$plans_wrong{$file}{'plan'} = "";
$plans_wrong{$file}{'not_ok'} = [];
$plans_wrong{$file}{'exit'} = 0;
$plans_wrong{$file}{'died'} = 0;
for my $line (@output) {
$plans_wrong{$file}{'plan'} =
$line =~ /(You planned \d+ but ran \d+\.)/
if $line =~ /Bad plan\./;
push $plans_wrong{$file}{'not_ok'}, $line
if $line =~ /^not ok.+$/ && $line !~ /TODO/;
$plans_wrong{$file}{'exit'}++
if $line =~ /Non-zero exit status: (\d+)/;
$plans_wrong{$file}{'died'}++
if $line =~ m! in .+ at t/spec/$file:\d+!;
}
}
chdir $dir;
my $retVal = 0;
if(keys(%plans_wrong) > 0) {
for my $file (keys %plans_wrong) {
if( @{ $plans_wrong{$file}{'not_ok'} } > 0 ) {
warn "Failed tests in $file:";
warn "\t" . $_ for @{$plans_wrong{$file}{'not_ok'}};
warn "Unstaging $file.";
`git reset HEAD $file`;
delete $plans_wrong{$file};
} elsif( $plans_wrong{$file}{'exit'} != 0 ) {
warn "Non-zero exit status on $file.";
warn "Unstaging $file.";
`git reset HEAD $file`;
delete $plans_wrong{$file};
} elsif( $plans_wrong{$file}{'died'} != 0 ) {
warn "It looks like $file died.";
warn "Unstaging $file.";
`git reset HEAD $file`;
delete $plans_wrong{$file};
} elsif( $plans_wrong{$file}{'plan'} ne "") {
warn "$file apparently has a wrong plan ("
. $plans_wrong{$file}{'plan'} . "), please check and restage.";
warn "Unstaging $file.";
`git reset HEAD $file`;
delete $plans_wrong{$file};
} else {
warn "$file seems fine.";
}
}
}
if (keys(%plans_wrong) > 0) {
warn "Passing staged files present, committing.";
warn "Previous output should indicate files that didn't get commited.";
} else {
warn "No passing or fixable files staged, aborting commit.";
exit(1);
}