3 # `Diff' program in Perl
4 # Copyright 1998 M-J. Dominus. (mjd-perl-diff@plover.com)
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
9 # Altered to output in `context diff' format (but without context)
10 # September 1998 Christian Murphy (cpm@muc.de)
12 # Command-line arguments and context lines feature added
13 # September 1998 Amir D. Karger (karger@bead.aecom.yu.edu)
15 # In this file, "item" usually means "line of text", and "item number" usually
16 # means "line number". But theoretically the code could be used more generally
19 use Algorithm::Diff qw(diff);
21 use vars qw ($opt_C $opt_c $opt_u $opt_U);
24 my $usage = << "ENDUSAGE";
25 Usage: $0 [{-c | -u}] [{-C | -U} lines] oldfile newfile
26 -c will do a context diff with 3 lines of context
27 -C will do a context diff with 'lines' lines of context
28 -u will do a unified diff with 3 lines of context
29 -U will do a unified diff with 'lines' lines of context
32 getopts('U:C:cu') or bag("$usage");
33 bag("$usage") unless @ARGV == 2;
34 my ($file1, $file2) = @ARGV;
35 if (defined $opt_C || defined $opt_c) {
36 $opt_c = ""; # -c on if -C given on command line
38 } elsif (defined $opt_U || defined $opt_u) {
39 $opt_u = ""; # -u on if -U given on command line
42 $opt_c = ""; # by default, do context diff, not old diff
45 my ($char1, $char2); # string to print before file names
46 my $Context_Lines; # lines of context to print
48 $Context_Lines = defined $opt_C ? $opt_C : 3;
49 $char1 = '*' x 3; $char2 = '-' x 3;
50 } elsif (defined $opt_u) {
51 $Context_Lines = defined $opt_U ? $opt_U : 3;
52 $char1 = '-' x 3; $char2 = '+' x 3;
55 # After we've read up to a certain point in each file, the number of items
56 # we've read from each file will differ by $FLD (could be 0)
57 my $File_Length_Difference = 0;
59 open (F1, $file1) or bag("Couldn't open $file1: $!");
60 open (F2, $file2) or bag("Couldn't open $file2: $!");
67 # diff yields lots of pieces, each of which is basically a Block object
68 my $diffs = diff(\@f1, \@f2);
69 exit 0 unless @$diffs;
71 my $st = stat($file1);
72 print "$char1 $file1\t", scalar localtime($st->mtime), "\n";
74 print "$char2 $file2\t", scalar localtime($st->mtime), "\n";
77 # Loop over hunks. If a hunk overlaps with the last hunk, join them.
78 # Otherwise, print out the old one.
79 foreach my $piece (@$diffs) {
80 $hunk = new Hunk ($piece, $Context_Lines);
83 if ($hunk->does_overlap($oldhunk)) {
84 $hunk->prepend_hunk($oldhunk);
86 $oldhunk->output_diff(\@f1, \@f2);
94 $oldhunk->output_diff(\@f1, \@f2);
105 # Package Hunk. A Hunk is a group of Blocks which overlap because of the
106 # context surrounding each block. (So if we're not using context, every
107 # hunk will contain one block.)
112 # Arg1 is output from &LCS::diff (which corresponds to one Block)
113 # Arg2 is the number of items (lines, e.g.,) of context around each block
115 # This subroutine changes $File_Length_Difference
118 # blocks - a list of Block objects
119 # start - index in file 1 where first block of the hunk starts
120 # end - index in file 1 where last block of the hunk ends
123 # before_diff - how much longer file 2 is than file 1 due to all hunks
124 # until but NOT including this one
125 # after_diff - difference due to all hunks including this one
126 my ($class, $piece, $context_items) = @_;
128 my $block = new Block ($piece); # this modifies $FLD!
130 my $before_diff = $File_Length_Difference; # BEFORE this hunk
131 my $after_diff = $before_diff + $block->{"length_diff"};
132 $File_Length_Difference += $block->{"length_diff"};
134 # @remove_array and @insert_array hold the items to insert and remove
135 # Save the start & beginning of each array. If the array doesn't exist
136 # though (e.g., we're only adding items in this block), then figure
137 # out the line number based on the line number of the other file and
138 # the current difference in file lenghts
139 my @remove_array = $block->remove;
140 my @insert_array = $block->insert;
141 my ($a1, $a2, $b1, $b2, $start1, $start2, $end1, $end2);
142 $a1 = @remove_array ? $remove_array[0 ]->{"item_no"} : -1;
143 $a2 = @remove_array ? $remove_array[-1]->{"item_no"} : -1;
144 $b1 = @insert_array ? $insert_array[0 ]->{"item_no"} : -1;
145 $b2 = @insert_array ? $insert_array[-1]->{"item_no"} : -1;
147 $start1 = $a1 == -1 ? $b1 - $before_diff : $a1;
148 $end1 = $a2 == -1 ? $b2 - $after_diff : $a2;
149 $start2 = $b1 == -1 ? $a1 + $before_diff : $b1;
150 $end2 = $b2 == -1 ? $a2 + $after_diff : $b2;
152 # At first, a hunk will have just one Block in it
158 "blocks" => [$block],
162 $hunk->flag_context($context_items);
167 # Change the "start" and "end" fields to note that context should be added
170 my ($hunk, $context_items) = @_;
171 return unless $context_items; # no context
174 my $start1 = $hunk->{"start1"};
175 my $num_added = $context_items > $start1 ? $start1 : $context_items;
176 $hunk->{"start1"} -= $num_added;
177 $hunk->{"start2"} -= $num_added;
180 my $end1 = $hunk->{"end1"};
181 $num_added = ($end1+$context_items > $#f1) ?
184 $hunk->{"end1"} += $num_added;
185 $hunk->{"end2"} += $num_added;
188 # Is there an overlap between hunk arg0 and old hunk arg1?
189 # Note: if end of old hunk is one less than beginning of second, they overlap
191 my ($hunk, $oldhunk) = @_;
192 return "" unless $oldhunk; # first time through, $oldhunk is empty
194 # Do I actually need to test both?
195 return ($hunk->{"start1"} - $oldhunk->{"end1"} <= 1 ||
196 $hunk->{"start2"} - $oldhunk->{"end2"} <= 1);
199 # Prepend hunk arg1 to hunk arg0
200 # Note that arg1 isn't updated! Only arg0 is.
202 my ($hunk, $oldhunk) = @_;
204 $hunk->{"start1"} = $oldhunk->{"start1"};
205 $hunk->{"start2"} = $oldhunk->{"start2"};
207 unshift (@{$hunk->{"blocks"}}, @{$oldhunk->{"blocks"}});
211 # DIFF OUTPUT ROUTINES. THESE ROUTINES CONTAIN DIFF FORMATTING INFO...
213 if (defined $main::opt_u) {&output_unified_diff(@_)}
214 elsif (defined $main::opt_c) {&output_context_diff(@_)}
215 else {die "unknown diff"}
218 sub output_unified_diff {
219 my ($hunk, $fileref1, $fileref2) = @_;
222 # Calculate item number range.
223 my $range1 = $hunk->unified_range(1);
224 my $range2 = $hunk->unified_range(2);
225 print "@@ -$range1 +$range2 @@\n";
227 # Outlist starts containing the hunk of file 1.
228 # Removing an item just means putting a '-' in front of it.
229 # Inserting an item requires getting it from file2 and splicing it in.
230 # We splice in $num_added items. Remove blocks use $num_added because
231 # splicing changed the length of outlist.
232 # We remove $num_removed items. Insert blocks use $num_removed because
233 # their item numbers---corresponding to positions in file *2*--- don't take
234 # removed items into account.
235 my $low = $hunk->{"start1"};
236 my $hi = $hunk->{"end1"};
237 my ($num_added, $num_removed) = (0,0);
238 my @outlist = @$fileref1[$low..$hi];
239 map {s/^/ /} @outlist; # assume it's just context
241 foreach my $block (@{$hunk->{"blocks"}}) {
242 foreach my $item ($block->remove) {
243 my $op = $item->{"sign"}; # -
244 my $offset = $item->{"item_no"} - $low + $num_added;
245 $outlist[$offset] =~ s/^ /$op/;
248 foreach my $item ($block->insert) {
249 my $op = $item->{"sign"}; # +
250 my $i = $item->{"item_no"};
251 my $offset = $i - $hunk->{"start2"} + $num_removed;
252 splice(@outlist,$offset,0,"$op$$fileref2[$i]");
257 map {s/$/\n/} @outlist; # add \n's
262 sub output_context_diff {
263 my ($hunk, $fileref1, $fileref2) = @_;
266 print "***************\n";
267 # Calculate item number range.
268 my $range1 = $hunk->context_range(1);
269 my $range2 = $hunk->context_range(2);
271 # Print out file 1 part for each block in context diff format if there are
272 # any blocks that remove items
273 print "*** $range1 ****\n";
274 my $low = $hunk->{"start1"};
275 my $hi = $hunk->{"end1"};
276 if (@blocklist = grep {$_->remove} @{$hunk->{"blocks"}}) {
277 my @outlist = @$fileref1[$low..$hi];
278 map {s/^/ /} @outlist; # assume it's just context
279 foreach my $block (@blocklist) {
280 my $op = $block->op; # - or !
281 foreach my $item ($block->remove) {
282 $outlist[$item->{"item_no"} - $low] =~ s/^ /$op/;
285 map {s/$/\n/} @outlist; # add \n's
289 print "--- $range2 ----\n";
290 $low = $hunk->{"start2"};
291 $hi = $hunk->{"end2"};
292 if (@blocklist = grep {$_->insert} @{$hunk->{"blocks"}}) {
293 my @outlist = @$fileref2[$low..$hi];
294 map {s/^/ /} @outlist; # assume it's just context
295 foreach my $block (@blocklist) {
296 my $op = $block->op; # + or !
297 foreach my $item ($block->insert) {
298 $outlist[$item->{"item_no"} - $low] =~ s/^ /$op/;
301 map {s/$/\n/} @outlist; # add \n's
307 # Generate a range of item numbers to print. Only print 1 number if the range
308 # has only one item in it. Otherwise, it's 'start,end'
309 my ($hunk, $flag) = @_;
310 my ($start, $end) = ($hunk->{"start$flag"},$hunk->{"end$flag"});
311 $start++; $end++; # index from 1, not zero
312 my $range = ($start < $end) ? "$start,$end" : $end;
317 # Generate a range of item numbers to print for unified diff
318 # Print number where block starts, followed by number of lines in the block
319 # (don't print number of lines if it's 1)
320 my ($hunk, $flag) = @_;
321 my ($start, $end) = ($hunk->{"start$flag"},$hunk->{"end$flag"});
322 $start++; $end++; # index from 1, not zero
323 my $length = $end - $start + 1;
324 my $first = $length < 2 ? $end : $start; # strange, but correct...
325 my $range = $length== 1 ? $first : "$first,$length";
330 # Package Block. A block is an operation removing, adding, or changing
331 # a group of items. Basically, this is just a list of changes, where each
332 # change adds or deletes a single item.
333 # (Change could be a separate class, but it didn't seem worth it)
337 # Input is a chunk from &Algorithm::LCS::diff
339 # length_diff - how much longer file 2 is than file 1 due to this block
341 # sign - '+' for insert, '-' for remove
342 # item_no - number of the item in the file (e.g., line number)
343 # We don't bother storing the text of the item
345 my ($class,$chunk) = @_;
348 # This just turns each change into a hash.
349 foreach my $item (@$chunk) {
350 my ($sign, $item_no, $text) = @$item;
351 my $hashref = {"sign" => $sign, "item_no" => $item_no};
352 push @changes, $hashref;
355 my $block = { "changes" => \@changes };
356 bless $block, $class;
358 $block->{"length_diff"} = $block->insert - $block->remove;
363 # LOW LEVEL FUNCTIONS
365 # what kind of block is this?
367 my $insert = $block->insert;
368 my $remove = $block->remove;
370 $remove && $insert and return '!';
371 $remove and return '-';
372 $insert and return '+';
373 warn "unknown block type";
374 return '^'; # context block
377 # Returns a list of the changes in this block that remove items
378 # (or the number of removals if called in scalar context)
379 sub remove { return grep {$_->{"sign"} eq '-'} @{shift->{"changes"}}; }
381 # Returns a list of the changes in this block that insert items
382 sub insert { return grep {$_->{"sign"} eq '+'} @{shift->{"changes"}}; }
384 } # end of package Block