blib/lib/Text/Patch.pm

filter: Statementstmt 12.0% 18 / 149 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 33.3% 5 / 15 Podpod 16.6% 1 / 6 totaltotal 9.0% 24 / 266 CC 49 SCAR 45.2
CC
49
Cov
8%
patch_context
210.0
patch_oldstyle
110.0
patch
90.0
CRAP 92.3
↑ Summary
1 package Text::Patch;
2 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our @ISA = qw( Exporter );
4 1 our @EXPORT = qw( patch );
5 1 our $VERSION = '1.8';
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use constant NO_NEWLINE => '\\ No newline at end of file';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub patch
13 0 ▶{
Subroutine patch: not called CC=9 SCAR=45.0
Pod documented
14 0 my $text = shift;
15 0 my $diff = shift;
16 0 ▶ my %options = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
17  
18 0 my %handler = ('unified' => \&patch_unified,
19 'context' => \&patch_context,
20 'oldstyle' => \&patch_oldstyle,
21 );
22 0 my $style = $options{STYLE};
23 0 ▶ croak "required STYLE option is missing" unless $style;
Branch: 1 branch
BranchTrueFalseTotal
unless $style 0 0 0
24 0 ▶ croak "source required" unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
25 0 ▶ croak "diff required" unless defined $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff 0 0 0
26 0 ▶ my $code = $handler{lc($style)} || croak "unrecognised STYLE '$style'";
Condition0%0/2
$handler{lc $style} || croak("unrecognised STYLE '${style}'")
l!l
00
MC/DC0%0/1
$handler{lc $style} || croak("unrecognised STYLE '${style}'")
$handler{lc $style}
Truth table0%0/2
A
A= $handler{lc $style}
Aresult
11
00
27  
28 0 my @text = split /^/m, $text;
29 0 my @diff = split /^/m, $diff;
30  
31 # analyse source/diff to determine line ending used.
32 # (if source is only 1 line, can't use it to determine line endings)
33 0 ▶ my $line1 = @text > 1 ? $text[0] : $diff[0];
Branch: 1 branch
BranchTrueFalseTotal
@text > 1 ? : 0 0 0
34 0 my($line1c, $sep) = _chomp($line1);
35 0 ▶ $sep ||= "\n"; # default to unix line ending
Condition0%0/2
$sep ||= "\n"
l!l
00
MC/DC0%0/1
$sep ||= "\n"
$sep
Truth table0%0/2
A
A= $sep
Aresult
11
00
36  
37 # apply patch
38 0 DUMP("got patch", \@diff);
39 0 my $out = $code->(\@text, \@diff, $sep);
40  
41 0 my $lastline = _chomp($diff[-1], $sep);
42 0 ▶ $out = _chomp($out, $sep) if $lastline eq NO_NEWLINE;
Branch: 1 branch
BranchTrueFalseTotal
if $lastline eq "\\ No newline at end of file" 0 0 0
43 0 return $out;
44 }
45  
46 sub patch_unified
47 0 ▶{
Subroutine patch_unified: not called CC=6 SCAR=37.4
Pod undocumented
48 0 my($text, $diff, $sep) = @_;
49 0 my @hunks;
50 my %hunk;
51  
52 0 for( @$diff )
53 {
54 #print STDERR ">>> ... [$_]";
55 0 ▶ if( /^\@\@\s*-([\d,]+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^\@\@\s*-([\d,]+)/) 0 0 0
56 {
57 #print STDERR ">>> *** HUNK!\n";
58 0 my($pos1, $count1) = split /,/, $1;
59 0 push @hunks, { %hunk };
60 0 %hunk = ();
61 0 $hunk{ FROM } = $pos1 - 1; # diff is 1-based
62 # Modification by Ben L., patches may have @@ -0,0 if the source is empty.
63 0 ▶ $hunk{ FROM } = 0 if $hunk{ FROM } < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hunk{"FROM"} < 0 0 0 0
64 0 ▶ $hunk{ LEN } = defined $count1 ? $count1 : $pos1 == 0 ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $count1 ? : 0 0 0
$pos1 == 0 ? : 0 0 0
65 0 $hunk{ DATA } = [];
66 }
67 0 push @{ $hunk{ DATA } }, $_;
68 }
69 0 push @hunks, { %hunk }; # push last hunk
70 0 shift @hunks; # first is always empty
71  
72 0 return _patch($text, \@hunks, $sep);
73 }
74  
75 0 ▶sub patch_oldstyle {
Subroutine patch_oldstyle: not called CC=10 SCAR=47.0
Pod undocumented
76 0 my($text, $diff, $sep) = @_;
77 0 my @hunks;
78 0 my $i = 0;
79  
80 0 my $hunk_head = qr/^([\d,]+)([acd])([\d,]+)$/;
81 0 ▶ while($i < @$diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$diff) 0 0 0
82 0 my $l = $diff->[$i];
83 0 my($r1, $type, $r2) = $l =~ $hunk_head;
84 0 ▶ die "Malformed patch at line ".($i + 1)."\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r1 and $type and defined $r2 0 0 0
Condition0%0/6
defined $r1 and $type and defined $r2
!ll&&!rl&&r
000
defined $r1 and $type
!ll&&!rl&&r
000
MC/DC0%0/3
defined $r1 and $type and defined $r2
defined $r1 $type defined $r2
Truth table0%0/4
A and B and C
A= defined $r1B= $typeC= defined $r2
ABCresult
0XX0
10X0
1100
1111
85 unless defined $r1 && $type && defined $r2;
86 0 my($pos1, $count1) = _range($r1);
87 0 my($pos2, $count2) = _range($r2);
88  
89 # parse chunk data
90 0 my @data;
91 0 my $j = $i + 1;
92 0 ▶ for(; $j < @$diff; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$diff) 0 0 0
93 0 $l = $diff->[$j];
94 0 ▶ last if $l =~ $hunk_head;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /$hunk_head/ 0 0 0
95 0 ▶ next if $l =~ /^---/; # separator
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^---/ 0 0 0
96 0 push @data, $l;
97 }
98 0 my $datalen = $j - $i - 1;
99  
100 0 ▶ if($type eq 'a') { # add
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "a") 0 0 0
101 0 $count1 = 0; # don't remove any lines
102 0 $pos1++; # add to line after pos1
103 }
104  
105 # convert data to a format _patch() will understand
106 0 for(@data) {
107 0 $_ =~ s/^< /-/;
108 0 $_ =~ s/^> /+/;
109 }
110  
111 0 push @hunks, { FROM => $pos1 - 1,
112 LEN => $count1,
113 DATA => \@data,
114 };
115 0 $i += $datalen + 1;
116 }
117 0 return _patch($text, \@hunks, $sep);
118 }
119  
120 # NB: this works by converting hunks into a kind of unified format
121 0 ▶sub patch_context {
Subroutine patch_context: not called CC=14 SCAR=53.5
Pod undocumented
122 0 my($text, $diff, $sep) = @_;
123 0 my $i = 0;
124 0 my @hunks;
125  
126 # skip past header
127 0 for(@$diff) {
128 0 $i++;
129 0 ▶ last if /^\Q***************\E$/; # end header marker
Branch: 1 branch
BranchTrueFalseTotal
if /^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$/ 0 0 0
130 }
131  
132 # this sub reads one half of a hunk (from/to part)
133 0 ▶ my $read_part = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
134 0 my $l = $diff->[$i++];
135 0 TRACE("got line: $l");
136 0 ▶ die "Malformed patch at line $i\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $l =~ /^(?:\*\*\*|---)\s+([\d,]+)\s+(?:\*\*\*|---)/ 0 0 0
137 unless $l =~ /^(?:\*\*\*|---)\s+([\d,]+)\s+(?:\*\*\*|---)/;
138 0 my($pos, $count) = _range($1);
139 0 my @part;
140 0 ▶ while($i < @$diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$diff) 0 0 0
141 0 my $l = $diff->[$i];
142 0 ▶ last if $l =~ /^(\*\*\*|---)/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^(\*\*\*|---)/ 0 0 0
143 0 push @part, $l;
144 0 $i++;
145 }
146 0 DUMP("got part", \@part);
147 0 return (\@part, $pos, $count);
148 0 };
149  
150 0 ▶ while($i < @$diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$diff) 0 0 0
151 # read the from and to part of this hunk
152 0 my($part1, $pos1, $count1) = $read_part->();
153 0 my($part2, $pos2, $count2) = $read_part->();
154 0 $i++; # skip chunk separator
155  
156 # convert operations to unified style ones
157 0 $_ =~ s/^(.)\s/$1/ for @$part1, @$part2;
158 0 $_ =~ s/^\!/-/ for @$part1; # remove
159 0 $_ =~ s/^\!/+/ for @$part2; # add
160  
161 # merge the parts to create a unified style chunk
162 0 my @data;
163 0 for(;;) {
164 0 my $c1 = $part1->[0];
165 0 my $c2 = $part2->[0];
166 0 ▶ last unless defined $c1 || defined $c2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c1 or defined $c2 0 0 0
Condition0%0/3
defined $c1 or defined $c2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $c1 or defined $c2
defined $c1 defined $c2
Truth table0%0/3
A or B
A= defined $c1B= defined $c2
ABresult
1X1
011
000
167  
168 0 ▶ if(defined $c1 && $c1 =~ /^-/) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $c1 and $c1 =~ /^-/) { } 0 0 0
elsif (defined $c2 and $c2 =~ /^\+/) { } 0 0 0
Condition0%0/3
defined $c1 and $c1 =~ /^-/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c1 and $c1 =~ /^-/
defined $c1 $c1 =~ /^-/
Truth table0%0/3
A and B
A= defined $c1B= $c1 =~ /^-/
ABresult
0X0
100
111
169 0 push @data, shift @$part1; # remove line
170 ▶ } elsif(defined $c2 && $c2 =~ /^\+/) {
Condition0%0/3
defined $c2 and $c2 =~ /^\+/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c2 and $c2 =~ /^\+/
defined $c2 $c2 =~ /^\+/
Truth table0%0/3
A and B
A= defined $c2B= $c2 =~ /^\+/
ABresult
0X0
100
111
171 0 push @data, shift @$part2; # add line
172 } else { # context
173 0 my($x1, $x2) = (shift @$part1, shift @$part2);
174 0 ▶ push @data, defined $x1 ? $x1 : $x2;
Branch: 1 branch
BranchTrueFalseTotal
defined $x1 ? : 0 0 0
175 }
176 }
177 0 push @hunks, { FROM => $pos1 - 1,
178 LEN => $count1,
179 DATA => \@data,
180 };
181 0 DUMP("merged data", \@data);
182 }
183 0 return _patch($text, \@hunks, $sep);
184 }
185  
186 ######################################################################
187 # private
188  
189 # returns (start line, line count)
190 0 ▶sub _range {
Subroutine _range: not called CC=2 SCAR=17.9
191 0 my($range) = @_;
192 0 my($pos1, $pos2) = split /,/, $range;
193 0 ▶ return ($pos1, defined $pos2 ? $pos2 - $pos1 + 1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $pos2 ? : 0 0 0
194 }
195  
196 0 ▶sub _patch {
Subroutine _patch: not called CC=8 SCAR=42.8
197 0 my($text, $hunks, $sep) = @_;
198 0 my $hunknum = scalar @$hunks + 1;
199 0 ▶ die "No hunks found\n" unless @$hunks;
Branch: 1 branch
BranchTrueFalseTotal
unless @$hunks 0 0 0
200 0 for my $hunk ( reverse @$hunks )
201 {
202 0 $hunknum--;
203 0 DUMP("hunk", $hunk);
204 0 my @pdata;
205 0 my $num = $hunk->{FROM};
206 0 for( @{ $hunk->{ DATA } } )
207 {
208 0 ▶ next unless s/^([ \-\+])//;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^([ \-\+])// 0 0 0
209 #print STDERR ">>> ($1) $_";
210 0 ▶ if($1 ne '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne "+") 0 0 0
211 # not an addition, check line for match against existing text.
212 # ignore line endings for comparison
213 0 my $orig = _chomp($text->[$num++], $sep); # num 0 based here
214 0 my $expect = _chomp($_, $sep);
215 0 TRACE("checking >>$orig<<");
216 0 TRACE(" against >>$expect<<");
217 0 ▶ die "Hunk #$hunknum failed at line $num.\n" # actual line number
Branch: 1 branch
BranchTrueFalseTotal
unless $orig eq $expect 0 0 0
218 unless $orig eq $expect;
219 }
220 0 ▶ next if $1 eq '-'; # removals
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "-" 0 0 0
221 0 push @pdata, $_; # add/replace line
222 }
223 0 splice @$text, $hunk->{ FROM }, $hunk->{ LEN }, @pdata;
224 }
225  
226 0 return join '', @$text;
227 }
228  
229 # chomp $sep from the end of line
230 # if $sep is not given, chomp unix or dos line ending
231 0 ▶sub _chomp {
Subroutine _chomp: not called CC=3 SCAR=24.8
232 0 my($text, $sep) = @_;
233 0 ▶ if($sep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep) { } 0 0 0
234 0 $text =~ s/($sep)$//;
235 } else {
236 0 $text =~ s/(\r\n|\n)$//;
237 }
238 0 ▶ return wantarray ? ($text, $1) : $text;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
239 }
240  
241 0 ▶sub DUMP {}
Subroutine DUMP: not called CC=1 SCAR=0.0
Pod undocumented
242 0 ▶sub TRACE {}
Subroutine TRACE: not called CC=1 SCAR=0.0
Pod undocumented
243  
244 #sub DUMP {
245 #use Data::Dumper;
246 #print STDERR Dumper(@_);
247 #}
248 #sub TRACE {
249 #use Data::Dumper;
250 #print STDERR Dumper(@_);
251 #}
252  
253  
254 =pod
255  
256 =head1 NAME
257  
258 Text::Patch - Patches text with given patch
259  
260 =head1 SYNOPSIS
261  
262 use Text::Patch;
263  
264 $output = patch( $source, $diff, STYLE => "Unified" );
265  
266 use Text::Diff;
267  
268 $src = ...
269 $dst = ...
270  
271 $diff = diff( \$src, \$dst, { STYLE => 'Unified' } );
272  
273 $out = patch( $src, $diff, { STYLE => 'Unified' } );
274  
275 print "Patch successful" if $out eq $dst;
276  
277 =head1 DESCRIPTION
278  
279 Text::Patch combines source text with given diff (difference) data.
280 Diff data is produced by Text::Diff module or by the standard diff
281 utility (man diff, see -u option).
282  
283 =over 4
284  
285 =item patch( $source, $diff, options... )
286  
287 First argument is source (original) text. Second is the diff data.
288 Third argument can be either hash reference with options or all the
289 rest arguments will be considered patch options:
290  
291 $output = patch( $source, $diff, STYLE => "Unified", ... );
292  
293 $output = patch( $source, $diff, { STYLE => "Unified", ... } );
294  
295 Options are:
296  
297 STYLE => 'Unified'
298  
299 STYLE can be "Unified", "Context" or "OldStyle".
300  
301 The 'Unified' diff format looks like this:
302  
303 @@ -1,7 +1,6 @@
304 -The Way that can be told of is not the eternal Way;
305 -The name that can be named is not the eternal name.
306 The Nameless is the origin of Heaven and Earth;
307 -The Named is the mother of all things.
308 +The named is the mother of all things.
309 +
310 Therefore let there always be non-being,
311 so we may see their subtlety,
312 And let there always be being,
313 @@ -9,3 +8,6 @@
314 The two are the same,
315 But after they are produced,
316 they have different names.
317 +They both may be called deep and profound.
318 +Deeper and more profound,
319 +The door of all subtleties!
320  
321  
322 =back
323  
324 =head1 TODO
325  
326 Interfaces with files, arrays, etc.
327  
328 =head1 AUTHOR
329  
330 Vladi Belperchinov-Shabanski "Cade"
331  
332 <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org>
333  
334 http://cade.datamax.bg
335  
336 =head1 VERSION
337  
338 $Id: Patch.pm,v 1.6 2007/04/07 19:57:41 cade Exp $
339  
340 =cut
↑ Summary