2002-12-12 00:10:17 +03:00
|
|
|
#! @PERL@ -w
|
2002-12-09 23:16:34 +03:00
|
|
|
#
|
|
|
|
# Written by Adam Byrtek <alpha@debian.org>, 2002
|
2003-01-13 15:07:45 +03:00
|
|
|
#
|
2003-03-11 10:14:03 +03:00
|
|
|
# Extfs to handle patches in context and unified diff format.
|
|
|
|
# Known issues: When name of file to patch is modified during editing,
|
|
|
|
# hunk is duplicated on copyin. It is unavoidable.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-19 21:20:11 +03:00
|
|
|
use bytes;
|
2002-12-09 23:16:34 +03:00
|
|
|
use strict;
|
2002-12-19 21:20:11 +03:00
|
|
|
use POSIX;
|
2003-02-26 18:08:37 +03:00
|
|
|
use File::Temp 'tempfile';
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-09 23:16:34 +03:00
|
|
|
# standard binaries
|
2003-01-13 15:07:45 +03:00
|
|
|
my $bzip = 'bzip2';
|
|
|
|
my $gzip = 'gzip';
|
2003-03-11 10:14:03 +03:00
|
|
|
my $fileutil = 'file';
|
2000-09-18 18:25:47 +04:00
|
|
|
|
2002-12-09 23:16:34 +03:00
|
|
|
# date parsing requires Date::Parse from TimeDate module
|
2003-02-26 18:08:37 +03:00
|
|
|
my $parsedates = eval 'require Date::Parse';
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-01-13 15:07:45 +03:00
|
|
|
# regular expressions
|
|
|
|
my $unified_header=qr/^--- .*\n\+\+\+ .*\n@@ .* @@.*\n$/;
|
|
|
|
my $unified_extract=qr/^--- ([^\s]+).*\n\+\+\+ ([^\s]+)\s*([^\t\n]*)/;
|
2003-03-27 19:10:42 +03:00
|
|
|
my $unified_contents=qr/^([+\-\\ \n]|@@ .* @@)/;
|
2003-01-13 15:07:45 +03:00
|
|
|
|
|
|
|
my $context_header=qr/^\*\*\* .*\n--- .*\n\*{15}\n$/;
|
|
|
|
my $context_extract=qr/^\*\*\* ([^\s]+).*\n--- ([^\s]+)\s*([^\t\n]*)/;
|
2003-03-27 19:10:42 +03:00
|
|
|
my $context_contents=qr/^([!+\-\\ \n]|-{3} .* -{4}|\*{3} .* \*{4}|\*{15})/;
|
2003-01-13 15:07:45 +03:00
|
|
|
|
|
|
|
my $ls_extract_id=qr/^[^\s]+\s+[^\s]+\s+([^\s]+)\s+([^\s]+)/;
|
|
|
|
my $basename=qr|^(.*/)*([^/]+)$|;
|
|
|
|
|
2002-12-13 07:42:08 +03:00
|
|
|
|
|
|
|
# output unix date in a mc-readable format
|
2002-12-09 23:16:34 +03:00
|
|
|
sub timef
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2002-12-09 23:16:34 +03:00
|
|
|
my @time=localtime($_[0]);
|
2003-02-26 18:08:37 +03:00
|
|
|
return sprintf '%02d-%02d-%02d %02d:%02d', $time[4]+1, $time[3],
|
2002-12-13 08:08:14 +03:00
|
|
|
$time[5]+1900, $time[2], $time[1];
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-13 07:42:08 +03:00
|
|
|
# parse given string as a date and return unix time
|
2002-12-09 23:16:34 +03:00
|
|
|
sub datetime
|
|
|
|
{
|
|
|
|
# in case of problems fall back to 0 in unix time
|
|
|
|
# note: str2time interprets some wrong values (eg. " ") as 'today'
|
|
|
|
if ($parsedates && defined (my $t=str2time($_[0]))) {
|
2002-12-13 08:22:09 +03:00
|
|
|
return timef($t);
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
2002-12-13 08:22:09 +03:00
|
|
|
return timef(time);
|
2002-12-13 07:42:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# print message on stderr and exit
|
|
|
|
sub error
|
|
|
|
{
|
2002-12-19 19:51:04 +03:00
|
|
|
print STDERR $_[0], "\n";
|
2002-12-13 07:42:08 +03:00
|
|
|
exit 1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
# (compressed) input
|
|
|
|
sub myin
|
|
|
|
{
|
|
|
|
my ($qfname)=(quotemeta $_[0]);
|
|
|
|
|
2003-03-11 10:14:03 +03:00
|
|
|
$_=`$fileutil $qfname`;
|
2003-02-26 18:08:37 +03:00
|
|
|
if (/bzip/) {
|
|
|
|
return "$bzip -dc $qfname";
|
|
|
|
} elsif (/gzip/) {
|
|
|
|
return "$gzip -dc $qfname";
|
|
|
|
} else {
|
|
|
|
return "cat $qfname";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# (compressed) output
|
|
|
|
sub myout
|
|
|
|
{
|
|
|
|
my ($qfname,$append)=(quotemeta $_[0],$_[1]);
|
|
|
|
my ($sep) = $append ? '>>' : '>';
|
|
|
|
|
2003-03-11 10:14:03 +03:00
|
|
|
$_=`$fileutil $qfname`;
|
2003-02-26 18:08:37 +03:00
|
|
|
if (/bzip/) {
|
|
|
|
return "$bzip -c $sep $qfname";
|
|
|
|
} elsif (/gzip/) {
|
|
|
|
return "$gzip -c $sep $qfname";
|
|
|
|
} else {
|
|
|
|
return "cat $sep $qfname";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# select diff filename conforming with rules found in diff.info
|
|
|
|
sub diff_filename
|
|
|
|
{
|
|
|
|
my ($fsrc,$fdst)=@_;
|
|
|
|
|
|
|
|
if (!$fdst && !$fsrc) {
|
|
|
|
error 'Index: not yet implemented';
|
|
|
|
} elsif (!$fsrc || $fsrc eq '/dev/null') {
|
|
|
|
return ($fdst,'PATCH-CREATE/');
|
|
|
|
} elsif (!$fdst || $fdst eq '/dev/null') {
|
|
|
|
return ($fsrc,'PATCH-REMOVE/');
|
|
|
|
} elsif (($fdst eq '/dev/null') && ($fsrc eq '/dev/null')) {
|
|
|
|
error 'Malformed diff';
|
|
|
|
} else {
|
|
|
|
# fewest path name components
|
|
|
|
if ($fdst=~s|/|/|g < $fsrc=~s|/|/|g) {
|
|
|
|
return ($fdst,'');
|
|
|
|
} elsif ($fdst=~s|/|/|g > $fsrc=~s|/|/|g) {
|
|
|
|
return ($fsrc,'');
|
|
|
|
} else {
|
|
|
|
# shorter base name
|
|
|
|
if (($fdst=~/$basename/,length $2) < ($fsrc=~/$basename/,length $2)) {
|
|
|
|
return ($fdst,'');
|
|
|
|
} elsif (($fdst=~/$basename/,length $2) > ($fsrc=~/$basename/,length $2)) {
|
|
|
|
return ($fsrc,'');
|
|
|
|
} else {
|
|
|
|
# shortest names
|
|
|
|
if (length $fdst < length $fsrc) {
|
|
|
|
return ($fdst,'');
|
|
|
|
} else {
|
|
|
|
return ($fsrc,'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# parse unified or context header
|
|
|
|
sub parse_header
|
|
|
|
{
|
|
|
|
my ($unified,$context,$buf)=@_;
|
|
|
|
|
|
|
|
if ($unified) {
|
|
|
|
error "Can't parse unified diff header"
|
|
|
|
unless ((($$buf.=<I>).=<I>)=~/$unified_header/);
|
|
|
|
return $$buf=~/$unified_extract/;
|
|
|
|
} elsif ($context) {
|
|
|
|
error "Can't parse context diff header"
|
|
|
|
unless ((($$buf.=<I>).=<I>)=~/$context_header/);
|
|
|
|
return $$buf=~/$context_extract/;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-13 07:42:08 +03:00
|
|
|
# list files affected by patch
|
2002-12-09 23:16:34 +03:00
|
|
|
sub list
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2002-12-18 17:31:54 +03:00
|
|
|
my ($archive)=(quotemeta $_[0]);
|
2002-12-19 21:20:11 +03:00
|
|
|
my ($state,$pos,$len,$time);
|
2002-12-13 07:42:08 +03:00
|
|
|
my ($f,$fsrc,$fdst,$prefix);
|
2003-01-13 15:07:45 +03:00
|
|
|
my ($unified,$context)=(0,0);
|
2002-12-13 07:42:08 +03:00
|
|
|
|
|
|
|
# use uid and gid from file
|
2003-01-13 15:07:45 +03:00
|
|
|
my ($uid,$gid)=(`ls -l $archive`=~/$ls_extract_id/);
|
2002-12-09 23:16:34 +03:00
|
|
|
|
|
|
|
import Date::Parse if ($parsedates);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-18 17:31:54 +03:00
|
|
|
# state==1 means diff contents, state==0 means comments
|
2003-01-13 15:07:45 +03:00
|
|
|
$state=0; $len=0; $f='';
|
2002-12-09 23:16:34 +03:00
|
|
|
while (<I>) {
|
2003-01-13 15:07:45 +03:00
|
|
|
|
|
|
|
# recognize diff type
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$unified=1 if (/^--- /);
|
|
|
|
$context=1 if (/^\*\*\* /);
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$len+=length;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($unified && /^--- /) || ($context && /^\*\*\* [^\*]*$/)) {
|
|
|
|
# start of new file
|
2002-12-09 23:16:34 +03:00
|
|
|
if ($state==1) {
|
2002-12-19 21:20:11 +03:00
|
|
|
printf "-rw-r--r-- 1 %s %s %d %s %s%s\n", $uid, $gid, $len, datetime($time), $prefix, $f
|
2002-12-09 23:16:34 +03:00
|
|
|
if $f;
|
2002-12-19 21:20:11 +03:00
|
|
|
$len=0;
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
|
|
|
$state=1;
|
2002-12-13 07:42:08 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
($fsrc,$fdst,$time)=parse_header($unified,$context,\$_);
|
|
|
|
($f,$prefix)=diff_filename($fsrc,$fdst);
|
2002-12-13 07:42:08 +03:00
|
|
|
$f=$f.".diff";
|
|
|
|
|
2003-01-13 15:07:45 +03:00
|
|
|
} elsif ($state==1 && (($unified && !/$unified_contents/) || ($context && !/$context_contents/))) {
|
2002-12-09 23:16:34 +03:00
|
|
|
# start of comments, end of diff contents
|
2002-12-19 21:20:11 +03:00
|
|
|
printf "-rw-r--r-- 1 %s %s %d %s %s%s\n", $uid, $gid, $len, datetime($time), $prefix, $f
|
2002-12-09 23:16:34 +03:00
|
|
|
if $f;
|
2002-12-19 21:20:11 +03:00
|
|
|
$state=$len=0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2003-01-13 15:07:45 +03:00
|
|
|
|
2002-12-19 21:20:11 +03:00
|
|
|
$len+=length;
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
2002-12-19 21:20:11 +03:00
|
|
|
printf "-rw-r--r-- 1 %s %s %d %s %s%s\n", $uid, $gid, $len, datetime($time), $prefix, $f
|
2003-02-26 18:08:37 +03:00
|
|
|
if ($f && $state==1);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-01-13 15:07:45 +03:00
|
|
|
# extract diff from patch
|
2002-12-09 23:16:34 +03:00
|
|
|
sub copyout
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2002-12-09 23:16:34 +03:00
|
|
|
my ($file,$out)=@_;
|
2002-12-13 07:42:08 +03:00
|
|
|
my ($fsrc,$fdst,$found,$state,$buf);
|
2003-01-13 15:07:45 +03:00
|
|
|
my ($unified,$context)=(0,0);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-13 07:42:08 +03:00
|
|
|
$file=~s/^(PATCH-(CREATE|REMOVE)\/)?(.*)\.diff$/$3/;
|
|
|
|
|
|
|
|
# state==1 means diff contents, state==0 mens comments
|
2003-01-13 15:07:45 +03:00
|
|
|
$state=0; $found=0; $buf='';
|
2002-12-09 23:16:34 +03:00
|
|
|
while (<I>) {
|
2003-01-13 15:07:45 +03:00
|
|
|
|
|
|
|
# recognize diff type
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$unified=1 if (/^--- /);
|
|
|
|
$context=1 if (/^\*\*\* /);
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$buf.=$_;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($unified && /^--- /) || ($context && /^\*\*\* [^\*]*$/)) {
|
2002-12-13 07:42:08 +03:00
|
|
|
last if ($state==1 && $found);
|
2002-12-09 23:16:34 +03:00
|
|
|
$state=1;
|
2002-12-13 07:42:08 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
($fsrc,$fdst,)=parse_header($unified,$context,\$_);
|
2002-12-13 07:42:08 +03:00
|
|
|
$found=1 if (($fsrc eq $file) || ($fdst eq $file));
|
|
|
|
|
2003-01-13 15:07:45 +03:00
|
|
|
} elsif ($state==1 && (($unified && !/$unified_contents/) || ($context && !/$context_contents/))) {
|
2002-12-09 23:16:34 +03:00
|
|
|
# start of comments, end of diff contents
|
2002-12-13 07:42:08 +03:00
|
|
|
last if ($found);
|
2002-12-09 23:16:34 +03:00
|
|
|
$state=0;
|
2003-02-26 18:08:37 +03:00
|
|
|
$buf='';
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
2003-01-13 15:07:45 +03:00
|
|
|
|
2002-12-13 07:42:08 +03:00
|
|
|
$buf.=$_ if ($found || $state==0)
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
2002-12-13 07:42:08 +03:00
|
|
|
if ($found) {
|
|
|
|
open O, "> $out";
|
2002-12-09 23:16:34 +03:00
|
|
|
print O $buf;
|
2002-12-13 07:42:08 +03:00
|
|
|
close O;
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
2002-12-13 07:42:08 +03:00
|
|
|
}
|
2002-12-09 23:16:34 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
# remove diff(s) from patch
|
|
|
|
sub rm
|
2002-12-13 07:42:08 +03:00
|
|
|
{
|
2003-02-26 18:08:37 +03:00
|
|
|
my ($archive)=(shift);
|
|
|
|
my ($fsrc,$fdst,$found,$state,$buf);
|
|
|
|
my ($tmp,$tmpname)=tempfile();
|
|
|
|
my ($unified,$context)=(0,0);
|
2002-12-13 07:42:08 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
@_=map {scalar(s/^(PATCH-(CREATE|REMOVE)\/)?(.*)\.diff$/$3/,$_)} @_;
|
2002-12-13 07:42:08 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
# state==1 means diff contents, state==0 mens comments
|
|
|
|
$state=0; $found=0; $buf='';
|
|
|
|
while (<I>) {
|
2003-01-13 15:07:45 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
# recognize diff type
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$unified=1 if (/^--- /);
|
|
|
|
$context=1 if (/^\*\*\* /);
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$buf.=$_;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($unified && /^--- /) || ($context && /^\*\*\* [^\*]*$/)) {
|
|
|
|
$state=1;
|
|
|
|
|
|
|
|
($fsrc,$fdst,)=parse_header($unified,$context,\$_);
|
|
|
|
|
|
|
|
# remove listed files
|
|
|
|
foreach (@_) {
|
|
|
|
if (($fsrc eq $_) || ($fdst eq $_)) {
|
|
|
|
$found=1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
print $tmp $buf;
|
|
|
|
$buf='';
|
|
|
|
}
|
|
|
|
|
|
|
|
} elsif ($state==1 && (($unified && !/$unified_contents/) || ($context && !/$context_contents/))) {
|
|
|
|
# start of comments, end of diff contents
|
|
|
|
$found=0;
|
|
|
|
$state=0;
|
|
|
|
$buf='';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state==0) {
|
|
|
|
$buf.=$_;
|
|
|
|
} elsif (!$found) {
|
|
|
|
print $tmp $_;
|
|
|
|
}
|
2002-12-13 07:42:08 +03:00
|
|
|
}
|
2003-02-26 18:08:37 +03:00
|
|
|
print $tmp $buf if (!$found);
|
|
|
|
close $tmp;
|
|
|
|
close I;
|
|
|
|
|
|
|
|
# replace archive with temporary file
|
|
|
|
system('cat '.quotemeta($tmpname).'|'.myout($archive,0))==0
|
|
|
|
or error "Can't write to archive";
|
|
|
|
system 'rm -f '.quotemeta($tmpname);
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-02-26 18:08:37 +03:00
|
|
|
# append diff to archive
|
|
|
|
sub copyin
|
2002-12-13 07:42:08 +03:00
|
|
|
{
|
2003-02-26 18:08:37 +03:00
|
|
|
my ($archive,$name,$src)=(@_);
|
|
|
|
my ($fsrc,$fdst,$f,@files);
|
|
|
|
my ($unified,$context)=(0,0);
|
|
|
|
|
|
|
|
# build filelist
|
|
|
|
open I, myin($src).'|';
|
|
|
|
while (<I>) {
|
|
|
|
# recognize diff type
|
|
|
|
if (!$unified && !$context) {
|
|
|
|
$unified=1 if (/^--- /);
|
|
|
|
$context=1 if (/^\*\*\* /);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($unified && /^--- /) || ($context && /^\*\*\* [^\*]*$/)) {
|
|
|
|
($fsrc,$fdst,)=parse_header($unified,$context,\$_);
|
|
|
|
($f,)=diff_filename($fsrc,$fdst);
|
|
|
|
push(@files,$f);
|
|
|
|
}
|
2002-12-13 07:42:08 +03:00
|
|
|
}
|
2003-02-26 18:08:37 +03:00
|
|
|
close I;
|
|
|
|
|
|
|
|
# remove overwrited files
|
|
|
|
open I, myin($archive).'|';
|
|
|
|
rm ($archive, map($_.'.diff',@files));
|
|
|
|
close I;
|
|
|
|
|
2003-03-11 10:14:03 +03:00
|
|
|
my $cmd1=myin($src);
|
|
|
|
my $cmd2=myout($archive,1);
|
2003-02-26 18:08:37 +03:00
|
|
|
system("$cmd1 | $cmd2")==0
|
|
|
|
or error "Can't write to archive";
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-13 07:42:08 +03:00
|
|
|
|
2003-01-13 15:07:45 +03:00
|
|
|
if ($ARGV[0] eq 'list') {
|
2003-02-26 18:08:37 +03:00
|
|
|
open I, myin($ARGV[1]).'|';
|
2002-12-13 07:42:08 +03:00
|
|
|
list $ARGV[1];
|
|
|
|
exit 0;
|
2004-08-19 19:43:09 +04:00
|
|
|
} elsif ($ARGV[0] eq 'copyout') {
|
2003-02-26 18:08:37 +03:00
|
|
|
open I, myin($ARGV[1])."|";
|
2002-12-09 23:16:34 +03:00
|
|
|
copyout ($ARGV[2], $ARGV[3]);
|
2002-12-13 07:42:08 +03:00
|
|
|
exit 0;
|
2004-08-19 19:43:09 +04:00
|
|
|
} elsif ($ARGV[0] eq 'rm') {
|
2003-02-26 18:08:37 +03:00
|
|
|
open I, myin($ARGV[1])."|";
|
|
|
|
rm ($ARGV[1], $ARGV[2]);
|
|
|
|
exit 0;
|
2004-08-19 19:43:09 +04:00
|
|
|
} elsif ($ARGV[0] eq 'rmdir') {
|
2003-02-26 18:08:37 +03:00
|
|
|
exit 0;
|
2004-08-19 19:43:09 +04:00
|
|
|
} elsif ($ARGV[0] eq 'mkdir') {
|
2003-02-26 18:08:37 +03:00
|
|
|
exit 0;
|
2004-08-19 19:43:09 +04:00
|
|
|
} elsif ($ARGV[0] eq 'copyin') {
|
2002-12-13 07:42:08 +03:00
|
|
|
copyin ($ARGV[1], $ARGV[2], $ARGV[3]);
|
|
|
|
exit 0;
|
2002-12-09 23:16:34 +03:00
|
|
|
}
|
2002-12-13 07:42:08 +03:00
|
|
|
exit 1;
|