SORT BY:

LIST ORDER
THREAD
AUTHOR
SUBJECT


SEARCH

IPS HOME


    [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

    Re: change bars



    
    My original script had bugs in it. Here is a newer version with better
    revision tracking and hopefully fewer bugs. :-)
    
    -Costa
    
    On Wed, 5 Jul 2000, Matt Wakeley wrote:
    
    > Hi Costa,
    > 
    > Thanks for the perl script.  It really helps.
    > 
    > But I noticed that it did not catch the "difference" between 0628 and 0706 of the
    > changing of the "ping" and the new "map" commands in section 3.1.1 (these are "new"
    > lines that it did not flag).
    > 
    > -Matt
    > 
    > Costa Sapuntzakis wrote:
    > 
    > > I have included the Perl script I used to generate the change bars.
    > > The draft was generated as follows:
    > >
    > > perl create-change-bar.pl iSCSI000628.txt iscsi00615.txt > iSCSI000628.2.txt
    > >
    > > Hope it helps!
    > >
    > > -Costa
    > 
    > 
    > 
    
    #!
    
    #
    # The script operates in the following fashion. First, it
    # strips all blank lines, headers, and footers from both
    # document. 
    #
    # It then runs diff on the stripped files to find the
    # differences.
    #
    # Finally, it takes the output of diff and generates the
    # change bars at the destination.
    #
    # Note: the change bars only mark additions and changes
    #
    # -Costa (csapuntz@alum.mit.edu) 06/28/00
    
    use strict;
    
    use Getopt::Long;
    
    my $lines_flag = 0;
    
    sub usage {
        print STDERR "Usage:\nperl create-change-bar.pl [--lines] <file1> <file2>\n";
        print STDERR "file1 will be printed with change bars\n\n";
        print STDERR "--lines is a less exact match that may work better for\n";
        print STDERR "        large diffs\n";
        exit 1;
    }
    
    &GetOptions(lines => \$lines_flag);
    
    &usage if ($#ARGV != 1);
    
    my $src = $ARGV[0];
    my $dst = $ARGV[1];
    
    &usage if (!defined $src || !defined $dst);
    
    open(SRC, $src) or die "Unabled to open $src: $!";
    
    $src .= ".tmp";
    
    if (-r $src) {
        print STDERR "Error: temporary file $src already exists\n";
        exit 1;
    }
    
    open(SRC2, ">$src")  or die "Unabled to open $src: $!";;
    
    open(DST, $dst)  or die "Unabled to open $dst: $!";;
    
    $dst .= ".tmp";
    
    if (-r $dst) {
        print STDERR "Error: temporary file $dst already exists\n";
        exit 1;
    }
    
    open(DST2, ">$dst")  or die "Unabled to open $dst: $!";;
    
    
    sub process_file {
        my ($linemap, $fh1, $fh2) = @_;
    
        # Strip leading change bars
        s/^|//;
        
    
        my $oldlineno = -1;
        my $newlineno = 0;
        my $state = 0;
    
        my $saveword ;
        my $savewordlineno = 0;
        
        while(<$fh1>) {
    	chomp;
    	
    	$oldlineno++;
    
    	if ($state == 1) {
    	    $state = 0;
    	    next;
    	}
    	
    	if (/^\014/) {
    	    $state = 1;
    	    next;
    	}
    	next if (/\[Page/);
    	next if (/^\s*$/);
    	
    	if ( $lines_flag ) {
    	    $$linemap[$newlineno] = $oldlineno;
    	    $newlineno++;
    	    print $fh2 $_ . "\n";
    	} else {
    	    s/[\t\n\r]//g;
    	    my @words = split;
    	    
    	    if ($saveword) {
    		print $fh2 $saveword;
    		undef $saveword;
    	    }
    	    
    	    if ($#words >= 0 &&
    		$words[$#words] =~ /-$/) {
    		$saveword = substr(pop @words, 0, -1);
    		$savewordlineno = $oldlineno;
    	    }
    	    
    	    foreach my $word (@words) {
    #	    print $word . "\n";
    		print $fh2 $word . "\n";
    		
    		$$linemap[$newlineno] = $oldlineno;
    		$newlineno++;
    	    }
    	}
        }
    
    }
    
    #
    # Store mapping between stripped line numbers and original
    # line numbers
    #
    my @linemap = ();
    my @changes = ();
    
    &process_file(\@linemap, \*SRC, \*SRC2);
    &process_file([], \*DST, \*DST2);
    
    close DST2;
    close DST;
    close SRC2;
    
    #
    # diff the two documents
    #
    open (DIFFS, "diff -w $dst $src |") or die "Unabled to run diff: $!";
    
    while(<DIFFS>) {
    #    print ;
    
        next if (!/^\d/);
    
        /[acd](\d+)(,(\d+))?/;
        
        next if (!defined $1);
    
        my $start = $1;
        my $end = $3;
    
        if (!defined $end) {
    	$end = $start;
        }
    
        for (my $idx = $start - 1; $idx <= $end - 1; $idx++) {
    	$changes[$linemap[$idx]] = 1;
        }
    }
    
    #
    # Print out document, marking changed lines
    #
    seek SRC, 0, 0;
    
    my $oldlineno = 0;
    
    while (<SRC>) {
        # Strip leading change bars
        s/^|//;
    
        if ($changes[$oldlineno]) {
    	print "|";
        } else {
    	print " ";
        }
    
        print;
    
        $oldlineno++;
    }
    
    #
    # Cleanup
    #
    
    unlink $src;
    unlink $dst;
    
    
    
    


Home

Last updated: Tue Sep 04 01:08:10 2001
6315 messages in chronological order