/usr/bin/diffindex-rred is in apt-file 2.5.0ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | #!/usr/bin/perl -w
# Copyright 2008 Stefan Fritsch <sf@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use File::Temp qw/tmpnam/;
if ( !scalar @ARGV or $ARGV[0] eq '-h' or $ARGV[0] eq '--help' ) {
print << "EOF";
usage:
$0 <diff1> [ <diff2> [...] ]
Input will be taken from stdin and written to stdout.
The files diff1 ... need to be ed style diffs from 'diff --ed'.
*.gz, *.bz2, and *.lzo patches are decompressed automatically
EOF
exit 1 if !scalar @ARGV;
exit 0;
}
my $patches;
my $filename;
foreach my $filename (@ARGV) {
if ( $filename =~ m{\.gz$} ) {
$filename = "gunzip -c $filename |";
}
elsif ( $filename =~ m{\.bz2$} ) {
$filename = "bunzip2 -c $filename |";
}
elsif ( $filename =~ m{\.lzo$} ) {
$filename = "lzop -dc $filename |";
}
else {
$filename = "<$filename";
}
$patches = merge_patches(read_patch($filename), $patches);
}
my $cur = 1;
my $p;
while (defined ($p = $patches->() )) {
my ( $op, $lines, $lines_ref ) = @{$p};
if ( $op eq 'D' ) {
die "Invalid number in D: $lines" unless $lines > 0;
while ($lines-- > 0) {
my $line = <STDIN>;
if ( !defined $line ) {
die "end of input while at: @{$p}\n";
}
}
}
elsif ($op eq 'I') {
print @{$lines_ref};
}
elsif ($op eq 'C') {
die "Invalid number in C: $lines" unless $lines > 0;
while ($lines-- > 0) {
my $line = <STDIN>;
if ( !defined $line ) {
die "end of input while at: @{$p}\n";
}
print $line;
}
}
}
# copy rest of file
my $line;
while ( defined( $line = <STDIN> ) ) {
print $line;
}
# Reads a diff --ed style patch file
# Returns iterator function over patch hunks
# - in reverse order
# - converted into simple operations: _C_opy, _I_nsert, _D_elete
# Each hunk is an array consisting of
# - operation
# - number of lines
# - ref to array of lines (only for insert)
sub read_patch {
my $filename = shift;
open( my $fh_patch, $filename ) or die "Could not open '$filename'\n";
my $line;
my @hunks;
# current position in the file
my $num;
while ( defined( $line = <$fh_patch> ) ) {
chomp $line;
my ($op, $n, $m);
if ( $line =~ /^(\d+)(a)$/ ) {
# Na: add lines after line N
$n = $1;
$op = $2;
$m = $n;
$n++;
}
elsif ( $line =~ /^(\d+)([cd])$/ ) {
# Nd: remove line N
# Nc: remove line N, then insert after line N-1
$n = $1;
$op = $2;
$m = $n;
}
elsif ( $line =~ /^(\d+),(\d+)([cd])$/ ) {
# N,Md: remove lines N to M
# N,Mc: remove lines N to M, then insert after line M-1
$n = $1;
$m = $2;
$op = $3;
}
elsif ( $line eq 's/.//' ) {
# replace inserted ".." line by "."
my $lines_ref = $hunks[0]->[2];
if ( ref($lines_ref) ne 'ARRAY') {
die "Invalid format:$filename:$.:$line:Not preceeded by add or change\n";
}
if ( $lines_ref->[-1] ne "..\n" ) {
die "Invalid format:$filename:$.:$line:Not preceeded by '..'\n";
}
else {
$lines_ref->[-1] = ".\n";
}
}
elsif ( $line eq 'a' ) {
# insert some more lines after the 's/.//' replacement
my $lines_ref = $hunks[0]->[2];
if ( ref($lines_ref) ne 'ARRAY' ) {
die "Invalid format:$filename:$.:$line:Not preceeded by add or change\n";
}
while ( defined( $line = <$fh_patch> ) and $line ne ".\n" ) {
push @$lines_ref, $line;
$hunks[0]->[1]++;
}
}
else {
die "Invalid format:$filename:$.:$line\n";
}
if (!defined $op) {
# we are merging with the last op
next;
}
if ( $num ) {
if ( $num < $n || $num < $m ) {
die "Not in reverse order:$filename:$.:$line\n$num\n";
}
my $copy = $num - $m - 1;
unshift @hunks, [ 'C', $copy ] if $copy;
}
$num = $n;
if ($op eq 'd' || $op eq 'c') {
unshift @hunks, [ 'D', $m - $n + 1];
}
if ($op eq 'a' || $op eq 'c') {
my @lines;
while ( defined( $line = <$fh_patch> ) and $line ne ".\n" ) {
push @lines, $line;
}
unshift @hunks, [ 'I', scalar @lines, \@lines ];
}
}
if (defined $num && $num > 1) {
# copy the rest of the file, i.e. the beginning since we are operating
# in reverse order
unshift @hunks, [ 'C', $num - 1];
}
#print STDERR "@$_\n" foreach (@hunks);
#print STDERR "\n";
close($fh_patch) or die "Error reading patch";
return sub { shift @hunks };
}
# merges two patches
# in1 is applied first, then in2
# returns iterator function over merged patch hunks
sub merge_patches {
my ($in1, $in2) = @_;
return $in1 unless $in2;
return $in2 unless $in1;
my $cur1 = $in1->();
my $cur2 = $in2->();
return sub {
my $ret;
while (1) {
#print STDERR "in:cur1: @$cur1\n" if $cur1;
#print STDERR "in:cur2: @$cur2\n" if $cur2;
if (!defined $cur1) {
$ret = $cur2;
$cur2 = $in2->();
last;
}
if (!defined $cur2) {
$ret = $cur1;
$cur1 = $in1->();
last;
}
if ($cur1->[0] eq 'I') {
$ret = $cur1;
$cur1 = $in1->();
last;
}
elsif ($cur2->[0] eq 'D') {
$ret = $cur2;
$cur2 = $in2->();
last;
}
elsif ($cur1->[1] >= $cur2->[1]) {
if ($cur2->[0] eq 'C') {
$ret = [ $cur1->[0], $cur2->[1] ];
}
elsif ($cur1->[0] eq 'D') {
# cur2 was 'I'; discard it
$cur1->[1] -= $cur2->[1];
$cur1 = $in1->() unless $cur1->[1] > 0;
$cur2 = $in2->();
next;
}
else {
# cur2 is 'I', cur1 is 'C'
$ret = $cur2;
}
$cur1->[1] -= $cur2->[1];
$cur2 = $in2->();
$cur1 = $in1->() unless $cur1->[1] > 0;
last;
}
else {
# cur1 is smaller than cur2
if ($cur1->[0] eq 'D') {
if ($cur2->[0] eq 'I') {
my $n = $cur1->[1];
my $list_ref = $cur2->[2];
$cur2->[2] = [@{$list_ref}[$n .. $#{$list_ref}]];
$cur2->[1] -= $n;
$cur1 = $in1->();
next;
}
else {
$ret = $cur1;
$cur2->[1] -= $cur1->[1];
$cur1 = $in1->();
last;
}
}
else {
# cur1 is 'C'
my $n = $cur1->[1];
$ret = [ $cur2->[0], $n];
if ($cur2->[0] eq 'I') {
my $list_ref = $cur2->[2];
push @$ret, [@{$list_ref}[0 .. $n -1]];
$cur2->[2] = [@{$list_ref}[$n .. $#{$list_ref}]]
}
$cur2->[1] -= $n;
$cur1 = $in1->();
last;
}
}
}
#print STDERR "out:ret: @$ret\n" if $ret;
return $ret;
};
}
# our style is roughly "perltidy -pbp"
# vim:sts=4:sw=4:expandtab
|