Alien-wxWidgets
view release on metacpan or search on metacpan
inc/bin/patch view on Meta::CPAN
#!/usr/bin/perl -w
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
# patch - apply a diff file to an original
#
# mail tgy@chocobo.org < bug_reports
#
# Copyright (c) 1999 Moogle Stuffy Software. All rights reserved.
#
# You may play with this software in accordance with the Perl Artistic License.
use strict;
my $VERSION = '0.25';
$|++;
if (@ARGV && $ARGV[0] eq '-v') {
print split /^ /m, qq[
This is patch $VERSION written in Perl.
Copyright (c) 1999 Moogle Stuffy Software. All rights reserved.
You may play with this software in accordance with the
Perl Artistic License.
];
exit;
}
my ($patchfile, @options);
if (@ARGV) {
require Getopt::Long;
Getopt::Long::Configure(qw/
bundling
no_ignore_case
/);
# List of supported options and acceptable arguments.
my @desc = qw/
suffix|b=s force|f reject-file|r=s
prefix|B=s batch|t reverse|R
context|c fuzz|F=i silent|quiet|s
check|C ignore-whitespace|l skip|S
directory|d=s normal|n unified|u
ifdef|D=s forward|N version|v
ed|e output|o=s version-control|V=s
remove-empty-files|E strip|p=i debug|x=i
/;
# Each patch may have its own set of options. These are separated by
# a '+' on the command line.
my @opts;
for (@ARGV, '+') { # Now '+' terminated instead of separated...
if ($_ eq '+') {
push @options, [splice @opts, 0];
} else {
push @opts, $_;
}
}
# Parse each set of options into a hash.
my $next = 0;
for (@options) {
local @ARGV = @$_;
Getopt::Long::GetOptions(\my %opts, @desc);
$opts{origfile} = shift;
$_ = \%opts;
$patchfile = shift unless $next++;
}
}
$patchfile = '-' unless defined $patchfile;
my $patch = Patch->new(@options);
tie *PATCH, Pushback => $patchfile or die "Can't open '$patchfile': $!";
# Extract patches from patchfile. We unread/pushback lines by printing to
# the PATCH filehandle: 'print PATCH'
PATCH:
while (<PATCH>) {
if (/^(\s*)(\@\@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? \@\@\n)/) {
# UNIFIED DIFF
my ($space, $range, $i_start, $i_lines, $o_start, $o_lines) =
($1, $2, $3, $4 || 1, $5, $6 || 1);
$patch->bless('unified') or next PATCH;
my @hunk;
my %saw = map {$_, 0} split //, ' +-';
my $re = qr/^$space([ +-])/;
while (<PATCH>) {
unless (s/$re/$1/) {
$patch->note("Short hunk ignored.\n");
$patch->reject($range, @hunk);
print PATCH;
next PATCH;
}
push @hunk, $_;
$saw{$1}++;
last if $saw{'-'} + $saw{' '} == $i_lines
&& $saw{'+'} + $saw{' '} == $o_lines;
}
$patch->apply($i_start, $o_start, @hunk)
or $patch->reject($range, @hunk);
} elsif (/^(\s*)\*{15}$/) {
# CONTEXT DIFF
my $space = $1;
$_ = <PATCH>;
inc/bin/patch view on Meta::CPAN
with a delete, which will fail, triggering the
heuristic.)
=item -s or --quiet or --silent
makes I<patch> do its work silently, unless an error
occurs.
=item -S or --skip
causes I<patch> to ignore this patch from the patch
file, but continue on looking for the next patch in
the file. Thus
patch -S + -S + < patchfile
will ignore the first and second of three patches.
=item -u or --unified
forces I<patch> to interpret the patch file as a unified
context diff (a unidiff).
=item -v or --version
causes I<patch> to print out its revision header and
patch level.
=item -V or --version-control
causes the next argument to be interpreted as a
method for creating backup file names. The type of
backups made can also be given in the B<VERSION>I<_>B<CONTROL>
environment variable, which is overridden by this
option. The B<-B> option overrides this option, causing
the prefix to always be used for making backup file
names. The value of the B<VERSION>I<_>B<CONTROL> environment
variable and the argument to the B<-V> option are like
the GNU Emacs `version-control' variable; they also
recognize synonyms that are more descriptive. The
valid values are (unique abbreviations are accepted):
=over
=item `t' or `numbered'
Always make numbered backups.
=item `nil' or `existing'
Make numbered backups of files that already
have them, simple backups of the others. This
is the default.
=item `never' or `simple'
Always make simple backups.
=back
=item -xnumber or --debug number
sets internal debugging flags,
and is of no interest to I<patch> patchers [see L<"note 8">].
=back
=head1 ENVIRONMENT
B<SIMPLE>I<_>B<BACKUP>I<_>B<SUFFIX>
Extension to use for backup file names instead of
".orig" or "~".
B<VERSION>I<_>B<CONTROL>
Selects when numbered backup files are made.
=head1 SEE ALSO
diff(1), ed(1)
=head1 NOTES FOR PATCH SENDERS
There are several things you should bear in mind if you
are going to be sending out patches. First, you can save
people a lot of grief by keeping a patchlevel.h file which
is patched to increment the patch level as the first diff
in the patch file you send out. If you put a Prereq: line
in with the patch, it won't let them apply patches out of
order without some warning. Second, make sure you've
specified the filenames right, either in a context diff
header, or with an Index: line. If you are patching something in a subdirectory, be sure to tell the patch user to
specify a B<-p> switch as needed. Third, you can create a
file by sending out a diff that compares a null file to
the file you want to create. This will only work if the
file you want to create doesn't exist already in the target directory. Fourth, take care not to send out reversed
patches, since it makes people wonder whether they already
applied the patch. Fifth, while you may be able to get
away with putting 582 diff listings into one file, it is
probably wiser to group related patches into separate
files in case something goes haywire.
=head1 DIAGNOSTICS
Too many to list here, but generally indicative that I<patch>
couldn't parse your patch file.
The message "Hmm..." indicates that there is unprocessed
text in the patch file and that I<patch> is attempting to
intuit whether there is a patch in that text and, if so,
what kind of patch it is.
I<Patch> will exit with a non-zero status if any reject files
were created. When applying a set of patches in a loop it
behooves you to check this exit status so you don't apply
a later patch to a partially patched file.
=head1 CAVEATS
I<Patch> cannot tell if the line numbers are off in an ed
script, and can only detect bad line numbers in a normal
diff when it finds a "change" or a "delete" command. A
context diff using fuzz factor 3 may have the same problem. Until a suitable interactive interface is added, you
should probably do a context diff in these cases to see if
inc/bin/patch view on Meta::CPAN
Check patch mode ( B<-C>) will fail if you try to check several patches in succession that build on each other. The
whole code of I<patch> would have to be restructured to keep
temporary files around so that it can handle this situation.
If code has been duplicated (for instance with #ifdef OLDCODE ... #else ... #endif), I<patch> is incapable of patch-
ing both versions, and, if it works at all, will likely
patch the wrong one, and tell you that it succeeded to
boot.
If you apply a patch you've already applied, I<patch> will
think it is a reversed patch, and offer to un-apply the
patch. This could be construed as a feature.
=head1 COMPATIBILITY
The perl implementation of patch is based on but not entire compatible with the
documentation for GNU patch version 2.1:
=head2 note 1
On systems that do not support long filenames,
GNU patch uses the extension "~" for backup files and the extension "#" for
reject files.
How to know if a system support long filenames?
=head2 note 2
Only new-style context diffs are supported.
What does old-style context diff look like?
=head2 note 3
If the pipe to ed fails, B<patch> will attempt to apply the ed script on its
own.
=head2 note 4
This algorithm differs from the one described in the documentation for GNU
patch, which scans forwards and backwards from the line number mentioned in the
diff (plus any offset used in applying the previous hunk).
=head2 note 5
Rejected hunks in GNU patch all come out as context diffs regardless of the
input diff, and the lines numbers reflect the approximate location GNU patch
thinks the failed hunks belong in the new file rather than the old one.
=head2 note 6
If the original file cannot be found or is read-only, but a suitable SCCS or RCS
file is handy, GNU patch will attempt to get or check out the file.
=head2 note 7
GNU patch requires a space between the B<-D> and the argument. This has been
made optional.
=head2 note 8
There are currently no debugging flags to go along with B<-x>.
=head1 AUTHOR
Fuzzy | tgy@chocobo.org | Will hack Perl for a moogle stuffy! =^.^=
=head1 COPYRIGHT
Copyright (c) 1999 Moogle Stuffy Software. All rights reserved.
You may play with this software in accordance with the Perl Artistic License.
You may use this documentation under the auspices of the GNU General Public
License.
=cut
( run in 0.516 second using v1.01-cache-2.11-cpan-119454b85a5 )