#!/usr/bin/perl
# Syntax:
#   external_tool | ./this_script.pl patch.diff
# Note only unified diff is supported

open(FILE, $ARGV[0]);

my %l;

my $file = "<Syntax error>";

# Fill a table with all chunk of modified lines 
while (<FILE>) {
    $file = $1 if (m/^\+\+\+ ([^\t ]*)/);
    if (m/^@@.*\+([0-9]+),([0-9]+) @@/) {
        push @{$l{$file}}, [ $1, $1 + $2 ];
    }
}

# Dump table
#foreach $k (keys %l) {
#    foreach $i (0 .. $#{$l{$k}}) {
#        print join ":", $k, @{$l{$k}[$i]}; 
#        print "\n"
#    }
#}

while (<STDIN>) {
    if (m/^([^:]+):([0-9]+)/) {
        if (defined $l{$1}) {
            foreach $i (0 .. $#{$l{$1}}) {
                print if ($2 >= $l{$1}[$i][0] and $2 <= $l{$1}[$i][1]);
            }
        }
    } else {
        # Print lines that doesn't looks like errors
        print;
    }
}

