| 1 | package VCS::Lite::Delta; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | VCS::Lite::Delta - VCS::Lite differences | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use VCS::Lite; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # diff | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my $lit = VCS::Lite->new('/home/me/foo1.txt'); | ||||||||||||||||||||||||||||
| 20 | my $lit2 = VCS::Lite->new('/home/me/foo2.txt'); | ||||||||||||||||||||||||||||
| 21 | my $difftxt = $lit->delta($lit2)->diff; | ||||||||||||||||||||||||||||
| 22 | print OUTFILE $difftxt; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # patch | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my $delt = VCS::Lite::Delta->new('/home/me/patch.diff'); | ||||||||||||||||||||||||||||
| 27 | my $lit3 = $lit->patch($delt); | ||||||||||||||||||||||||||||
| 28 | print OUTFILE $lit3->text; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This module provides a Delta class for the differencing functionality of | ||||||||||||||||||||||||||||
| 33 | VCS::Lite | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | ############################################################################# | ||||||||||||||||||||||||||||
| 40 | #Library Modules # | ||||||||||||||||||||||||||||
| 41 | ############################################################################# | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Error handling, use package vars to control it for now. | ||||||||||||||||||||||||||||
| 48 | 9 | ▶ | use vars qw($error_action $error_msg $error_line); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ############################################################################# | ||||||||||||||||||||||||||||
| 53 | #Interface Methods # | ||||||||||||||||||||||||||||
| 54 | ############################################################################# | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 13 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=53 SCAR=40.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 13 | my $class = shift; | |||||||||||||||||||||||||||
| 58 | 13 | my $src = shift; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # DWIM logic, based on $src parameter. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Case 0: string. Use $id as file name, becomes case 2 | ||||||||||||||||||||||||||||
| 63 | 13 | ▶ | if ( !ref $src ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 6 | ▶ | open my $fh, $src or croak("failed to open '$src': $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 6 | $src = $fh; # becomes case 2 below | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 13 | my $atyp = ref $src; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Case 1: $src is arrayref | ||||||||||||||||||||||||||||
| 70 | 13 | ▶ | return bless { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | id1 => $_[0], | ||||||||||||||||||||||||||||
| 72 | id2 => $_[1], | ||||||||||||||||||||||||||||
| 73 | sep => $_[2], | ||||||||||||||||||||||||||||
| 74 | diff => [@$src] | ||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | $class | ||||||||||||||||||||||||||||
| 77 | if $atyp eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 6 | my $sep = shift; | |||||||||||||||||||||||||||
| 80 | 6 | my %proto; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Decode $sep as needed | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | if (ref($sep) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | %proto = %$sep; | |||||||||||||||||||||||||||
| 86 | 2 | $sep = $proto{in}; | |||||||||||||||||||||||||||
| 87 | 2 | delete $proto{in}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | $sep ||= $/; | ||||||||||||||||||||||||||
|
Condition33%1/3
$sep ||= $/
MC/DC0%0/2
$sep ||= $/
$sep
$/
Truth table33%1/3
A ||= B
A= $sepB= $/
| |||||||||||||||||||||||||||||
| 91 | 6 | ▶ | local $/ = $sep if $sep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 6 | ▶ | $sep ||= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$sep ||= ""
MC/DC0%0/1
$sep ||= ""
$sep
Truth table50%1/2
A
A= $sep
| |||||||||||||||||||||||||||||
| 93 | 6 | my @diff; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Case 2: $src is globref (file handle) - slurp file | ||||||||||||||||||||||||||||
| 96 | 6 | ▶ | if ( $atyp eq 'GLOB' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | 6 | @diff = <$src>; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Case 3: $src is scalar ref (string) | ||||||||||||||||||||||||||||
| 101 | elsif ( $atyp eq 'SCALAR' ) { | ||||||||||||||||||||||||||||
| 102 | 0 | @diff = split /(?=$sep)/, $$src; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Case otherwise is an error. | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 0 | croak "Invalid argument to VCS::Lite::Delta::new"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # If we have reached this point, we have been passed something in a | ||||||||||||||||||||||||||||
| 111 | # text/diff format. It could be diff or udiff format. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | my ( $id1, $id2 ) = @_; | |||||||||||||||||||||||||||
| 114 | 6 | my @out; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 6 | ▶ | if ( $diff[0] =~ /^---/ ) { # udiff format | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 3 | my $state = 'inputdef'; | |||||||||||||||||||||||||||
| 118 | 3 | my ( $a_line, $a_count, @a_hunk, $b_line, $b_count, @b_hunk ); | |||||||||||||||||||||||||||
| 119 | 3 | for my $lin ( 0 .. $#diff ) { | |||||||||||||||||||||||||||
| 120 | 92 | local $_ = $diff[$lin]; | |||||||||||||||||||||||||||
| 121 | 92 | ▶ | chomp if $proto{chomp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | # inputdef = --- and +++ to identify the files being diffed | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 92 | ▶ | if ( $state eq 'inputdef' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 9 | ▶ | $id1 = $1 if /^--- # --- | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | \s | ||||||||||||||||||||||||||||
| 127 | (\S+)/x; # file => $1 | ||||||||||||||||||||||||||||
| 128 | 9 | ▶ | $id2 = $1 if /^\+{3} # +++ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | \s | ||||||||||||||||||||||||||||
| 130 | (\S+)/x; # file => $1 | ||||||||||||||||||||||||||||
| 131 | 9 | ▶ | $state = 'patch' if /^\@\@/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # patch expects @@ -a,b +c,d @@ | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 92 | ▶ | if ( $state eq 'patch' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 11 | ▶ | next unless /^\@\@ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | \s+ | ||||||||||||||||||||||||||||
| 139 | - | ||||||||||||||||||||||||||||
| 140 | (\d+) # line of file 1 => $1 | ||||||||||||||||||||||||||||
| 141 | , | ||||||||||||||||||||||||||||
| 142 | (\d+) # count of file 1 => $2 | ||||||||||||||||||||||||||||
| 143 | \s* | ||||||||||||||||||||||||||||
| 144 | \+ | ||||||||||||||||||||||||||||
| 145 | (\d+) # line of file 2 => $3 | ||||||||||||||||||||||||||||
| 146 | , | ||||||||||||||||||||||||||||
| 147 | (\d+) # count of file 2 => $4 | ||||||||||||||||||||||||||||
| 148 | \s* | ||||||||||||||||||||||||||||
| 149 | \@\@/x; | ||||||||||||||||||||||||||||
| 150 | 10 | $a_line = $1 - 1; | |||||||||||||||||||||||||||
| 151 | 10 | $a_count = $2; | |||||||||||||||||||||||||||
| 152 | 10 | $b_line = $3 - 1; | |||||||||||||||||||||||||||
| 153 | 10 | $b_count = $4; | |||||||||||||||||||||||||||
| 154 | 10 | $state = 'detail'; | |||||||||||||||||||||||||||
| 155 | 10 | next; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # detail expects [-+ ]line of text | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 81 | ▶ | if ( $state eq 'detail' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 75 | my $ind = substr $_, 0, 1, ''; | |||||||||||||||||||||||||||
| 162 | 75 | ▶ | _error( $lin, 'Bad diff' ), return undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | unless $ind =~ /[ +\-i\\]/; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 75 | ▶ | next if $ind eq '\\'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | #[- ]line, add to @a_hunk | ||||||||||||||||||||||||||||
| 168 | 74 | ▶ | if ( $ind ne '+' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 64 | my $lead = '-'; | |||||||||||||||||||||||||||
| 170 | 64 | ▶ | if (($lin < $#diff) && $diff[$lin+1] =~ /^\\/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
MC/DC100%2/2
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
$lin < $#diff
$diff[$lin + 1] =~ /^\\/
Truth table100%3/3
A and B
A= $lin < $#diffB= $diff[$lin + 1] =~ /^\\/
| |||||||||||||||||||||||||||||
| 171 | 1 | $lead .= '/'; | |||||||||||||||||||||||||||
| 172 | 1 | s/$sep$//s; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 64 | push @a_hunk, [ $lead, $a_line++, $_ ]; | |||||||||||||||||||||||||||
| 175 | 64 | $a_count--; | |||||||||||||||||||||||||||
| 176 | 64 | ▶ | _error( $lin, 'Too large diff' ), return undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | if $a_count < 0; | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | #[+ ]line, add to @b_hunk | ||||||||||||||||||||||||||||
| 181 | 74 | ▶ | if ( $ind ne '-' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 56 | my $lead = '+'; | |||||||||||||||||||||||||||
| 183 | 56 | ▶ | if (($lin < $#diff) && $diff[$lin+1] =~ /^\\/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
MC/DC100%2/2
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
$lin < $#diff
$diff[$lin + 1] =~ /^\\/
Truth table100%3/3
A and B
A= $lin < $#diffB= $diff[$lin + 1] =~ /^\\/
| |||||||||||||||||||||||||||||
| 184 | 1 | $lead .= '/'; | |||||||||||||||||||||||||||
| 185 | 1 | s/$sep$//s; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 56 | push @b_hunk, [ $lead, $b_line++, $_ ]; | |||||||||||||||||||||||||||
| 188 | 56 | $b_count--; | |||||||||||||||||||||||||||
| 189 | 56 | ▶ | _error( $lin, 'Too large diff' ), return undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | if $b_count < 0; | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # are we there yet, daddy? | ||||||||||||||||||||||||||||
| 194 | 74 | ▶ | if ( !$a_count and !$b_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$a_count or $b_count
MC/DC100%2/2
$a_count or $b_count
$a_count
$b_count
Truth table100%3/3
A or B
A= $a_countB= $b_count
| |||||||||||||||||||||||||||||
| 195 | 10 | push @out, [ @a_hunk, @b_hunk ]; | |||||||||||||||||||||||||||
| 196 | 10 | @a_hunk = @b_hunk = (); | |||||||||||||||||||||||||||
| 197 | 10 | $state = 'patch'; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } # next line of patch | ||||||||||||||||||||||||||||
| 201 | 3 | return bless { | |||||||||||||||||||||||||||
| 202 | id1 => $id1, | ||||||||||||||||||||||||||||
| 203 | id2 => $id2, | ||||||||||||||||||||||||||||
| 204 | sep => $sep, | ||||||||||||||||||||||||||||
| 205 | diff => \@out, | ||||||||||||||||||||||||||||
| 206 | %proto | ||||||||||||||||||||||||||||
| 207 | }, $class; | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # not a udiff mode patch, assume straight diff mode | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 3 | my $state = 'patch'; | |||||||||||||||||||||||||||
| 213 | 3 | my ( $a_line, $a_count, @a_hunk, $b_line, $b_count, @b_hunk ); | |||||||||||||||||||||||||||
| 214 | 3 | for my $lin ( 0 .. $#diff ) { | |||||||||||||||||||||||||||
| 215 | 46 | local $_ = $diff[$lin]; | |||||||||||||||||||||||||||
| 216 | 46 | ▶ | chomp if $proto{chomp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # patch expects ww,xx[acd]yy,zz style | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 46 | ▶ | if ( $state eq 'patch' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 11 | ▶ | next unless /^(\d+) # start line of file 1 => $1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | (?:,(\d+))? # end line of file 1 => $2 | ||||||||||||||||||||||||||||
| 223 | ([acd]) # Add, change, delete => $3 | ||||||||||||||||||||||||||||
| 224 | (\d+) # start line of file 2 => $4 | ||||||||||||||||||||||||||||
| 225 | (?:,(\d+))? # end line of file 2 => $5 | ||||||||||||||||||||||||||||
| 226 | /x; | ||||||||||||||||||||||||||||
| 227 | 10 | $a_line = $1 - 1; | |||||||||||||||||||||||||||
| 228 | 10 | ▶ | $a_count = $2 ? ( $2 - $a_line ) : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 10 | $b_line = $4 - 1; | |||||||||||||||||||||||||||
| 230 | 10 | ▶ | $b_count = $5 ? ( $5 - $b_line ) : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 10 | ▶ | $a_count = 0 if $3 eq 'a'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 10 | ▶ | $b_count = 0 if $3 eq 'd'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 10 | $state = 'detail'; | |||||||||||||||||||||||||||
| 234 | 10 | next; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # detail expects < lines --- > lines | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 35 | ▶ | if ( $state eq 'detail' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 35 | ▶ | next if /^---/; # ignore separator | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 29 | my $ind = substr $_, 0, 2, ''; | |||||||||||||||||||||||||||
| 242 | 29 | ▶ | _error( $lin, 'Bad diff' ), return undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | unless $ind =~ /[<>\\] /; | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # < line goes to @a_hunk | ||||||||||||||||||||||||||||
| 246 | 29 | ▶ | if ( $ind eq '< ' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 18 | my $lead = '-'; | |||||||||||||||||||||||||||
| 248 | 18 | ▶ | if (($lin < $#diff) && $diff[$lin+1] =~ /^\\/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
MC/DC50%1/2
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
$lin < $#diff
$diff[$lin + 1] =~ /^\\/
Truth table66%2/3
A and B
A= $lin < $#diffB= $diff[$lin + 1] =~ /^\\/
| |||||||||||||||||||||||||||||
| 249 | 1 | $lead .= '/'; | |||||||||||||||||||||||||||
| 250 | 1 | s/$sep$//s; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 18 | push @a_hunk, [ $lead, $a_line++, $_ ]; | |||||||||||||||||||||||||||
| 253 | 18 | $a_count--; | |||||||||||||||||||||||||||
| 254 | 18 | ▶ | _error( $lin, 'Too large diff' ), return undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | if $a_count < 0; | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # > line goes to @b_hunk | ||||||||||||||||||||||||||||
| 259 | 29 | ▶ | if ( $ind eq '> ' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 10 | my $lead = '+'; | |||||||||||||||||||||||||||
| 261 | 10 | ▶ | if (($lin < $#diff) && $diff[$lin+1] =~ /^\\/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
MC/DC100%2/2
$lin < $#diff and $diff[$lin + 1] =~ /^\\/
$lin < $#diff
$diff[$lin + 1] =~ /^\\/
Truth table100%3/3
A and B
A= $lin < $#diffB= $diff[$lin + 1] =~ /^\\/
| |||||||||||||||||||||||||||||
| 262 | 1 | $lead .= '/'; | |||||||||||||||||||||||||||
| 263 | 1 | s/$sep$//s; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 10 | push @b_hunk, [ $lead, $b_line++, $_ ]; | |||||||||||||||||||||||||||
| 266 | 10 | $b_count--; | |||||||||||||||||||||||||||
| 267 | 10 | ▶ | _error( $lin, 'Too large diff' ), return undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | if $b_count < 0; | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # are we there yet, daddy? | ||||||||||||||||||||||||||||
| 272 | 29 | ▶ | if ( !$a_count and !$b_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$a_count or $b_count
MC/DC100%2/2
$a_count or $b_count
$a_count
$b_count
Truth table100%3/3
A or B
A= $a_countB= $b_count
| |||||||||||||||||||||||||||||
| 273 | 10 | push @out, [ @a_hunk, @b_hunk ]; | |||||||||||||||||||||||||||
| 274 | 10 | @a_hunk = @b_hunk = (); | |||||||||||||||||||||||||||
| 275 | 10 | $state = 'patch'; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | 3 | return bless { | |||||||||||||||||||||||||||
| 280 | id1 => $id1, | ||||||||||||||||||||||||||||
| 281 | id2 => $id2, | ||||||||||||||||||||||||||||
| 282 | sep => $sep, | ||||||||||||||||||||||||||||
| 283 | diff => \@out, | ||||||||||||||||||||||||||||
| 284 | %proto | ||||||||||||||||||||||||||||
| 285 | }, $class; | ||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 0 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 289 | 0 | ( $error_line, my $msg ) = @_; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | $error_msg = "Line $error_line: $msg"; | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | ▶ | goto &$error_action if ref($error_action) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 0 | ▶ | confess $error_msg if $error_action eq 'raise'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | print STDERR $error_msg, "\n" unless $error_action eq 'silent'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 17 | ▶ | sub _diff_hunk { | ||||||||||||||||||||||||||
|
Subroutine
_diff_hunk: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 17 | my $sep = shift; | |||||||||||||||||||||||||||
| 302 | 17 | my $r_line_offset = shift; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 17 | my @ins; | |||||||||||||||||||||||||||
| 305 | 17 | my ( $ins_firstline, $ins_lastline ) = ( 0, 0 ); | |||||||||||||||||||||||||||
| 306 | 17 | my @del; | |||||||||||||||||||||||||||
| 307 | 17 | my ( $del_firstline, $del_lastline ) = ( 0, 0 ); | |||||||||||||||||||||||||||
| 308 | 17 | my $op; | |||||||||||||||||||||||||||
| 309 | 17 | my $shortins = ''; | |||||||||||||||||||||||||||
| 310 | 17 | my $shortdel = ''; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | # construct @ins and @del from hunk | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 17 | for (@_) { | |||||||||||||||||||||||||||
| 315 | 47 | my ( $typ, $lno, $txt ) = @$_; | |||||||||||||||||||||||||||
| 316 | 47 | my $short = substr($typ, 1, 1, ''); | |||||||||||||||||||||||||||
| 317 | 47 | $lno++; | |||||||||||||||||||||||||||
| 318 | 47 | ▶ | if ( $typ eq '+' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 21 | push @ins, $txt; | |||||||||||||||||||||||||||
| 320 | 21 | ▶ | $ins_firstline ||= $lno; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ins_firstline ||= $lno
MC/DC0%0/2
$ins_firstline ||= $lno
$ins_firstline
$lno
Truth table66%2/3
A ||= B
A= $ins_firstlineB= $lno
| |||||||||||||||||||||||||||||
| 321 | 21 | $ins_lastline = $lno; | |||||||||||||||||||||||||||
| 322 | 21 | ▶ | $shortins = "\n\\ No newline at end of file\n" if $short; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | else { | ||||||||||||||||||||||||||||
| 325 | 26 | push @del, $txt; | |||||||||||||||||||||||||||
| 326 | 26 | ▶ | $del_firstline ||= $lno; | ||||||||||||||||||||||||||
|
Condition66%2/3
$del_firstline ||= $lno
MC/DC0%0/2
$del_firstline ||= $lno
$del_firstline
$lno
Truth table66%2/3
A ||= B
A= $del_firstlineB= $lno
| |||||||||||||||||||||||||||||
| 327 | 26 | $del_lastline = $lno; | |||||||||||||||||||||||||||
| 328 | 26 | ▶ | $shortdel = "\n\\ No newline at end of file\n" if $short; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | # Work out whether we are a, c or d | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 17 | ▶ | if ( !@del ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 335 | 3 | $op = 'a'; | |||||||||||||||||||||||||||
| 336 | 3 | $del_firstline = $ins_firstline - $$r_line_offset - 1; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | elsif ( !@ins ) { | ||||||||||||||||||||||||||||
| 339 | 2 | $op = 'd'; | |||||||||||||||||||||||||||
| 340 | 2 | $ins_firstline = $del_firstline + $$r_line_offset - 1; | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | else { | ||||||||||||||||||||||||||||
| 343 | 12 | $op = 'c'; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 17 | $$r_line_offset += @ins - @del; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 17 | ▶ | $ins_lastline ||= $ins_firstline; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ins_lastline ||= $ins_firstline
MC/DC0%0/2
$ins_lastline ||= $ins_firstline
$ins_lastline
$ins_firstline
Truth table66%2/3
A ||= B
A= $ins_lastlineB= $ins_firstline
| |||||||||||||||||||||||||||||
| 349 | 17 | ▶ | $del_lastline ||= $del_firstline; | ||||||||||||||||||||||||||
|
Condition100%3/3
$del_lastline ||= $del_firstline
MC/DC100%2/2
$del_lastline ||= $del_firstline
$del_lastline
$del_firstline
Truth table100%3/3
A ||= B
A= $del_lastlineB= $del_firstline
| |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # Make the header line | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 17 | my $outstr = | |||||||||||||||||||||||||||
| 354 | "$del_firstline,$del_lastline$op$ins_firstline,$ins_lastline\n"; | ||||||||||||||||||||||||||||
| 355 | 17 | $outstr =~ s/(^|\D)(\d+),\2(?=\D|$)/$1$2/g; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # < deletions | ||||||||||||||||||||||||||||
| 358 | 17 | for (@del) { | |||||||||||||||||||||||||||
| 359 | 26 | $outstr .= '< ' . $_ . $sep; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | 17 | $outstr .= $shortdel; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # --- | ||||||||||||||||||||||||||||
| 364 | 17 | ▶ | $outstr .= "---\n" if @ins && @del; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@ins and @del
MC/DC100%2/2
@ins and @del
@ins
@del
Truth table100%3/3
A and B
A= @insB= @del
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | # > insertions | ||||||||||||||||||||||||||||
| 367 | 17 | for (@ins) { | |||||||||||||||||||||||||||
| 368 | 21 | $outstr .= '> ' . $_ . $sep; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | 17 | $outstr .= $shortins; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 17 | $outstr; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 4 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 376 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 377 | 4 | ▶ | my $sep = shift || $self->{sep} || ''; | ||||||||||||||||||||||||||
|
Condition80%4/5
shift() || $self->{'sep'} || ''
shift() or $self->{"sep"}
MC/DC50%1/2
shift() || $self->{'sep'} || ''
shift()
$self->{"sep"}
Truth table66%2/3
A or B
A= shift()B= $self->{"sep"}
| |||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 4 | my $off = 0; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 4 | join '', map { _diff_hunk( $sep, \$off, @$_ ) } @{ $self->{diff} }; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 6 | ▶ | sub udiff { | ||||||||||||||||||||||||||
|
Subroutine
udiff: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 386 | 6 | ▶ | my $sep = shift || $self->{sep} || ''; | ||||||||||||||||||||||||||
|
Condition80%4/5
shift() || $self->{'sep'} || ''
shift() or $self->{"sep"}
MC/DC50%1/2
shift() || $self->{'sep'} || ''
shift()
$self->{"sep"}
Truth table66%2/3
A or B
A= shift()B= $self->{"sep"}
| |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 6 | my ( $in, $out, $diff ) = @{$self}{qw/id1 id2 diff/}; | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | # Header with file names | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 6 | my @out = ( "--- $in \n", "+++ $out \n" ); | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 6 | my $offset = 0; | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 6 | for (@$diff) { | |||||||||||||||||||||||||||
| 397 | 22 | my @t1 = grep { $_->[0] =~ /^\-/ } @$_; | |||||||||||||||||||||||||||
| 398 | 22 | my @t2 = grep { $_->[0] =~ /^\+/ } @$_; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 22 | my $short1 = ''; | |||||||||||||||||||||||||||
| 401 | $short1 = "\n\\ No newline at end of file\n" | ||||||||||||||||||||||||||||
| 402 | 22 | ▶ | if grep { $_->[0] eq '-/' } @t1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | 22 | my $short2 = ''; | |||||||||||||||||||||||||||
| 404 | $short2 = "\n\\ No newline at end of file\n" | ||||||||||||||||||||||||||||
| 405 | 22 | ▶ | if grep { $_->[0] eq '+/' } @t2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # Work out base line numbers in both files | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 22 | ▶ | my $base1 = @t1 ? $t1[0][1] : $t2[0][1] - $offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | 22 | ▶ | my $base2 = @t2 ? $t2[0][1] : $t1[0][1] + $offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | 22 | $base1++; | |||||||||||||||||||||||||||
| 412 | 22 | $base2++; # Our lines were 0 based | |||||||||||||||||||||||||||
| 413 | 22 | $offset += @t2 - @t1; | |||||||||||||||||||||||||||
| 414 | 22 | my $count1 = @t1; | |||||||||||||||||||||||||||
| 415 | 22 | my $count2 = @t2; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | # Header line | ||||||||||||||||||||||||||||
| 418 | 22 | push @out, "@@ -$base1,$count1 +$base2,$count2 @@\n"; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | # Use Algorithm::Diff::sdiff to munge out any lines in common inside | ||||||||||||||||||||||||||||
| 421 | # the hunk | ||||||||||||||||||||||||||||
| 422 | 22 | my @txt1 = map { $_->[2] } @t1; | |||||||||||||||||||||||||||
| 423 | 22 | my @txt2 = map { $_->[2] } @t2; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 22 | my @ad = Algorithm::Diff::sdiff( \@txt1, \@txt2 ); | |||||||||||||||||||||||||||
| 426 | 22 | my @defer; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | # for each subhunk, we want all the file1 lines first, then all the file2 lines | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 22 | for (@ad) { | |||||||||||||||||||||||||||
| 431 | 139 | my ( $ind, $txt1, $txt2 ) = @$_; | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | # we want to flush out the + lines when we run off the end of a 'c' section | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 139 | ▶ | ( push @out, @defer ), @defer = () unless $ind eq 'c'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | # unchanged lines, just wack 'em out | ||||||||||||||||||||||||||||
| 438 | 139 | ▶ | ( push @out, ' ' . $txt1 . $sep ), next if $ind eq 'u'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | # output original line (- line) | ||||||||||||||||||||||||||||
| 441 | 47 | ▶ | push @out, '-' . $txt1 . $sep unless $ind eq '+'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | # defer changed + lines | ||||||||||||||||||||||||||||
| 444 | 47 | ▶ | push @defer, '+' . $txt2 . $sep unless $ind eq '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | 22 | push @out, $short1; | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | # and flush at the end | ||||||||||||||||||||||||||||
| 449 | 22 | push @out, @defer, $short2; | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | 6 | ▶ | wantarray ? @out : join '', @out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 1 | ▶ | sub id { | ||||||||||||||||||||||||||
|
Subroutine
id: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||
| 455 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 1 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | 0 | $self->{id1} = shift; | |||||||||||||||||||||||||||
| 459 | 0 | $self->{id2} = shift; | |||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 1 | @{$self}{qw/id1 id2/}; | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 7 | ▶ | sub hunks { | ||||||||||||||||||||||||||
|
Subroutine
hunks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 466 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 7 | @{ $self->{diff} }; | |||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 9 | 1; | |||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | __END__ | ||||||||||||||||||||||||||||