Text-Patch-Rred
view release on metacpan or search on metacpan
lib/Text/Patch/Rred.pm view on Meta::CPAN
} else { # delete last line and stay at last line
$ra->[1] = --$i;
}
} elsif ($j < $ra->[3]) {
# delete non-existant line 0 and advance to line 1
$ra->[4] = $ra->[$j];
$ra->[1] = 5;
$ra->[2] = ++$j;
} # Last possibility is an empty file, do nothing
} elsif (/^([0-9]+)d/) {
_GoPos($ra, $1, $1);
_GoPos($ra, 1 + $1, $1); # Set current line to line after
} elsif (/^([0-9]+),([0-9]+)d/) {
_GoPos($ra, $1, $2);
_GoPos($ra, 1 + $1, $1); # Set current line to line after
} elsif (/^c\s*$/) {
$ra->[0] = 1;
if ($i > 4) {
$ra->[1] = --$i; # delete cur line and append after the one bef
} # Otherwise delete nonexistent line 0 and append there
} elsif (/^([0-9]+)c/) {
$ra->[0] = 1;
_GoPos($ra, $1, $1);
} elsif (/^([0-9]+),([0-9]+)c/) {
$ra->[0] = 1;
_GoPos($ra, $1, $2);
} elsif (m!^s/\^?\\?\.//\s*$!) {
if ($i > 4) {
# diff uses this to insert lines consisting of a single .
$_ = $ra->[ --$i ];
$$_ =~ s/^\.//;
$ra->[$i] = $_;
}
} elsif (/^w/) {
# ignore write command sometimes appended to patch files
} elsif (/^\s*$/) {
# ignore blank command lines (but not blank lines in added text)
} else {
carp "Unexpected non-patch ed command: '" . $_ . "'\n";
return;
}
return 1;
}
## use critic (ProhibitExcessComplexity, ProhibitCascadingIfElse)
=item B<Do> I<$edState>, I<@lines>
=item I<$edState>->B<Do>(I<@lines>)
Simply calls L<B<Do1>|/Do1 $edState, $patchline> for each element of
C<@lines>, returns a true value if all calls were successful or
C<@lines> was empty. Otherwise returns C<undef>.
=cut
## no critic (RequireArgUnpacking)
sub Do($;)
{
my $self = shift;
local $_ = undef;
my $ok = 1;
for (@_) {
unless (ref $_) {
Do1($self, $_) or ($ok = undef);
} else {
for (@$_) {
Do1($self, $_) or ($ok = undef);
}
}
}
return $ok;
}
## use critic (RequireArgUnpacking)
=item B<main> I<@ARGV>
The main program code of rred as a function, accepting the command
line syntax in the L<B<SYNOPSIS>|/Command Line:> above. Returns the
program exit code (0 ok, 1 error, 2 bad syntax). Running C<rred
args> is the same as running
perl -MText::Patch::Rred \
-e 'exit Text::Patch::Rred::main @ARGV' args
=cut
sub main(@)
{
my (@args) = @_;
if (@args < 3) {
print STDERR <<'ENDUSAGE'
Copyright (C) 2006-2009 Jakob Bohm. All Rights Reserved.
usage: rred file.old file.new file.patch1 [ file.patch2 ... ]
(perl open magics are supported in all file names)
Applies one or more diff --ed or diff -e patches to file.old, creating
file.new . file.old and file.new can be the same file. Perl open
magics are supported in all input file names. This help is displayed
on stderr, use rred --help 2>file to capture it.
example:
zcat Packages.diff/2006-04-23-1343.24.gz \
Packages.diff/2006-04-24-1329.37.gz |
rred Packages Packages -
ENDUSAGE
or croak $0. ': Printing usage: ' . $!;
return 2;
}
my $namIn = shift @args;
my $namOut = shift @args;
## no critic (ProhibitTwoArgOpen)
open FH, $namIn or croak "Loading '" . $namIn . "': " . $!;
my @lines = <FH> or croak "Loading '" . $namIn . "': " . $!;
close FH or croak "Loading '" . $namIn . "': " . $!;
my $edState = Init(@lines);
( run in 3.131 seconds using v1.01-cache-2.11-cpan-524268b4103 )