blib/lib/Config/Patch.pm

filter: Statementstmt 83.0% 332 / 400 Branchbran 61.0% 105 / 172 Conditioncond 82.1% 23 / 28 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 92.0% 58 / 63 Podpod 46.1% 12 / 26 totaltotal 76.8% 543 / 707 CC 100 SCAR 16.7
CC
100
Cov
77%
unlock
12.6
lock
12.2
data_traverse
12.0
CRAP 5.3
↑ Summary
1 ##################################################
2 package Config::Patcher::Util;
3 ##################################################
4  
5 ##################################################
6 # Poor man's Class::Struct
7 ##################################################
8 60 ▶sub make_accessor {
Subroutine make_accessor: called CC=3 SCAR=13.6
9 ##################################################
10 60 my($package, $name) = @_;
11  
12 4 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 60 my $code = <<EOT;
15 *{"$package\\::$name"} = sub {
16 my(\$self, \$value) = \@_;
17  
18 if(defined \$value) {
19 \$self->{$name} = \$value;
20 }
21 if(exists \$self->{$name}) {
22 return (\$self->{$name});
23 } else {
24 return "";
25 }
26 }
27 EOT
28 60 ▶ if(! defined *{"$package\::$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined *{"${package}::$name";}) 60 0 60
29 0 ▶ eval $code or die "$@";
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: not called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Subroutine __ANON__: not called CC=3 SCAR=13.9
Subroutine __ANON__: not called CC=3 SCAR=13.9
Subroutine __ANON__: called CC=3 SCAR=13.9
Branch: 31 branches
BranchTrueFalseTotal
eval $code or die "$@" 60 0 60
if (defined $value) 0 98 98
if (exists $self->{'mode'}) { } 98 0 98
if (defined $value) 0 56 56
if (exists $self->{'key'}) { } 56 0 56
if (defined $value) 21 85 106
if (exists $self->{'text'}) { } 106 0 106
if (defined $value) 0 33 33
if (exists $self->{'pos_from'}) { } 33 0 33
if (defined $value) 0 33 33
if (exists $self->{'pos_to'}) { } 33 0 33
if (defined $value) 0 8 8
if (exists $self->{'header'}) { } 8 0 8
if (defined $value) 0 12 12
if (exists $self->{'content_pos_from'}) { } 12 0 12
if (defined $value) 0 9 9
if (exists $self->{'content_pos_to'}) { } 9 0 9
if (defined $value) 0 7 7
if (exists $self->{'regex'}) { } 7 0 7
if (defined $value) 0 0 0
if (exists $self->{'method'}) { } 0 0 0
if (defined $value) 0 1 1
if (exists $self->{'as_string'}) { } 1 0 1
if (defined $value) 5 11 16
if (exists $self->{'data'}) { } 16 0 16
if (defined $value) 0 0 0
if (exists $self->{'file'}) { } 0 0 0
if (defined $value) 0 0 0
if (exists $self->{'comment_char'}) { } 0 0 0
if (defined $value) 5 0 5
if (exists $self->{'key'}) { } 5 0 5
30 }
31 }
32  
33 ###########################################
34 package Config::Patch::Hunk;
35 ###########################################
36 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 our @accessors = qw(
39 mode key text pos_from pos_to header
40 content_pos_from content_pos_to regex method
41 as_string
42 );
43 4 Config::Patcher::Util::make_accessor( __PACKAGE__, $_ ) for @accessors;
44  
45 ###########################################
46 66 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
47 ###########################################
48 66 my($class, %options) = @_;
49  
50 66 my $self = {
51 comment_char => '#',
52 mode => "append",
53 key => undef,
54 text => undef,
55 %options,
56 };
57  
58 66 bless $self, $class;
59 }
60  
61 ###########################################
62 72 ▶sub patch_marker {
Subroutine patch_marker: called CC=1 SCAR=0.0
63 ###########################################
64 72 my($self) = @_;
65  
66 return $self->{comment_char} .
67 72 "(Config::Patch-" .
68 "$self->{key}-" .
69 $self->mode() .
70 ")" .
71 "\n";
72 }
73  
74 ###########################################
75 36 ▶sub string_generate {
Subroutine string_generate: called CC=1 SCAR=0.0
76 ###########################################
77 36 my($self) = @_;
78  
79 36 return $self->patch_marker() .
80 $self->text() .
81 $self->patch_marker();
82 }
83  
84 ###########################################
85 13 ▶sub freeze {
Subroutine freeze: called CC=1 SCAR=0.0
86 ###########################################
87 13 my($self, $string) = @_;
88  
89 # Hide an arbitrary string in a comment
90 13 my $encoded = encode_base64($string);
91  
92 13 $encoded =~ s/^/$self->{comment_char} /gm;
93 13 return $encoded;
94 }
95  
96 ###########################################
97 9 ▶sub thaw {
Subroutine thaw: called CC=1 SCAR=0.0
98 ###########################################
99 9 my($self, $string) = @_;
100  
101 # Decode a hidden string
102 9 $string =~ s/^$self->{comment_char} //gm;
103 9 my $decoded = decode_base64($string);
104 9 return $decoded;
105 }
106  
107 ###########################################
108 9 ▶sub replstring_extract {
Subroutine replstring_extract: called CC=2 SCAR=7.1
109 ###########################################
110 9 my($self) = @_;
111  
112 9 my $text = $self->text();
113  
114 # Find the replace string in a patch
115 9 my $replace_marker = $self->replace_marker();
116 9 $replace_marker = quotemeta($replace_marker);
117 9 ▶ if($text =~ /^$replace_marker\n(.*?)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^$replace_marker\n(.*?) ^$replace_marker/msx) 9 0 9
118 ^$replace_marker/xms) {
119 9 my $repl = $1;
120 9 $text =~ s/^$replace_marker.*?
121 ^$replace_marker\n//xms;
122  
123 9 return($self->thaw($repl), $text);
124 }
125  
126 0 return undef;
127 }
128  
129 ###########################################
130 13 ▶sub replstring_hide {
Subroutine replstring_hide: called CC=1 SCAR=0.0
131 ###########################################
132 13 my($self, $replstring) = @_;
133  
134 # Add a replace string to a patch
135 13 my $replace_marker = $self->replace_marker();
136 13 my $encoded = $replace_marker . "\n" .
137 $self->freeze($replstring) .
138 $replace_marker .
139 "\n";
140  
141 13 return $encoded;
142 }
143  
144 ###########################################
145 22 ▶sub replace_marker {
Subroutine replace_marker: called CC=1 SCAR=0.0
146 ###########################################
147 22 my($self) = @_;
148  
149 return $self->{comment_char} .
150 22 "(Config::Patch::replace)";
151 }
152  
153 ###########################################
154 package Config::Patch;
155 ###########################################
156 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 4 ▶use Set::IntSpan;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 4 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
160 4 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 4 our $VERSION = "0.09";
163  
164 4 our @accessors = qw(data file comment_char key);
165 4 Config::Patcher::Util::make_accessor( __PACKAGE__, $_ ) for @accessors;
166  
167 ###########################################
168 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
169 ###########################################
170 20 my($class, %options) = @_;
171  
172 20 my $self = {
173 comment_char => '#',
174 key => undef,
175 file => undef,
176 parsed => 0,
177 read => 0,
178 %options,
179 };
180  
181 20 my $package = __PACKAGE__;
182  
183 $self->{patch_regex} =
184 20 qr{^$self->{comment_char}\($package-(.*)-(.*?)\)}m;
185  
186 20 bless $self, $class;
187 }
188  
189 ###########################################
190 21 ▶sub read {
Subroutine read: called CC=3 SCAR=11.4
Pod documented
191 ###########################################
192 21 my($self, $file) = @_;
193  
194 21 ▶ if(defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 21 21
195 0 $self->{file} = $file;
196 }
197  
198 21 $self->{data} = $self->slurp( $self->{file} );
199  
200 # fix trailing newline if it's missing
201 21 ▶ $self->{data} .= "\n" unless substr($self->{data}, -1, 1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($self->{"data"}, -1, 1) eq "\n" 0 21 21
202  
203 21 $self->{parsed} = 0;
204 21 $self->{read} = 1;
205  
206 21 return $self->{data};
207 }
208  
209 ###########################################
210 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
211 ###########################################
212 0 my($self, @text) = @_;
213  
214 0 ▶ if(defined $text[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text[0]) 0 0 0
215 0 $self->{error} = join "", @text;
216 0 ERROR $self->{error};
217 }
218  
219 0 return $self->{error};
220 }
221  
222 ###########################################
223 6 ▶sub patch_by_stretch {
Subroutine patch_by_stretch: called CC=3 SCAR=11.3
Pod undocumented
224 ###########################################
225 6 my($self, $text, $mode) = @_;
226  
227 LOGDIE "No key defined" unless
228 6 ▶ defined $self->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key"} 0 6 6
229  
230 6 ▶ LOGDIE "No mode defined" unless
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 6 6
231 defined $mode;
232  
233 my $patch = Config::Patch::Hunk->new(
234 comment_char => $self->{comment_char},
235 key => $self->{key},
236 6 text => $text,
237 mode => $mode,
238 );
239  
240 6 return $self->apply( $patch );
241 }
242  
243 ###########################################
244 15 ▶sub apply {
Subroutine apply: called CC=9 SCAR=22.3
Pod documented
245 ###########################################
246 15 my($self, $patch) = @_;
247  
248 15 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 1 14 15
249  
250 15 $patch->{comment_char} = $self->{comment_char};
251  
252 15 my $key = $patch->key();
253  
254 # TODO: Lower-level functions expect it there, but probably should
255 # be carried as an argument.
256 15 $self->{key} = $key;
257  
258 15 my $patchtext = $patch->string_generate();
259
260 15 ▶ if ($patch->{mode} eq "prepend") {
Branch: 7 branches
BranchTrueFalseTotal
if ($patch->{'mode'} eq 'prepend') { } 1 14 15
elsif ($patch->{'mode'} eq 'append') { } 5 9 14
elsif ($patch->{'mode'} eq 'replace') { } 2 7 9
elsif ($patch->{'mode'} eq 'insert-before') { } 4 3 7
elsif ($patch->{'mode'} eq 'insert-after') { } 1 2 3
elsif ($patch->{'mode'} eq 'update') { } 2 0 2
elsif ($patch->{'mode'} eq 'comment_out') { } 0 0 0
261 1 $self->{data} = $patchtext . $self->{data};
262  
263 } elsif ($patch->{mode} eq "append") {
264 5 $self->{data} .= $patchtext;
265  
266 } elsif ($patch->{mode} eq "replace") {
267 2 $self->patch_by_wedge($patch->regex(), $patch->text(), "replace");
268  
269 } elsif ($patch->{mode} eq "insert-before") {
270 4 $self->patch_by_wedge($patch->regex(), $patch->text(), "insert");
271  
272 } elsif ($patch->{mode} eq "insert-after") {
273 1 $self->patch_by_wedge($patch->regex(), $patch->text(), "insert", 1);
274  
275 } elsif ($patch->{mode} eq "update") {
276 2 $self->patch_update( $patch->key(), $patch->text() );
277  
278 } elsif ($patch->{mode} eq "comment_out") {
279 0 $self->patch_comment_out( $patch->key(), $patch->regex() );
280  
281 } else {
282 0 LOGDIE "Unknown mode '$patch->{mode}'";
283 }
284  
285 15 return 1;
286 }
287  
288 ###########################################
289 34 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
290 ###########################################
291 34 my($self) = @_;
292  
293 34 $self->blurt($self->{data}, $self->{file});
294 }
295  
296 ###########################################
297 0 ▶sub save_as {
Subroutine save_as: not called CC=2 SCAR=17.9
Pod documented
298 ###########################################
299 0 my($self, $file) = @_;
300  
301 0 ▶ LOGDIE "No file defined" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
302 0 $self->{file} = $file;
303  
304 0 return $self->save();
305 }
306  
307 ###########################################
308 20 ▶sub patched {
Subroutine patched: called CC=2 SCAR=6.9
Pod documented
309 ###########################################
310 20 my($self, $key) = @_;
311  
312 20 my @patches = $self->parse();
313  
314 20 ▶ if( grep { $key eq $_->key() } @patches ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$key eq $_->key;} @patches) 1 19 20
315 1 return 1;
316 }
317  
318 19 return 0;
319 }
320  
321 ###########################################
322 23 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
323 ###########################################
324 23 my($self) = @_;
325  
326 23 my @patches = ();
327  
328 23 $self->{forbidden_zones} = Set::IntSpan->new();
329  
330 6 ▶ $self->data_traverse( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
331 6 my($patcher, $patch) = @_;
332  
333 $patcher->{forbidden_zones} =
334 Set::IntSpan::union( $patcher->{forbidden_zones},
335 6 ($patch->pos_from() . "-" . $patch->pos_to()));
336  
337 6 push @patches, $patch;
338 29 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
339 23 });
340  
341 23 $self->{parsed} = 1;
342  
343 23 return @patches;
344 }
345  
346 ###########################################
347 2 ▶sub patch_update {
Subroutine patch_update: called CC=5 SCAR=17.0
Pod undocumented
348 ###########################################
349 2 my($self, $key, $newvalue) = @_;
350  
351 2 ▶ if(length $newvalue and
Branch: 1 branch
BranchTrueFalseTotal
if (length $newvalue and substr($newvalue, -1, 1) ne "\n") 2 0 2
Condition33%1/3
length $newvalue and substr($newvalue, -1, 1) ne "\n"
!ll&&!rl&&r
002
MC/DC0%0/2
length $newvalue and substr($newvalue, -1, 1) ne "\n"
length $newvalue substr($newvalue, -1, 1) ne "\n"
Truth table33%1/3
A and B
A= length $newvalueB= substr($newvalue, -1, 1) ne "\n"
ABresult
0X0
100
111
352 substr($newvalue, -1, 1) ne "\n") {
353 2 $newvalue .= "\n";
354 }
355  
356 2 my $skew = 0;
357  
358 2 for my $hunk ( $self->parse() ) {
359 3 ▶ next if $hunk->key() ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if $hunk->key ne $key 0 3 3
360  
361 3 substr($self->{data}, $hunk->content_pos_from + $skew,
362 $hunk->content_pos_to - $hunk->content_pos_from)
363 = $newvalue;
364  
365 3 $skew += length($newvalue) - length($hunk->text());
366 }
367 }
368  
369 ###########################################
370 1 ▶sub patches_only {
Subroutine patches_only: called CC=2 SCAR=6.9
Pod undocumented
371 ###########################################
372 1 my($self) = @_;
373  
374 1 my $new_content = "";
375  
376 1 for my $hunk ( $self->parse() ) {
377 1 $new_content .= $hunk->as_string();
378 }
379  
380 1 $self->{data} = $new_content;
381 }
382  
383 ###########################################
384 20 ▶sub patch_by_wedge {
Subroutine patch_by_wedge: called CC=9 SCAR=22.0
Pod undocumented
385 ###########################################
386 20 my($self, $search, $replace, $mode, $after) = @_;
387  
388 20 ▶ if($self->patched( $self->{key} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->patched($self->{"key"})) 1 19 20
389 1 INFO "$mode cancelled: File already patched with key $self->{key}";
390 1 return undef;
391 }
392  
393 19 ▶ if(ref($search) ne "Regexp") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $search ne "Regexp") 0 19 19
394 0 LOGDIE "$mode search parameter not a regex {$search}";
395 }
396  
397 19 ▶ if(length $replace and
Branch: 1 branch
BranchTrueFalseTotal
if (length $replace and substr($replace, -1, 1) ne "\n") 10 9 19
Condition100%3/3
length $replace and substr($replace, -1, 1) ne "\n"
!ll&&!rl&&r
1810
MC/DC100%2/2
length $replace and substr($replace, -1, 1) ne "\n"
length $replace substr($replace, -1, 1) ne "\n"
Truth table100%3/3
A and B
A= length $replaceB= substr($replace, -1, 1) ne "\n"
ABresult
0X0
100
111
398 substr($replace, -1, 1) ne "\n") {
399 10 $replace .= "\n";
400 }
401  
402 19 my $data = $self->{data};
403  
404 19 my $positions = $self->full_line_match($data, $search);
405 19 my @pieces = ();
406 19 my $rest = $data;
407 19 my $offset = 0;
408  
409 my $patch = Config::Patch::Hunk->new(
410 comment_char => $self->{comment_char},
411 key => $self->{key},
412 19 mode => $mode,
413 );
414  
415 19 for my $pos (@$positions) {
416 21 my($from, $to) = @$pos;
417 21 my($before, $trail);
418 21 my $hide;
419 21 ▶ if ($mode eq "insert" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'insert') { } 8 13 21
elsif ($mode eq 'replace') { } 13 0 13
420 8 ▶ if ($after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after) { } 2 6 8
421 2 $before = substr($data, $offset, $to+1);
422 2 $rest = substr($data, $to+1);
423 2 $hide = "";
424 2 $trail = "";
425 } else {
426 6 $before = substr($data, $offset, $from-$offset);
427 6 $rest = substr($data, $to+1);
428 6 $hide = "";
429 6 $trail = substr($data, $from, $to - $from + 1);
430 }
431 } elsif ($mode eq "replace") {
432 13 $before = substr($data, $offset, $from-$offset);
433 13 $rest = substr($data, $to+1);
434  
435 13 $hide = $patch->replstring_hide(
436 substr($data, $from, $to - $from + 1));
437 13 $trail = "";
438 }
439  
440 21 $patch->text( $replace . $hide );
441 21 push @pieces, $before, $patch->string_generate(), $trail;
442 21 $offset = $to + 1;
443 }
444  
445 19 push @pieces, $rest;
446  
447 19 $self->{data} = join '', @pieces;
448  
449 19 return 1;
450 }
451  
452 ###########################################
453 19 ▶sub full_line_match {
Subroutine full_line_match: called CC=10 SCAR=23.0
Pod undocumented
454 ###########################################
455 19 my($self, $string, $rex) = @_;
456  
457 19 DEBUG "Trying to match '$string' with /$rex/";
458  
459 # Try a regex match and if it succeeds, extend the match
460 # to cover the full first and last line. Return a ref to
461 # an array of from-to offsets of all (extended) matching
462 # regions.
463 19 my @positions = ();
464  
465 19 ▶ while($string =~ /($rex)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /($rex)/g) 26 19 45
466 26 my $first = pos($string) - length($1);
467 26 my $last = pos($string) - 1;
468  
469 26 DEBUG "Found match at pos $first-$last ($1) pos=", pos($string);
470  
471 # Is this match located in any of the forbidden zones?
472 my $intersect = Set::IntSpan::intersect(
473 26 $self->{forbidden_zones}, "$first-$last");
474 26 ▶ unless(Set::IntSpan::empty($intersect)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Set::IntSpan::empty($intersect)) 4 22 26
475 4 DEBUG "Match was in forbidden zone - skipped";
476 4 next;
477 }
478  
479 # Go back to the start of the line
480 22 ▶ while($first and
Branch: 1 branch
BranchTrueFalseTotal
if ($first and substr($string, $first, 1) ne "\n") 35 22 57
Condition100%3/3
$first and substr($string, $first, 1) ne "\n"
!ll&&!rl&&r
101235
MC/DC100%2/2
$first and substr($string, $first, 1) ne "\n"
$first substr($string, $first, 1) ne "\n"
Truth table100%3/3
A and B
A= $firstB= substr($string, $first, 1) ne "\n"
ABresult
0X0
100
111
481 substr($string, $first, 1) ne "\n") {
482 35 $first--;
483 }
484 22 ▶ $first += 1 if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 12 10 22
485  
486 # Proceed until the end of the line
487 22 ▶ while($last < length($string) and
Branch: 1 branch
BranchTrueFalseTotal
if ($last < length $string and substr($string, $last, 1) ne "\n") 31 22 53
Condition66%2/3
$last < length $string and substr($string, $last, 1) ne "\n"
!ll&&!rl&&r
02231
MC/DC50%1/2
$last < length $string and substr($string, $last, 1) ne "\n"
$last < length $string substr($string, $last, 1) ne "\n"
Truth table66%2/3
A and B
A= $last < length $stringB= substr($string, $last, 1) ne "\n"
ABresult
0X0
100
111
488 substr($string, $last, 1) ne "\n") {
489 31 $last++;
490 }
491  
492 22 DEBUG "Match positions corrected to $first-$last (line start/end)";
493  
494 # Ignore overlapping matches
495 22 ▶ if(@positions and $positions[-1]->[1] > $first) {
Branch: 1 branch
BranchTrueFalseTotal
if (@positions and $positions[-1][1] > $first) 1 21 22
Condition100%3/3
@positions and $positions[-1][1] > $first
!ll&&!rl&&r
1831
MC/DC100%2/2
@positions and $positions[-1][1] > $first
@positions $positions[-1][1] > $first
Truth table100%3/3
A and B
A= @positionsB= $positions[-1][1] > $first
ABresult
0X0
100
111
496 1 DEBUG "Detected overlap (two matches in same line) - skipped";
497 1 next;
498 }
499  
500 21 push @positions, [$first, $last];
501 }
502  
503 19 return \@positions;
504 }
505  
506 ###########################################
507 1 ▶sub comment_out {
Subroutine comment_out: called CC=1 SCAR=0.0
Pod undocumented
508 ###########################################
509 1 my($self, $search) = @_;
510  
511 # Same as "replace by nothing"
512 1 return $self->replace($search, "");
513 }
514  
515 ###########################################
516 19 ▶sub eject {
Subroutine eject: called CC=4 SCAR=13.9
Pod documented
517 ###########################################
518 19 my($self, $key) = @_;
519  
520 19 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 0 19 19
521  
522 # We accept a hunk instead of a key as well
523 19 ▶ if(ref $key eq __PACKAGE__ . "::Hunk") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq "Config::Patch::Hunk") 1 18 19
524 1 $key = $key->key();
525 }
526  
527 19 ▶ $key = $self->{key} unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 14 5 19
528  
529 19 my $new_content = "";
530  
531 21 ▶ $self->data_traverse( sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
532 21 my($patcher, $patch) = @_;
533  
534 21 DEBUG "Remove: '", $patch->text(), "' (",
535 $patch->pos_from(), "-", $patch->pos_to();
536  
537 21 ▶ if($patch->key() eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patch->key eq $key) { } 20 1 21
538 20 ▶ if($patch->mode() eq "replace") {
Branch: 1 branch
BranchTrueFalseTotal
if ($patch->mode eq 'replace') { } 9 11 20
539 # We've got a replace section, extract its
540 # hidden content and re-establish it
541 9 my($hidden, $stripped) = $patch->replstring_extract();
542 9 $new_content .= $hidden;
543 } else {
544 # Replace by nothing
545 }
546 } else {
547 # This isn't our patch
548 1 $new_content .= $patch->header() .
549 $patch->text() .
550 $patch->header();
551 }
552 32 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
553 32 my($patcher, $text) = @_;
554 32 $new_content .= $text;
555 19 });
556  
557 19 $self->{data} = $new_content;
558 }
559  
560 ###########################################
561 46 ▶sub data_traverse {
Subroutine data_traverse: called CC=12 SCAR=24.9
Pod undocumented
562 ###########################################
563 46 my($self, $patch_cb, $text_cb) = @_;
564  
565 46 my $in_patch = 0;
566 46 my $patch_text = "";
567 46 my $text = "";
568 46 my $start_pos;
569 my $end_pos;
570 46 my $pos = 0;
571 46 my $header;
572  
573 46 for my $line (split /\n/, $self->{data}) {
574 304 $_ = "$line\n";
575  
576 304 $pos += length($_);
577 304 ▶ $patch_text .= $_ if $in_patch and $_ !~ $self->{patch_regex};
Branch: 1 branch
BranchTrueFalseTotal
if $in_patch and not $_ =~ /$self->{"patch_regex"}/m 108 196 304
Condition100%3/3
$in_patch and not $_ =~ /$self->{"patch_regex"}/m
!ll&&!rl&&r
16333108
MC/DC100%2/2
$in_patch and not $_ =~ /$self->{"patch_regex"}/m
$in_patch not $_ =~ /$self->{"patch_regex"}/m
Truth table100%3/3
A and not(B)
A= $in_patchB= not $_ =~ /$self->{"patch_regex"}/m
ABresult
0X0
100
111
578  
579 # text line?
580 304 ▶ if($_ !~ $self->{patch_regex} and !$in_patch) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ =~ /$self->{"patch_regex"}/m or $in_patch) 130 174 304
Condition100%3/3
$_ =~ /$self->{"patch_regex"}/m or $in_patch
l!l&&r!l&&!r
66108130
MC/DC100%2/2
$_ =~ /$self->{"patch_regex"}/m or $in_patch
$_ =~ /$self->{"patch_regex"}/m $in_patch
Truth table100%3/3
A or B
A= $_ =~ /$self->{"patch_regex"}/mB= $in_patch
ABresult
1X1
011
000
581 130 $text .= $_;
582 }
583  
584 # closing line of patch
585 304 ▶ if($_ =~ $self->{patch_regex} and
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$self->{"patch_regex"}/m and $in_patch) 33 271 304
Condition100%3/3
$_ =~ /$self->{"patch_regex"}/m and $in_patch
!ll&&!rl&&r
2383333
MC/DC100%2/2
$_ =~ /$self->{"patch_regex"}/m and $in_patch
$_ =~ /$self->{"patch_regex"}/m $in_patch
Truth table100%3/3
A and B
A= $_ =~ /$self->{"patch_regex"}/mB= $in_patch
ABresult
0X0
100
111
586 $in_patch) {
587 33 $end_pos = $pos - 1;
588  
589 my $patch_obj = Config::Patch::Hunk->new(
590 comment_char => $self->{comment_char},
591 key => $1,
592 mode => $2,
593 text => $patch_text,
594 pos_from => $start_pos,
595 pos_to => $end_pos,
596 header => $header,
597 content_pos_from => $start_pos + length($&) + 1,
598 content_pos_to => $end_pos - length($&),
599 33 as_string => substr( $self->{data}, $start_pos,
600 $end_pos - $start_pos + 1 ),
601 );
602  
603 33 $patch_cb->($self, $patch_obj);
604 33 $patch_text = "";
605 }
606  
607 # toggle flag
608 304 ▶ if($_ =~ $self->{patch_regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$self->{"patch_regex"}/m) 66 238 304
609 66 ▶ if($in_patch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_patch) { } 33 33 66
610 # End line
611 } else {
612 # Start Line
613 33 $text_cb->($self, $text);
614 33 $start_pos = $pos - length $_;
615 33 $header = $_;
616 }
617 66 $text = "";
618 66 ▶ $in_patch = ($in_patch xor 1);
Condition50%2/4
$in_patch ^^ 1
l&&rl&&!r!l&&r!l&&!r
330330
MC/DC0%0/2
$in_patch ^^ 1
$in_patch 1
Truth table0%0/4
A ^^ B
A= $in_patchB= 1
ABresult
110
101
011
000
619 }
620 }
621  
622 46 ▶ $text_cb->($self, $text) if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 34 12 46
623  
624 46 return 1;
625 }
626  
627 ###############################################
628 51 ▶sub blurt {
Subroutine blurt: called CC=2 SCAR=7.0
Pod undocumented
629 ###############################################
630 51 my($self, $data, $file) = @_;
631  
632 51 ▶ open FILE, ">$file" or LOGDIE "Cannot open $file ($!)";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$file" or LOGDIE("Cannot open $file ($!)") 51 0 51
633 51 print FILE $data;
634 51 close FILE;
635 }
636  
637 ###############################################
638 46 ▶sub slurp {
Subroutine slurp: called CC=2 SCAR=7.0
Pod undocumented
639 ###############################################
640 46 my($self, $file) = @_;
641  
642 46 local $/ = undef;
643 46 ▶ open FILE, "<$file" or LOGDIE "Cannot open $file ($!)";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or LOGDIE("Cannot open $file ($!)") 46 0 46
644 46 my $data = <FILE>;
645 46 close FILE;
646  
647 46 return $data;
648 }
649  
650 ###########################################
651 33 ▶sub lock {
Subroutine lock: called CC=4 SCAR=25.0
Pod undocumented
652 ###########################################
653 33 my($self) = @_;
654  
655 # Ignore if locking wasn't requested
656 33 ▶ return if ! $self->{flock};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"flock"} 33 0 33
657  
658 # Already locked?
659 0 ▶ if($self->{locked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locked"}) 0 0 0
660 0 $self->{locked}++;
661 0 return 1;
662 }
663  
664 0 ▶ open my $fh, "+<$self->{file}" or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, qq[+<$self->{"file"}] or LOGDIE(qq[Cannot open $self->{"file"} ($!)]) 0 0 0
665 LOGDIE "Cannot open $self->{file} ($!)";
666  
667 0 flock($fh, LOCK_EX);
668  
669 0 $self->{fh} = $fh;
670  
671 0 $self->{locked} = 1;
672 }
673  
674 ###########################################
675 33 ▶sub unlock {
Subroutine unlock: called CC=4 SCAR=25.3
Pod undocumented
676 ###########################################
677 33 my($self) = @_;
678  
679 # Ignore if locking wasn't requested
680 33 ▶ return if ! $self->{flock};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"flock"} 33 0 33
681  
682 0 ▶ if(! $self->{locked}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"locked"}) 0 0 0
683 # Not locked?
684 0 return 1;
685 }
686  
687 0 ▶ if($self->{locked} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locked"} > 1) 0 0 0
688 # Multiple lock released?
689 0 $self->{locked}--;
690 0 return 1;
691 }
692  
693 # Release the last lock
694 0 flock($self->{fh}, LOCK_UN);
695 0 $self->{locked} = undef;
696 0 1;
697 }
698  
699 # LEGACY METHODS
700  
701 ###########################################
702 4 ▶sub patches {
Subroutine patches: called CC=1 SCAR=0.0
Pod documented
703 ###########################################
704 4 my($self) = @_;
705  
706 # LEGACY METHOD, DON'T USE
707  
708 4 my @patches = ();
709 4 my %patches = ();
710  
711 $self->data_traverse(
712 6 ▶ sub { my($patcher, $patch) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
713 6 push @patches,
714 [$patch->key(),
715 $patch->mode(),
716 $patch->text(),
717 $patch->pos_from(),
718 $patch->pos_to(),
719 $patch->header(),
720 $patch->content_pos_from(),
721 $patch->content_pos_to(),
722 ];
723 6 $patches{ $patch->key() }++;
724 },
725 6 ▶ sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
726 4 );
727  
728 4 return \@patches, \%patches;
729 }
730  
731 ###########################################
732 1 ▶sub prepend {
Subroutine prepend: called CC=2 SCAR=7.0
Pod documented
733 ###########################################
734 1 my($self, $text) = @_;
735  
736 # LEGACY METHOD, DON'T USE
737  
738 1 $self->lock();
739 1 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 1 0 1
740  
741 1 $self->patch_by_stretch( $text, "prepend" );
742  
743 1 $self->save();
744 1 $self->unlock();
745 1 return 1;
746 }
747  
748 ###########################################
749 5 ▶sub append {
Subroutine append: called CC=2 SCAR=6.9
Pod documented
750 ###########################################
751 5 my($self, $text) = @_;
752  
753 # LEGACY METHOD, DON'T USE
754  
755 5 $self->lock();
756 5 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 4 1 5
757  
758 5 $self->patch_by_stretch( $text, "append" );
759  
760 5 $self->save();
761 5 $self->unlock();
762 5 return 1;
763 }
764  
765 ###########################################
766 11 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod documented
767 ###########################################
768 11 my($self, $search, $patchtext) = @_;
769  
770 # LEGACY METHOD, DON'T USE
771  
772 11 $self->lock();
773 11 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 9 2 11
774  
775 11 $self->patch_by_wedge($search, $patchtext, "replace");
776  
777 11 $self->save();
778 11 $self->unlock();
779 }
780  
781 ###########################################
782 2 ▶sub insert {
Subroutine insert: called CC=2 SCAR=7.0
Pod undocumented
783 ###########################################
784 2 my($self, $search, $data, $after) = @_;
785  
786 # LEGACY METHOD, DON'T USE
787  
788 2 $self->lock();
789 2 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 2 0 2
790  
791 2 $self->patch_by_wedge($search, $data, "insert", $after);
792  
793 2 $self->save();
794 2 $self->unlock();
795 }
796  
797 ###########################################
798 14 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod undocumented
799 ###########################################
800 14 my($self, $key) = @_;
801  
802 # LEGACY METHOD, DON'T USE
803  
804 14 $self->lock();
805 14 ▶ $self->read() unless $self->{read};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read"} 3 11 14
806  
807 14 $self->eject( $key );
808  
809 14 $self->save();
810 14 $self->unlock();
811 }
812  
813 4 1;
814  
815 __END__
↑ Summary