blib/lib/Git/Hooks/CheckDiff.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 39 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 231 CC 20 SCAR 36.2
CC
20
Cov
0%
_check_token
90.0
_check_ref
42.0
_check_command
30.0
CRAP 37.4
1 use warnings;
2  
3 package Git::Hooks::CheckDiff;
4 # ABSTRACT: Git::Hooks plugin to enforce commit policies
5 $Git::Hooks::CheckDiff::VERSION = '3.3.1';
6 use v5.16.0;
7 use utf8;
8 use Carp;
9 use Log::Any '$log';
10 use Git::Hooks;
11 use Path::Tiny;
12  
13 my $PKG = __PACKAGE__;
14 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
15  
16 # Install hooks
17 GITHOOKS_CHECK_AFFECTED_REFS(\&_check_ref);
18 GITHOOKS_CHECK_PRE_COMMIT(\&check_commit);
19 GITHOOKS_CHECK_PATCHSET(\&check_patchset);
20  
21 sub check_commit {
22 my ($git, $current_branch) = @_;
23  
24 return _check_everything($git, {ref => $current_branch}, qw/diff-index --cached HEAD/);
25 }
26  
27 sub check_patchset {
28 my ($git, $branch, $commit) = @_;
29  
30 return _check_everything($git, {ref => $branch, commit => $commit}, 'diff-tree', $commit);
31 }
32  
33 sub _check_ref {
34 my ($git, $ref) = @_;
35  
36 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 return 0 if $new_commit eq $git->undef_commit;
40  
41 # If the reference is being created we have to calculate a proper
42 # $old_commit to diff against.
43 if ($old_commit eq $git->undef_commit) {
44 my $last_log;
45 my $log_iterator = $git->log($new_commit, qw/--not --all/);
46 while (my $log = $log_iterator->next()) {
47 $last_log = $log;
48 }
49 return 0 unless $last_log;
50 my @parents = $last_log->parent;
51 if (@parents == 0) {
52 # We reached the repository root. Hence, let's consider
53 # $old_commit to be the empty tree.
54 $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 $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 $old_commit = $last_log->commit;
64 }
65 }
66  
67 return _check_everything($git, {ref => $ref}, 'diff-tree', $old_commit, $new_commit);
68 }
69  
70 sub _check_everything {
71 my ($git, $ctx, $git_cmd, @git_args) = @_;
72  
73 my $diff_text;
74 my $diff = sub {
75 $diff_text //= $git->run(
76 $git_cmd, qw/-p -U0 --no-color --diff-filter=AM --no-prefix/, @git_args
77 );
78 return $diff_text;
79 };
80  
81 return 0 == (
82 _check_shell($git, $ctx, $diff) +
83 _check_token($git, $ctx, $diff)
84 )
85 }
86  
87 sub _check_shell {
88 my ($git, $ctx, $diff) = @_;
89  
90 my @commands = $git->get_config($CFG => 'shell');
91  
92 return 0 unless @commands;
93  
94 my $diff_file = _diff_file($diff);
95  
96 unless ($diff_file) {
97 $git->fault("git diff failed", {%$ctx});
98 return 1;
99 }
100  
101 my $errors = 0;
102  
103 foreach my $command (@commands) {
104 $errors += _check_command($git, $ctx, $command, $diff_file);
105 } continue {
106 $git->check_timeout();
107 }
108  
109 return $errors;
110 }
111  
112 sub _diff_file {
113 my ($diff) = @_;
114  
115 my $file = Path::Tiny->tempfile();
116  
117 $file->spew($diff->());
118  
119 return $file;
120 }
121  
122 sub _check_command {
123 my ($git, $ctx, $command, $input_file) = @_;
124  
125 # execute command and update $errors
126 my ($exit, $output);
127 {
128 my $output_file = Path::Tiny->tempfile();
129  
130 ## no critic (RequireBriefOpen, RequireCarping)
131 open(my $oldin, '<&', \*STDIN) or croak "Can't dup STDIN: $!";
132 open(STDIN , '<' , $input_file) or croak "Can't redirect STDIN to \$input_file: $!";
133 open(my $oldout, '>&', \*STDOUT) or croak "Can't dup STDOUT: $!";
134 open(STDOUT , '>' , $output_file) or croak "Can't redirect STDOUT to \$output_file: $!";
135 open(my $olderr, '>&', \*STDERR) or croak "Can't dup STDERR: $!";
136 open(STDERR , '>&', \*STDOUT) or croak "Can't dup STDOUT for STDERR: $!";
137  
138 $exit = system('/bin/sh', '-c', $command);
139  
140 open(STDIN, '<&', $oldin) or croak "Can't dup \$oldin: $!";
141 open(STDOUT, '>&', $oldout) or croak "Can't dup \$oldout: $!";
142 open(STDERR, '>&', $olderr) or croak "Can't dup \$olderr: $!";
143 ## use critic
144  
145 $output = $output_file->slurp;
146 }
147  
148 if ($exit != 0) {
149 my $message = do {
150 if ($exit == -1) {
151 "Command '$command' could not be executed: $!";
152 } elsif ($exit & 127) {
153 sprintf("Command '%s' was killed by signal %d, %s coredump",
154 $command, ($exit & 127), ($exit & 128) ? 'with' : 'without');
155 } else {
156 sprintf("Command '%s' failed with exit code %d", $command, $exit >> 8);
157 }
158 };
159  
160 $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 return $exit != 0;
167 }
168  
169 sub _check_token {
170 my ($git, $ctx, $diff) = @_;
171  
172 my @deny_tokens = $git->get_config($CFG => 'deny-token')
173 or return 0;
174  
175 my $errors = 0;
176  
177 foreach my $deny_token (@deny_tokens) {
178 my ($regex, $filters) = split /\s+--\s+/, $deny_token, 2;
179  
180 my $match_token = qr/^\+.*?$regex/; # FIXME: detect error
181  
182 my @filters;
183  
184 if ($filters) {
185 foreach my $filter (split ' ', $filters) {
186 my $negated;
187 if ($filter =~ s/^\!//) {
188 $negated = 1;
189 }
190 if ($filter =~ m/^\^/) {
191 $filter = qr/$filter/;
192 }
193 push @filters, [$negated, $filter];
194 }
195 }
196  
197 my $file = '';
198 my @matches;
199  
200 LINE:
201 foreach (split /\n/, $diff->()) {
202 if (/^\+\+\+ (.+)/) {
203 $file = $1;
204 if (@filters) {
205 foreach my $filter (@filters) {
206 if ($filter->[0] xor ## no critic (ProhibitDeepNests)
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 next LINE;
212 }
213 }
214 $file = '';
215 }
216 } elsif (length $file && $_ =~ $match_token) {
217 push @matches, "$file: $_";
218 }
219 }
220  
221 if (@matches) {
222 $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 $errors += 1;
227 }
228 }
229  
230 return $errors;
231 }
232  
233 1;
234  
235 __END__