blib/lib/Git/Hooks/CheckFile.pm

filter: Statementstmt 95.0% 229 / 241 Branchbran 76.8% 83 / 108 Conditioncond 66.6% 14 / 21 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 12 totaltotal 85.0% 358 / 421 CC 80 SCAR 22.5
CC
80
Cov
88%
deny_case_conflicts
15.9
check_command
14.9
check_acls
14.1
CRAP 9.5
« lib/Git/Hooks/Test.pm ↑ Summary blib/lib/Git/Hooks.pm »
1 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::CheckFile;
4 # ABSTRACT: Git::Hooks plugin for checking files
5 46 $Git::Hooks::CheckFile::VERSION = '4.0.0';
6 46 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 46 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 46 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 46 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 46 ▶use Text::Glob qw/glob_to_regex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 46 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 46 ▶use List::MoreUtils qw/any none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 46 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
16  
17 #############
18 # Grok hook configuration, check it and set defaults.
19  
20 46 ▶sub _setup_config {
Subroutine _setup_config: called CC=4 SCAR=13.9
21 46 my ($git) = @_;
22  
23 46 my $config = $git->get_config();
24  
25 46 ▶ $config->{lc $CFG} //= {};
Condition100%2/2
$$config{lc $CFG} //= {}
l!l
397
MC/DC100%1/1
$$config{lc $CFG} //= {}
$$config{lc $CFG}
Truth table100%2/2
A
A= $$config{lc $CFG}
Aresult
11
00
26  
27 46 my $default = $config->{lc $CFG};
28  
29 46 ▶ $default->{sizelimit} //= [0];
Condition100%2/2
$default->{"sizelimit"} //= [0]
l!l
838
MC/DC100%1/1
$default->{"sizelimit"} //= [0]
$default->{"sizelimit"}
Truth table100%2/2
A
A= $default->{"sizelimit"}
Aresult
11
00
30  
31 46 ▶ $default->{'max-path'} //= [0];
Condition100%2/2
$default->{"max-path"} //= [0]
l!l
244
MC/DC100%1/1
$default->{"max-path"} //= [0]
$default->{"max-path"}
Truth table100%2/2
A
A= $default->{"max-path"}
Aresult
11
00
32  
33 46 return;
34 }
35  
36 8 ▶sub check_command {
Subroutine check_command: called CC=12 SCAR=27.0
Pod undocumented
37 8 my ($git, $ctx, $commit, $file, $command) = @_;
38  
39 8 ▶ my $tmpfile = $git->blob($commit, $file)
Branch: 1 branch
BranchTrueFalseTotal
my $tmpfile = $git->blob($commit, $file) or return 1 8 0 8
40 or return 1;
41  
42 # interpolate filename in $command
43 8 my $cmd = $command =~ s/\{\}/\'$tmpfile\'/gr;
44  
45 # execute command and update $errors
46 8 my ($exit, $output);
47 {
48 8 my $tempfile = Path::Tiny->tempfile(UNLINK => 1);
49  
50 ## no critic (RequireBriefOpen, RequireCarping)
51 8 ▶ open(my $oldout, '>&', \*STDOUT) or croak "Can't dup STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $oldout, ">&", \*STDOUT or croak("Can't dup STDOUT: $!") 8 0 8
52 8 ▶ open(STDOUT , '>' , $tempfile) or croak "Can't redirect STDOUT to \$tempfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $tempfile or croak("Can't redirect STDOUT to \$tempfile: $!") 8 0 8
53 8 ▶ open(my $olderr, '>&', \*STDERR) or croak "Can't dup STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $olderr, ">&", \*STDERR or croak("Can't dup STDERR: $!") 8 0 8
54 8 ▶ 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: $!") 8 0 8
55  
56 # Let the external command know the commit that's being checked in
57 # case it needs to grok something from Git.
58 8 local $ENV{GIT_COMMIT} = $commit;
59 8 $exit = system $cmd;
60  
61 8 ▶ open(STDOUT, '>&', $oldout) or croak "Can't dup \$oldout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or croak("Can't dup \$oldout: $!") 8 0 8
62 8 ▶ open(STDERR, '>&', $olderr) or croak "Can't dup \$olderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or croak("Can't dup \$olderr: $!") 8 0 8
63 ## use critic
64  
65 8 $output = $tempfile->slurp;
66 }
67  
68 8 ▶ if ($exit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit != 0) { } 4 4 8
69 4 $command =~ s/\{\}/\'$file\'/g;
70 4 my $message = do {
71 4 ▶ if ($exit == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exit == -1) { } 0 4 4
elsif ($exit & 127) { } 0 4 4
72 0 "Command '$command' could not be executed: $!";
73 } elsif ($exit & 127) {
74 0 ▶ sprintf("Command '%s' was killed by signal %d, %s coredump",
Branch: 1 branch
BranchTrueFalseTotal
$exit & 128 ? : 0 0 0
75 $command, ($exit & 127), ($exit & 128) ? 'with' : 'without');
76 } else {
77 4 sprintf("Command '%s' failed with exit code %d", $command, $exit >> 8);
78 }
79 };
80  
81 # Replace any instance of the $tmpfile name in the output by
82 # $file to avoid confounding the user.
83 4 $output =~ s/\Q$tmpfile\E/$file/g;
84  
85 4 $git->fault($message, {%$ctx, details => $output});
86 4 return 1;
87 } else {
88 # FIXME: What should we do with eventual output from a
89 # successful command?
90 }
91 4 return 0;
92 }
93  
94 28 ▶sub check_commands {
Subroutine check_commands: called CC=6 SCAR=17.9
Pod undocumented
95 28 my ($git, $ctx, $commit, $files) = @_;
96  
97 # Construct a list of command checks from the
98 # githooks.checkfile.name configuration. Each check in the list is a
99 # pair containing a regex and a command specification.
100 28 my @name_checks;
101 28 foreach my $check ($git->get_config($CFG => 'name')) {
102 9 my ($pattern, $command) = split ' ', $check, 2;
103 9 ▶ if ($pattern =~ m/^qr(.)(.*)\g{1}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^qr(.)(.*)\g{1}/u) { } 2 7 9
104 2 $pattern = qr/$2/;
105 } else {
106 7 $pattern = glob_to_regex($pattern);
107 }
108 9 ▶ $command .= ' {}' unless $command =~ /\{\}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $command =~ /\{\}/u 8 1 9
109 9 push @name_checks, [$pattern => $command];
110 }
111  
112 28 my $errors = 0;
113  
114 28 foreach my $file (@$files) {
115 28 my $basename = path($file)->basename;
116  
117 28 foreach my $command (map {$_->[1]} grep {$basename =~ $_->[0]} @name_checks) {
118 8 $errors += check_command($git, $ctx, $commit, $file, $command);
119 } continue {
120 8 $git->check_timeout();
121 }
122 }
123  
124 28 return $errors;
125 }
126  
127 43 ▶sub check_sizes {
Subroutine check_sizes: called CC=9 SCAR=22.0
Pod undocumented
128 43 my ($git, $ctx, $commit, $files) = @_;
129  
130 # See if we have to check a file size limit
131 43 my $sizelimit = $git->get_config_integer($CFG => 'sizelimit');
132  
133 # Grok all REGEXP checks
134 43 my %re_checks;
135 43 foreach ($git->get_config("$CFG.basename" => 'sizelimit')) {
136 1 my ($bytes, $regexp) = split ' ', $_, 2;
137 1 unshift $re_checks{basename}{sizelimit}->@*, [qr/$regexp/, $bytes];
138 }
139  
140 43 ▶ return 0 unless $sizelimit || %re_checks;
Branch: 1 branch
BranchTrueFalseTotal
unless $sizelimit or %re_checks 35 8 43
Condition66%2/3
$sizelimit or %re_checks
l!l&&r!l&&!r
8035
MC/DC50%1/2
$sizelimit or %re_checks
$sizelimit %re_checks
Truth table66%2/3
A or B
A= $sizelimitB= %re_checks
ABresult
1X1
011
000
141  
142 8 my $errors = 0;
143  
144 8 foreach my $file (@$files) {
145 8 my $basename = path($file)->basename;
146  
147 8 my $size = $git->file_size($commit, $file);
148  
149 8 my $file_sizelimit = $sizelimit;
150 8 foreach my $spec ($re_checks{basename}{sizelimit}->@*) {
151 1 ▶ if ($basename =~ $spec->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename =~ /$spec->[0]/u) 1 0 1
152 1 $file_sizelimit = $spec->[1];
153 1 last;
154 }
155 }
156  
157 8 ▶ if ($file_sizelimit && $file_sizelimit < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_sizelimit and $file_sizelimit < $size) 4 4 8
Condition66%2/3
$file_sizelimit and $file_sizelimit < $size
!ll&&!rl&&r
044
MC/DC50%1/2
$file_sizelimit and $file_sizelimit < $size
$file_sizelimit $file_sizelimit < $size
Truth table66%2/3
A and B
A= $file_sizelimitB= $file_sizelimit < $size
ABresult
0X0
100
111
158 4 $git->fault(<<"EOS", {%$ctx, option => '[basename.]sizelimit'});
159 The file '$file' is too big.
160  
161 It has $size bytes but the current limit is $file_sizelimit bytes.
162 Please, check your configuration options.
163 EOS
164 4 ++$errors;
165 }
166 }
167  
168 8 return $errors;
169 }
170  
171 43 ▶sub check_executables {
Subroutine check_executables: called CC=11 SCAR=24.2
Pod undocumented
172 43 my ($git, $ctx, $commit, $files) = @_;
173  
174 # Grok the list of patterns to check for executable permissions
175 43 my %executable_checks;
176 43 foreach my $check (qw/executable not-executable/) {
177 86 foreach my $pattern ($git->get_config($CFG => $check)) {
178 8 ▶ if ($pattern =~ m/^qr(.)(.*)\g{1}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^qr(.)(.*)\g{1}/u) { } 0 8 8
179 0 $pattern = qr/$2/;
180 } else {
181 8 $pattern = glob_to_regex($pattern);
182 }
183 8 push $executable_checks{$check}->@*, $pattern;
184 }
185 }
186  
187 43 ▶ return 0 unless %executable_checks;
Branch: 1 branch
BranchTrueFalseTotal
unless %executable_checks 39 4 43
188  
189 4 my $errors = 0;
190  
191 FILE:
192 4 foreach my $file (@$files) {
193 4 my $basename = path($file)->basename;
194  
195 4 my $mode;
196  
197 4 ▶ if (any {$basename =~ $_} $executable_checks{'executable'}->@*) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $basename =~ /$_/u; } , @{$executable_checks{"executable"}})) 2 2 4
198 2 $mode = $git->file_mode($commit, $file);
199 2 ▶ unless ($mode & 0b1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode & 1) 1 1 2
200 1 $git->fault(<<"EOS", {%$ctx, option => 'executable'});
201 The file '$file' is not executable but should be.
202 Please, check your configuration options.
203 EOS
204 1 ++$errors;
205 }
206 }
207  
208 4 ▶ if (any {$basename =~ $_} $executable_checks{'not-executable'}->@*) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $basename =~ /$_/u; } , @{$executable_checks{"not-executable"}})) 2 2 4
209 2 ▶ if (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 0 2 2
210 0 git->fault(<<"EOS", {%$ctx, option => '[not-]executable'});
211 Configuration error: The file '$file' matches a 'executable' and a
212 'not-executable' option simultaneously, which is inconsistent.
213 Please, fix your configuration so that it matches only one of these options.
214 EOS
215 0 ++$errors;
216 }
217 2 $mode = $git->file_mode($commit, $file);
218 2 ▶ if ($mode & 0b1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode & 1) 1 1 2
219 1 $git->fault(<<"EOS", {%$ctx, option => 'not-executable'});
220 The file '$file' is executable but should not be.
221 Please, check your configuration options.
222 EOS
223 1 ++$errors;
224 }
225 }
226 }
227  
228 4 return $errors;
229 }
230  
231 23 ▶sub deny_case_conflicts {
Subroutine deny_case_conflicts: called CC=11 SCAR=27.6
Pod undocumented
232 23 my ($git, $ctx, $commit, $files) = @_;
233  
234 23 ▶ return 0 unless $git->get_config_boolean($CFG => 'deny-case-conflict');
Branch: 1 branch
BranchTrueFalseTotal
unless $git->get_config_boolean($CFG, "deny-case-conflict") 21 2 23
235  
236 # Grok the list of all files in the repository at $commit
237 2 ▶ my @ls_files = split(
Branch: 1 branch
BranchTrueFalseTotal
$commit ne ':0' ? : 1 1 2
238 /\0/,
239 $git->run(qw/ls-tree -r -z --name-only --full-tree/,
240 $commit ne ':0' ? $commit : $git->get_head_or_empty_tree),
241 );
242  
243 2 my $errors = 0;
244  
245 # Check if the new files conflict with each other
246 2 ▶ for (my $i = 0; $i < $#$files; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $files->$#*) 0 2 2
247 0 ▶ for (my $j = $i + 1; $j <= $#$files; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $files->$#*) 0 0 0
248 0 ▶ if (lc($files->[$i]) eq lc($files->[$j]) &&
Branch: 1 branch
BranchTrueFalseTotal
if (lc $files->[$i] eq lc $files->[$j] and $files->[$i] ne $files->[$j]) 0 0 0
Condition0%0/3
lc $files->[$i] eq lc $files->[$j] and $files->[$i] ne $files->[$j]
!ll&&!rl&&r
000
MC/DC0%0/2
lc $files->[$i] eq lc $files->[$j] and $files->[$i] ne $files->[$j]
lc $files->[$i] eq lc $files->[$j] $files->[$i] ne $files->[$j]
Truth table0%0/3
A and B
A= lc $files->[$i] eq lc $files->[$j]B= $files->[$i] ne $files->[$j]
ABresult
0X0
100
111
249 $files->[$i] ne $files->[$j]) {
250 0 ++$errors;
251 0 $git->fault(<<"EOS", {%$ctx, option => 'deny-case-conflict'});
252 This commit adds two files with names that will conflict
253 with each other in the repository in case-insensitive
254 filesystems:
255  
256 $files->[$i]
257 $files->[$j]
258  
259 Please, rename the added files to avoid the conflict and amend your commit.
260 EOS
261 }
262 }
263 }
264  
265 # Check if the new files conflict with already existing files
266 2 foreach my $file (@ls_files) {
267 5 my $lc_file = lc $file;
268 5 foreach my $name (@$files) {
269 5 my $lc_name = lc $name;
270 5 ▶ if ($lc_name eq $lc_file && $name ne $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_name eq $lc_file and $name ne $file) 3 2 5
Condition100%3/3
$lc_name eq $lc_file and $name ne $file
!ll&&!rl&&r
113
MC/DC100%2/2
$lc_name eq $lc_file and $name ne $file
$lc_name eq $lc_file $name ne $file
Truth table100%3/3
A and B
A= $lc_name eq $lc_fileB= $name ne $file
ABresult
0X0
100
111
271 3 ++$errors;
272 3 $git->fault(<<"EOS", {%$ctx, option => 'deny-case-conflict'});
273 This commit adds a file with a name that will conflict
274 with the name of another file already existing in the repository
275 in case-insensitive filesystems:
276  
277 ADDED: $name
278 EXISTING: $file
279  
280 Please, rename the added file to avoid the conflict and amend your commit.
281 EOS
282 }
283 }
284 }
285  
286 2 return $errors;
287 }
288  
289 43 ▶sub check_locks {
Subroutine check_locks: called CC=9 SCAR=22.0
Pod undocumented
290 43 my ($git, $ctx, $commit, $files) = @_;
291  
292 43 my @lock_checks;
293  
294 43 foreach my $pattern ($git->get_config($CFG => 'lock')) {
295 4 ▶ if ($pattern =~ m/^qr(.)(.*)\g{1}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^qr(.)(.*)\g{1}/u) { } 0 4 4
296 0 $pattern = qr/$2/;
297 } else {
298 4 $pattern = glob_to_regex($pattern);
299 }
300 4 push @lock_checks, qr/$pattern/i;
301 }
302  
303 43 ▶ return 0 unless @lock_checks;
Branch: 1 branch
BranchTrueFalseTotal
unless @lock_checks 39 4 43
304  
305 4 my @files_to_check;
306  
307 4 foreach my $file (@$files) {
308 push @files_to_check, $file
309 4 ▶ if any {$file =~ $_} @lock_checks;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $file =~ /$_/u; } , @lock_checks) 3 1 4
310 }
311  
312 4 ▶ return 0 unless @files_to_check;
Branch: 1 branch
BranchTrueFalseTotal
unless @files_to_check 1 3 4
313  
314 # Grok the list of all lock files in the repository at $commit
315 my @lock_files = split(
316 /\0/,
317 $git->run(qw/ls-tree -z --name-only --full-tree/,
318 $commit ne ':0' ? $commit : $git->get_head_or_empty_tree,
319 '--',
320 3 ▶ map {"$_.lock"} @files_to_check,
Branch: 1 branch
BranchTrueFalseTotal
$commit ne ':0' ? : 0 3 3
321 ),
322 );
323  
324 3 ▶ return 0 unless @lock_files;
Branch: 1 branch
BranchTrueFalseTotal
unless @lock_files 1 2 3
325  
326 2 $git->fault(<<"EOS", {%$ctx, option => 'lock'});
327 This commit adds or changes files that are locked. A file is considered locked
328 if there is another file with the same name and the '.lock' suffix in the same
329 commit. You should see who added the lock file and coordinate with them how to
330 integrate your changes in the changes that they may have already be working
331 on. Wait for them to perform a commit and to remove the lock file. Then, you
332 should create a lock of your own, manually integrate your changes into theirs,
333 and commit your changes while removing your lock.
334  
335 These are the locked files that this commit tried to add or change:
336  
337 2 @{[join("\n ", map {s/\.lock$//r} @lock_files)]}
338 EOS
339  
340 2 return scalar @lock_files;
341 }
342  
343 23 ▶sub check_max_paths {
Subroutine check_max_paths: called CC=3 SCAR=11.0
Pod undocumented
344 23 my ($git, $ctx, $commit, $files) = @_;
345  
346 # See if we have to check a file size limit
347 23 my $max_path = $git->get_config_integer($CFG => 'max-path');
348  
349 23 ▶ return 0 unless $max_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $max_path 21 2 23
350  
351 2 my @bigs = grep {length > $max_path} @$files;
352  
353 2 ▶ if (@bigs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bigs) 1 1 2
354 1 $git->fault(<<"EOS", {%$ctx, option => 'max-path'});
355 The following files have paths more than $max_path characters long.
356  
357 1 @{[join("\n ", @bigs)]}
358  
359 Git may not be able to check them out on a Windows host due to its maximum path
360 length limit of 260 characteres. See:
361 https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
362  
363 Please, ammend your changes to shorten their paths.
364 EOS
365 }
366  
367 2 return @bigs;
368 }
369  
370 # Assign meaningful names to action codes.
371 46 my %ACTION = (
372 A => 'add',
373 M => 'modify',
374 D => 'delete',
375 );
376  
377 46 ▶sub check_acls {
Subroutine check_acls: called CC=14 SCAR=26.5
Pod undocumented
378 46 my ($git, $ctx, $name2status) = @_;
379  
380 46 my @acls = eval { $git->grok_acls($CFG, 'AMD') };
381 46 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 45 46
382 1 $git->fault($@, $ctx);
383 1 return 1;
384 }
385  
386 45 ▶ return 0 unless @acls;
Branch: 1 branch
BranchTrueFalseTotal
unless @acls 37 8 45
387  
388 # Collect the ACL errors and group them by ACL/ACTION so that we can produce
389 # more compact error messages.
390 8 my %acl_errors;
391  
392 FILE:
393 8 foreach my $file (sort keys %$name2status) {
394 8 my $statuses = $name2status->{$file};
395 8 foreach my $acl (@acls) {
396 11 ▶ next unless ref $acl->{spec} ? $file =~ $acl->{spec} : $file eq $acl->{spec};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $acl->{"spec"} ? $file =~ /$acl->{"spec"}/u : $file eq $acl->{"spec"} 0 11 11
ref $acl->{'spec'} ? : 2 9 11
397  
398 # $status is usually a single letter but it can be a string of
399 # letters if we grokked affected files in a merge commit. So, we
400 # consider a match if the intersection of the two strings ($statuses
401 # and $acl->{action}) is not empty.
402 11 ▶ next if none {index($acl->{action}, $_) >= 0} split //, $statuses;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { index($acl->{"action"}, $_) != -1; } , split(//u, $statuses, 0)) 3 8 11
403  
404 8 ▶ unless ($acl->{allow}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($acl->{"allow"}) 4 4 8
405 4 ▶ my $action = $ACTION{$statuses} || $statuses;
Condition33%1/3
$ACTION{$statuses} || $statuses
l!l&&r!l&&!r
400
MC/DC0%0/2
$ACTION{$statuses} || $statuses
$ACTION{$statuses} $statuses
Truth table33%1/3
A || B
A= $ACTION{$statuses}B= $statuses
ABresult
1X1
011
000
406 4 push $acl_errors{$acl->{acl}}{$action}->@*, $file;
407 }
408  
409 8 next FILE;
410 }
411 }
412  
413 8 ▶ if (%acl_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (%acl_errors) 4 4 8
414 4 my $myself = $git->authenticated_user();
415 4 my %context = (%$ctx, option => 'acl');
416 4 ▶ while (my ($acl, $actions) = each %acl_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %acl_errors) 4 4 8
417 4 ▶ while (my ($action, $files) = each %$actions) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$actions) 4 4 8
418 4 ▶ my $these_files = scalar(@$files) > 1 ? 'these files' : 'this file';
Branch: 1 branch
BranchTrueFalseTotal
scalar @$files > 1 ? : 0 4 4
419 4 $git->fault(<<"EOS", \%context);
420 Authorization error: you ($myself) cannot $action $these_files:
421  
422 4 @{[join("\n ", @$files)]}
423  
424 Due to the following acl:
425  
426 $acl
427 EOS
428 }
429 }
430 }
431  
432 8 return scalar %acl_errors;
433 }
434  
435 46 ▶sub check_everything {
Subroutine check_everything: called CC=9 SCAR=22.0
Pod undocumented
436 46 my ($git, $ref, $commit, $extra) = @_;
437  
438 # The $extra information was generated by the --name-status --cc options to
439 # git-log. It has one line for each file affected in the commit. Merge
440 # commits only show files with conflicts. The format is "<S>+\t<FILE>". <S>
441 # is one letter indicating how the file was affected, as documented in the
442 # --diff-filter option. <FILE> is the file path since the repository root,
443 # without a leading slash.
444  
445 46 my %name2status;
446  
447 46 ▶ if (defined $extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extra) 46 0 46
448 46 foreach (split /\n/, $extra) {
449 46 ▶ if (/^(?<status>[ACDMRTUXB0-9]+)\t(?<file>.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?<status>[ACDMRTUXB0-9]+)\t(?<file>.+)/u) 46 0 46
450 46 my ($status, $file) = ($+{status}, $+{file});
451 46 ▶ if ($file =~ /^\".*\"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^\".*\"$/u) 2 44 46
452 # Pathnames with "unusual" characters are quoted as
453 # explained for the configuration variable core.quotePath
454 # (see git-config(1)): "by enclosing the pathname in
455 # double-quotes and escaping those characters with
456 # backslashes in the same way C escapes control characters
457 # (e.g. \t for TAB, \n for LF, \\ for backslash) or bytes
458 # with values larger than 0x80 (e.g. octal \302\265 for
459 # "micro" in UTF-8)."
460  
461 # The section "Quote and Quote-like Operators" of perlop
462 # explains how Perl's string literal syntax is an (almost)
463 # superset of C's. The only C escape that Perl doesn't have
464 # is "\v", which can be expressed in Perl as "\x0b". So,
465 # first we use a s/// operator to replace any and all
466 # occurrences of \v.
467  
468 2 $file =~ s/(?<!\\)\\v/\x0b/g;
469  
470 # However, since we must evaluate the literal string as
471 # doubly-quoted we must take care to escape the $ and @
472 # sigils, or Perl will try to innterpolate them.
473  
474 2 $file =~ s/([\$\@])/\\$1/g;
475  
476 # Now, we can use eval to read the resulting string as if it
477 # were a Perl string literal.
478  
479 2 $file = eval $file; ## no critic (ProhibitStringyEval)
480 }
481 46 $name2status{$file} = $status;
482 }
483 }
484 }
485  
486 46 my %context = (ref => $ref);
487 46 ▶ $context{commit} = $commit unless $commit eq ':0';
Branch: 1 branch
BranchTrueFalseTotal
unless $commit eq ":0" 11 35 46
488  
489 46 my $errors = check_acls($git, \%context, \%name2status);
490  
491 46 ▶ if (my @AC_files = sort grep {$name2status{$_} =~ /[AC]/} keys %name2status) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@AC_files) = sort(grep({$name2status{$_} =~ /[AC]/u;} keys %name2status))) 23 23 46
492 23 $errors +=
493 deny_case_conflicts($git, \%context, $commit, \@AC_files) +
494 check_max_paths($git, \%context, $commit, \@AC_files);
495 }
496  
497 46 ▶ if (my @ACM_files = sort grep {$name2status{$_} =~ /[ACM]/} keys %name2status) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ACM_files) = sort(grep({$name2status{$_} =~ /[ACM]/u;} keys %name2status))) 43 3 46
498 43 $errors +=
499 check_executables($git, \%context, $commit, \@ACM_files) +
500 check_locks($git, \%context, $commit, \@ACM_files) +
501 check_sizes($git, \%context, $commit, \@ACM_files);
502 # Avoid external checks if there are errors already
503 43 ▶ $errors += check_commands($git, \%context, $commit, \@ACM_files)
Branch: 1 branch
BranchTrueFalseTotal
unless $errors 28 15 43
504 unless $errors;
505 }
506  
507 46 return $errors;
508 }
509  
510 11 ▶sub check_ref {
Subroutine check_ref: called CC=2 SCAR=6.9
Pod undocumented
511 11 my ($git, $ref) = @_;
512  
513 11 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
514  
515 11 my @commits = $git->get_commits(
516 $old_commit,
517 $new_commit,
518 [qw/--name-status --ignore-submodules -r --cc/],
519 );
520  
521 11 my $errors = 0;
522  
523 11 foreach my $commit (@commits) {
524 11 $errors += check_everything($git, $ref, $commit->commit, $commit->extra);
525 }
526  
527 11 return $errors;
528 }
529  
530 35 ▶sub check_commit {
Subroutine check_commit: called CC=1 SCAR=0.0
Pod undocumented
531 35 my ($git, $current_branch) = @_;
532  
533 35 my $extra = $git->run(qw/diff-index --name-status --ignore-submodules --no-commit-id --cached -r/,
534 $git->get_head_or_empty_tree);
535  
536 35 return check_everything(
537 $git,
538 $current_branch,
539 ':0', # mark to signify the index
540 $extra,
541 );
542 }
543  
544 0 ▶sub check_patchset {
Subroutine check_patchset: not called CC=1 SCAR=6.9
Pod undocumented
545 0 my ($git, $branch, $commit) = @_;
546  
547 0 return check_everything($git, $branch, $commit->commit, $commit->extra);
548 }
549  
550 # Install hooks
551 46 my $options = {config => \&_setup_config};
552  
553 46 GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref, $options);
554 46 GITHOOKS_CHECK_PRE_COMMIT(\&check_commit, $options);
555 46 GITHOOKS_CHECK_PATCHSET(\&check_patchset, $options);
556  
557 46 1;
558  
559 __END__
« lib/Git/Hooks/Test.pm ↑ Summary blib/lib/Git/Hooks.pm »