blib/lib/Git/Hooks/CheckDiff.pm

filter: Statementstmt 70.2% 85 / 121 Branchbran 40.0% 24 / 60 Conditioncond 5.2% 1 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 93.7% 15 / 16 Podpod 0.0% 0 / 2 totaltotal 55.0% 125 / 227 CC 42 SCAR 47.6
CC
42
Cov
55%
_check_token
331.6
_check_command
19.6
_check_ref
7.9
CRAP 116.9
↑ Summary lib/Git/Repository/Plugin/GitHooks.pm »
1 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::CheckDiff;
4 # ABSTRACT: Git::Hooks plugin to enforce commit policies
5 10 $Git::Hooks::CheckDiff::VERSION = '4.0.0';
6 10 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 my $PKG = __PACKAGE__;
14 10 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
15  
16 # Install hooks
17 10 GITHOOKS_CHECK_AFFECTED_REFS(\&_check_ref);
18 10 GITHOOKS_CHECK_PRE_COMMIT(\&check_commit);
19 10 GITHOOKS_CHECK_PATCHSET(\&check_patchset);
20  
21 5 ▶sub check_commit {
Subroutine check_commit: called CC=1 SCAR=0.0
Pod undocumented
22 5 my ($git, $current_branch) = @_;
23  
24 5 return _check_everything($git, {ref => $current_branch}, qw/diff-index --cached HEAD/);
25 }
26  
27 0 ▶sub check_patchset {
Subroutine check_patchset: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($git, $branch, $commit) = @_;
29  
30 0 return _check_everything($git, {ref => $branch, commit => $commit}, 'diff-tree', $commit);
31 }
32  
33 5 ▶sub _check_ref {
Subroutine _check_ref: called CC=7 SCAR=20.6
34 5 my ($git, $ref) = @_;
35  
36 5 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
37  
38 # If the reference is being deleted we have nothing to check
39 5 ▶ return 0 if $new_commit eq $git->undef_commit;
Branch: 1 branch
BranchTrueFalseTotal
if $new_commit eq $git->undef_commit 0 5 5
40  
41 # If the reference is being created we have to calculate a proper
42 # $old_commit to diff against.
43 5 ▶ if ($old_commit eq $git->undef_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_commit eq $git->undef_commit) 2 3 5
44 2 my $last_log;
45 2 my $log_iterator = $git->log($new_commit, qw/--not --all/);
46 2 ▶ while (my $log = $log_iterator->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $log_iterator->next) 2 2 4
47 2 $last_log = $log;
48 }
49 2 ▶ return 0 unless $last_log;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_log 0 2 2
50 2 my @parents = $last_log->parent;
51 2 ▶ if (@parents == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parents == 0) { } 2 0 2
elsif (@parents == 1) { } 0 0 0
52 # We reached the repository root. Hence, let's consider
53 # $old_commit to be the empty tree.
54 2 $old_commit = $git->empty_tree;
55 } elsif (@parents == 1) {
56 # We reached the first new commit and it's a normal commit. So,
57 # let's consider $old_commit to be its parent.
58 0 $old_commit = $parents[0];
59 } else {
60 # We reached the first new commit and it's a merge commit. So,
61 # let's consider $old_commit to be this commit, disregarding
62 # only the eventual conflict resolutions.
63 0 $old_commit = $last_log->commit;
64 }
65 }
66  
67 5 return _check_everything($git, {ref => $ref}, 'diff-tree', $old_commit, $new_commit);
68 }
69  
70 10 ▶sub _check_everything {
Subroutine _check_everything: called CC=1 SCAR=0.1
71 10 my ($git, $ctx, $git_cmd, @git_args) = @_;
72  
73 10 my $diff_text;
74 7 ▶ my $diff = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
75 7 ▶ $diff_text //= $git->run(
Condition33%1/3
$diff_text //= $git->run($git_cmd, ("-p", "-U0", "--no-color", "--diff-filter=AM", "--no-prefix"), @git_args)
l!l&&r!l&&!r
070
MC/DC0%0/2
$diff_text //= $git->run($git_cmd, ("-p", "-U0", "--no-color", "--diff-filter=AM", "--no-prefix"), @git_args)
$diff_text $git->run($git_cmd, ("-p", "-U0", "--no-color", "--diff-filter=AM", "--no-prefix"), @git_args)
Truth table33%1/3
A //= B
A= $diff_textB= $git->run($git_cmd, ("-p", "-U0", "--no-color", "--diff-filter=AM", "--no-prefix"), @git_args)
ABresult
1X1
011
000
76 $git_cmd, qw/-p -U0 --no-color --diff-filter=AM --no-prefix/, @git_args
77 );
78 7 return $diff_text;
79 10 };
80  
81 10 return 0 == (
82 _check_shell($git, $ctx, $diff) +
83 _check_token($git, $ctx, $diff)
84 )
85 }
86  
87 10 ▶sub _check_shell {
Subroutine _check_shell: called CC=4 SCAR=14.1
88 10 my ($git, $ctx, $diff) = @_;
89  
90 10 my @commands = $git->get_config($CFG => 'shell');
91  
92 10 ▶ return 0 unless @commands;
Branch: 1 branch
BranchTrueFalseTotal
unless @commands 3 7 10
93  
94 7 my $diff_file = _diff_file($diff);
95  
96 7 ▶ unless ($diff_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diff_file) 0 7 7
97 0 $git->fault("git diff failed", {%$ctx});
98 0 return 1;
99 }
100  
101 7 my $errors = 0;
102  
103 7 foreach my $command (@commands) {
104 7 $errors += _check_command($git, $ctx, $command, $diff_file);
105 } continue {
106 7 $git->check_timeout();
107 }
108  
109 7 return $errors;
110 }
111  
112 7 ▶sub _diff_file {
Subroutine _diff_file: called CC=1 SCAR=0.0
113 7 my ($diff) = @_;
114  
115 7 my $file = Path::Tiny->tempfile();
116  
117 7 $file->spew($diff->());
118  
119 7 return $file;
120 }
121  
122 7 ▶sub _check_command {
Subroutine _check_command: called CC=14 SCAR=29.8
123 7 my ($git, $ctx, $command, $input_file) = @_;
124  
125 # execute command and update $errors
126 7 my ($exit, $output);
127 {
128 7 my $output_file = Path::Tiny->tempfile();
129  
130 ## no critic (RequireBriefOpen, RequireCarping)
131 7 ▶ open(my $oldin, '<&', \*STDIN) or croak "Can't dup STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $oldin, "<&", \*STDIN or croak("Can't dup STDIN: $!") 7 0 7
132 7 ▶ open(STDIN , '<' , $input_file) or croak "Can't redirect STDIN to \$input_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $input_file or croak("Can't redirect STDIN to \$input_file: $!") 7 0 7
133 7 ▶ open(my $oldout, '>&', \*STDOUT) or croak "Can't dup STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $oldout, ">&", \*STDOUT or croak("Can't dup STDOUT: $!") 7 0 7
134 7 ▶ open(STDOUT , '>' , $output_file) or croak "Can't redirect STDOUT to \$output_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $output_file or croak("Can't redirect STDOUT to \$output_file: $!") 7 0 7
135 7 ▶ open(my $olderr, '>&', \*STDERR) or croak "Can't dup STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $olderr, ">&", \*STDERR or croak("Can't dup STDERR: $!") 7 0 7
136 7 ▶ open(STDERR , '>&', \*STDOUT) or croak "Can't dup STDOUT for STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", \*STDOUT or croak("Can't dup STDOUT for STDERR: $!") 7 0 7
137  
138 7 $exit = system('/bin/sh', '-c', $command);
139  
140 7 ▶ open(STDIN, '<&', $oldin) or croak "Can't dup \$oldin: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $oldin or croak("Can't dup \$oldin: $!") 7 0 7
141 7 ▶ open(STDOUT, '>&', $oldout) or croak "Can't dup \$oldout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or croak("Can't dup \$oldout: $!") 7 0 7
142 7 ▶ open(STDERR, '>&', $olderr) or croak "Can't dup \$olderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or croak("Can't dup \$olderr: $!") 7 0 7
143 ## use critic
144  
145 7 $output = $output_file->slurp;
146 }
147  
148 7 ▶ if ($exit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit != 0) { } 3 4 7
149 3 my $message = do {
150 3 ▶ if ($exit == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exit == -1) { } 0 3 3
elsif ($exit & 127) { } 0 3 3
151 0 "Command '$command' could not be executed: $!";
152 } elsif ($exit & 127) {
153 0 ▶ sprintf("Command '%s' was killed by signal %d, %s coredump",
Branch: 1 branch
BranchTrueFalseTotal
$exit & 128 ? : 0 0 0
154 $command, ($exit & 127), ($exit & 128) ? 'with' : 'without');
155 } else {
156 3 sprintf("Command '%s' failed with exit code %d", $command, $exit >> 8);
157 }
158 };
159  
160 3 $git->fault($message, {%$ctx, details => $output});
161 } else {
162 # FIXME: What should we do with eventual output from a
163 # successful command?
164 }
165  
166 7 return $exit != 0;
167 }
168  
169 10 ▶sub _check_token {
Subroutine _check_token: called CC=19 SCAR=58.0
170 10 my ($git, $ctx, $diff) = @_;
171  
172 10 ▶ my @deny_tokens = $git->get_config($CFG => 'deny-token')
Branch: 1 branch
BranchTrueFalseTotal
my(@deny_tokens) = $git->get_config($CFG, "deny-token") or return 0 0 10 10
173 or return 0;
174  
175 0 my $errors = 0;
176  
177 0 foreach my $deny_token (@deny_tokens) {
178 0 my ($regex, $filters) = split /\s+--\s+/, $deny_token, 2;
179  
180 0 my $match_token = qr/^\+.*?$regex/; # FIXME: detect error
181  
182 0 my @filters;
183  
184 0 ▶ if ($filters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filters) 0 0 0
185 0 foreach my $filter (split ' ', $filters) {
186 0 my $negated;
187 0 ▶ if ($filter =~ s/^\!//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter =~ s/^\!//u) 0 0 0
188 0 $negated = 1;
189 }
190 0 ▶ if ($filter =~ m/^\^/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter =~ /^\^/u) 0 0 0
191 0 $filter = qr/$filter/;
192 }
193 0 push @filters, [$negated, $filter];
194 }
195 }
196  
197 0 my $file = '';
198 0 my @matches;
199  
200 LINE:
201 0 foreach (split /\n/, $diff->()) {
202 0 ▶ if (/^\+\+\+ (.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\+\+\+ (.+)/u) { } 0 0 0
elsif (length $file and $_ =~ /$match_token/u) { } 0 0 0
203 0 $file = $1;
204 0 ▶ if (@filters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@filters) 0 0 0
205 0 foreach my $filter (@filters) {
206 0 ▶ if ($filter->[0] xor ## no critic (ProhibitDeepNests)
Branch: 1 branch
BranchTrueFalseTotal
if ($filter->[0] xor (ref $filter->[1] and $file =~ /$filter->[1]/u or not ref $filter->[1] and $filter->[1] eq substr($file, 0, length $filter->[1]))) 0 0 0
Condition0%0/13
$filter->[0] xor ref $filter->[1] and $file =~ /$filter->[1]/u or not ref $filter->[1] and $filter->[1] eq substr($file, 0, length $filter->[1])
l&&rl&&!r!l&&r!l&&!r
0000
ref $filter->[1] && $file =~ /$filter->[1]/u || !ref($filter->[1]) && $filter->[1] eq substr($file, 0, length $filter->[1])
l!l&&r!l&&!r
000
ref $filter->[1] and $file =~ /$filter->[1]/u
!ll&&!rl&&r
000
not ref $filter->[1] and $filter->[1] eq substr($file, 0, length $filter->[1])
!ll&&!rl&&r
000
MC/DC0%0/5
$filter->[0] xor ref $filter->[1] and $file =~ /$filter->[1]/u or not ref $filter->[1] and $filter->[1] eq substr($file, 0, length $filter->[1])
$filter->[0] ref $filter->[1] $file =~ /$filter->[1]/u not ref $filter->[1] $filter->[1] eq substr($file, 0, length $filter->[1])
Truth table0%0/14
A xor B and C || not(D) and E
A= $filter->[0]B= ref $filter->[1]C= $file =~ /$filter->[1]/uD= not ref $filter->[1]E= $filter->[1] eq substr($file, 0, length $filter->[1])
ABCDEresult
111XX0
10X110
110110
10X0X1
10X101
1100X1
110101
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
207 ((ref $filter->[1] and
208 $file =~ $filter->[1]) or
209 (not ref $filter->[1] and
210 $filter->[1] eq substr($file, 0, length($filter->[1]))))) {
211 0 next LINE;
212 }
213 }
214 0 $file = '';
215 }
216 ▶ } elsif (length $file && $_ =~ $match_token) {
Condition0%0/3
length $file and $_ =~ /$match_token/u
!ll&&!rl&&r
000
MC/DC0%0/2
length $file and $_ =~ /$match_token/u
length $file $_ =~ /$match_token/u
Truth table0%0/3
A and B
A= length $fileB= $_ =~ /$match_token/u
ABresult
0X0
100
111
217 0 push @matches, "$file: $_";
218 }
219 }
220  
221 0 ▶ if (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 0 0 0
222 0 $git->fault(<<"EOS", {%$ctx, option => 'deny-token', details => join("\n", @matches)});
223 Invalid lines matching '$regex' below.
224 Please, rewrite them and try again.
225 EOS
226 0 $errors += 1;
227 }
228 }
229  
230 0 return $errors;
231 }
232  
233 10 1;
234  
235 __END__
↑ Summary lib/Git/Repository/Plugin/GitHooks.pm »