In my shop, we use cvs. To compare the differences between two versions, I used to do:
cvs diff -y -r v1 -r v2 >out.txt 2>&1
This works fine with comparing two source files. However when I compare two directories, the output file becomes really big, and even worse, the output fom cvs is a mixtrue of differences and common lines, so the differences are just buried among all those common lines.
I want to extra those real differences.
For those lines really different between two versions, its 63th column is one of those three chars: |, >, or <.
The part caused me a little bit effort is to translate \t into blanks, as I have to calculate the number of blanks. (\t is not always 8 blanks, but rather padding up to the next 8's multiplier)
To use this program, do this:
real_diff.pl out
It would create a file called out.rl, which only contains those lines that are really different between two versions.
real_diff.pl
#!/usr/bin/perl
use strict;
use warnings;
my $directory;
if (!$ARGV[0]) {
die "Usage: real_diff.pl directory_name\n";
} else {
$directory = $ARGV[0];
}
open(DIFF_FILE, "<", "$directory.txt") || die "There is no diff file f
+or directory $directory\n";
my @lines = <DIFF_FILE>;
close(DIFF_FILE);
print "Now creating real diff file $directory.rl ...\n";
my $prev_line;
my $current_file;
open(REAL_DIFF, ">", "$directory.rl");
for (0 .. $#lines) {
my $line = $lines[$_];
my $out_line = "";
for (my $i = 0; $i < length($line); $i ++) {
if (substr($line, $i, 1) eq "\t") {
$out_line .= " " x (8 - length($out_line) % 8);
} else {
$out_line .= substr($line, $i, 1);
}
}
if ($out_line =~ m/^RCS file: (.*?),/) {
$current_file = $1;
}
if ($out_line =~ m/^.{62}[<|>|\|]/) {
if (!$prev_line || ($_ > $prev_line + 1)) {
print REAL_DIFF "=========================================
+=============================\n";
print REAL_DIFF "line number in $directory.txt: " , $_ + 1
+, "\n";
print REAL_DIFF "Source file name: $current_file\n\n";
}
print REAL_DIFF $out_line;
$prev_line = $_;
}
}
close(REAL_DIFF);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.