line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Git::Lint::Check::Commit; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
4618
|
use strict; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
159
|
|
4
|
6
|
|
|
6
|
|
26
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
149
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
24
|
use parent 'Git::Lint::Check'; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
34
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
2383
|
use Git::Lint::Command; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
3763
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.014'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub diff { |
13
|
1
|
|
|
1
|
1
|
12
|
my $self = shift; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
3
|
my $diff_arref = $self->_diff_index( $self->_against ); |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
6
|
return $diff_arref; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub _against { |
21
|
3
|
|
|
3
|
|
50
|
my $self = shift; |
22
|
|
|
|
|
|
|
|
23
|
3
|
|
|
|
|
7
|
my @git_head_cmd = (qw{ git show-ref --head }); |
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
|
|
5
|
my $against; |
26
|
3
|
|
|
|
|
20
|
my ( $stdout, $stderr, $exit ) = Git::Lint::Command::run( \@git_head_cmd ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# show-ref --head returns 1 if there are no prior commits, but doesn't |
29
|
|
|
|
|
|
|
# return a message to stderr. since we need to halt for other errors and |
30
|
|
|
|
|
|
|
# can't rely on the error code alone, checking for stderr seems like the |
31
|
|
|
|
|
|
|
# least worst way to detect if we encountered any other errors. |
32
|
|
|
|
|
|
|
# checking the error string for 'fatal: Needed a single revision' was |
33
|
|
|
|
|
|
|
# the previous way we were checking for initial commit, but seemed more |
34
|
|
|
|
|
|
|
# brittle over the long term to check for a specific error string. |
35
|
3
|
100
|
66
|
|
|
16
|
if ( $exit && $stderr ) { |
36
|
1
|
|
|
|
|
8
|
die "git-lint: $stderr\n"; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
2
|
100
|
|
|
|
10
|
if ($stdout) { |
40
|
1
|
|
|
|
|
2
|
$against = 'HEAD'; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
|
|
|
|
|
|
# Initial commit: diff against an empty tree object |
44
|
1
|
|
|
|
|
2
|
$against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
|
|
6
|
return $against; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _diff_index { |
51
|
2
|
|
|
2
|
|
54
|
my $self = shift; |
52
|
2
|
|
|
|
|
3
|
my $against = shift; |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
7
|
my @git_diff_index_cmd = ( qw{ git diff-index -p -M --cached }, $against ); |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
7
|
my ( $stdout, $stderr, $exit ) = Git::Lint::Command::run( \@git_diff_index_cmd ); |
57
|
|
|
|
|
|
|
|
58
|
2
|
100
|
|
|
|
13
|
die "git-lint: $stderr\n" if $exit; |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
4
|
return [ split( /\n/, $stdout ) ]; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub format_issue { |
64
|
6
|
|
|
6
|
1
|
126
|
my $self = shift; |
65
|
6
|
|
|
|
|
25
|
my $args = { |
66
|
|
|
|
|
|
|
filename => undef, |
67
|
|
|
|
|
|
|
check => undef, |
68
|
|
|
|
|
|
|
lineno => undef, |
69
|
|
|
|
|
|
|
@_, |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
|
72
|
6
|
|
|
|
|
10
|
foreach ( keys %{$args} ) { |
|
6
|
|
|
|
|
34
|
|
73
|
|
|
|
|
|
|
die "$_ is a required argument" |
74
|
14
|
100
|
|
|
|
61
|
unless defined $args->{$_}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
|
|
10
|
my $message = $args->{check} . ' (line ' . $args->{lineno} . ')'; |
78
|
|
|
|
|
|
|
|
79
|
3
|
|
|
|
|
23
|
return { filename => $args->{filename}, message => $message }; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub get_filename { |
83
|
9
|
|
|
9
|
1
|
11
|
my $self = shift; |
84
|
9
|
|
|
|
|
12
|
my $line = shift; |
85
|
|
|
|
|
|
|
|
86
|
9
|
|
|
|
|
9
|
my $filename; |
87
|
9
|
100
|
|
|
|
20
|
if ( $line =~ m|^diff --git a/(.*) b/\1$| ) { |
88
|
1
|
|
|
|
|
3
|
$filename = $1; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
9
|
|
|
|
|
15
|
return $filename; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub parse { |
95
|
5
|
|
|
5
|
1
|
148
|
my $self = shift; |
96
|
5
|
|
|
|
|
16
|
my $args = { |
97
|
|
|
|
|
|
|
input => undef, |
98
|
|
|
|
|
|
|
match => undef, |
99
|
|
|
|
|
|
|
check => undef, |
100
|
|
|
|
|
|
|
@_, |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
5
|
|
|
|
|
10
|
foreach ( keys %{$args} ) { |
|
5
|
|
|
|
|
15
|
|
104
|
|
|
|
|
|
|
die "$_ is a required argument" |
105
|
14
|
100
|
|
|
|
54
|
unless defined $args->{$_}; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
die 'match argument must be a code ref' |
109
|
2
|
100
|
|
|
|
15
|
unless ref $args->{match} eq 'CODE'; |
110
|
|
|
|
|
|
|
|
111
|
1
|
|
|
|
|
3
|
my @issues; |
112
|
|
|
|
|
|
|
my $filename; |
113
|
1
|
|
|
|
|
0
|
my $lineno; |
114
|
|
|
|
|
|
|
|
115
|
1
|
|
|
|
|
1
|
foreach my $line ( @{ $args->{input} } ) { |
|
1
|
|
|
|
|
2
|
|
116
|
9
|
|
|
|
|
17
|
my $ret = $self->get_filename($line); |
117
|
9
|
100
|
|
|
|
14
|
if ($ret) { |
118
|
1
|
|
|
|
|
2
|
$filename = $ret; |
119
|
1
|
|
|
|
|
2
|
next; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
8
|
100
|
|
|
|
16
|
if ( $line =~ /^@@ -\S+ \+(\d+)/ ) { |
123
|
1
|
|
|
|
|
3
|
$lineno = $1 - 1; |
124
|
1
|
|
|
|
|
2
|
next; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
7
|
50
|
|
|
|
12
|
if ( $line =~ /^ / ) { |
128
|
0
|
|
|
|
|
0
|
$lineno++; |
129
|
0
|
|
|
|
|
0
|
next; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
7
|
100
|
100
|
|
|
23
|
if ( $line =~ /^--- / || $line =~ /^\+\+\+ / ) { |
133
|
2
|
|
|
|
|
3
|
$lineno++; |
134
|
2
|
|
|
|
|
3
|
next; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
5
|
100
|
|
|
|
13
|
if ( $line =~ s/^\+// ) { |
138
|
2
|
|
|
|
|
4
|
$lineno++; |
139
|
2
|
|
|
|
|
3
|
chomp $line; |
140
|
|
|
|
|
|
|
|
141
|
2
|
50
|
|
|
|
6
|
if ( $args->{match}->($line) ) { |
142
|
|
|
|
|
|
|
push @issues, |
143
|
|
|
|
|
|
|
$self->format_issue( |
144
|
|
|
|
|
|
|
filename => $filename, |
145
|
|
|
|
|
|
|
check => $args->{check}, |
146
|
2
|
|
|
|
|
15
|
lineno => $lineno, |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
1
|
|
|
|
|
4
|
return @issues; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |