A patch for Service Location Protocol support (derived from a
[rsync/rsync-patches.git] / verify-patches
index 661f8a8..28d37b9 100755 (executable)
@@ -1,6 +1,18 @@
 #!/usr/bin/perl
 
 use strict;
+use Getopt::Long;
+
+my($no_cvs, $failures_only, $minor_updates);
+
+&Getopt::Long::Configure('bundling');
+GetOptions(
+    'no-cvs|n' => \$no_cvs,
+    'failures-only|f' => \$failures_only,
+    'minor-updates|u' => \$minor_updates,
+) or &usage;
+
+my $interesting_fuzz = $minor_updates ? '\d' : '[2-9]';
 
 chdir('patches') if -d 'patches';
 
@@ -11,12 +23,15 @@ from inside the patches subdir.
 EOT
 }
 
-$ENV{'TZ'} = 'GMT';
+$| = 1;
+$ENV{'TZ'} = 'UTC';
+my $CONF_OPTS = '-C';
 
-my($has_dependencies, @new);
+my($has_dependencies, @new, @rejects);
 
 END {
     &restore_cvsdir;
+    system "rsync -a --delete cvsdir/ workdir/" if -d 'cvsdir';
 };
 
 my $root;
@@ -24,14 +39,13 @@ open(IN, '../CVS/Root') or die $!;
 chomp($root = <IN>);
 close IN;
 
-my $tmpdir = ',tmp-for-patch-tests';
-
-mkdir($tmpdir, 0777) unless -d $tmpdir;
-chdir($tmpdir) or die "Unable to chdir to $tmpdir";
+mkdir('tmp', 0777) unless -d 'tmp';
+chdir('tmp') or die "Unable to chdir to 'tmp'";
 
 mkdir('workdir') unless -d 'workdir';
 open(OUT, '>exclude') or die $!;
 print OUT <<EOT;
+CVS
 proto.h
 configure
 config.h.in
@@ -40,15 +54,17 @@ rsyncd.conf.5
 EOT
 close OUT;
 
-print "Using CVS to update the $tmpdir/cvsdir copy of the source.\n";
-system qq|cvs -d "$root" co -d cvsdir rsync|;
+unless ($no_cvs) {
+    print "Using CVS to update the tmp/cvsdir copy of the source.\n";
+    system qq|cvs -d "$root" co -d cvsdir rsync|;
+}
 
 @ARGV = glob('../*.diff') unless @ARGV;
 
 DIFF:
 foreach my $diff (@ARGV) {
     next unless $diff =~ /\.diff$/;
-    next if $diff =~ /gzip-rsyncable\.diff$/;
+    next if $diff =~ /gzip-rsyncable[-_a-z]*\.diff$/;
     $diff =~ s#^(patches|\.\.)/##;
 
     open(IN, "../$diff") or die $!;
@@ -57,9 +73,10 @@ foreach my $diff (@ARGV) {
        if (/^Depends-On-Patch: (\S+.diff)$/) {
            my $dep = $1;
            $has_dependencies = 1;
-           print "\nApplying dependency patch $dep\n";
-           if (system("patch -d cvsdir -p0 -b -Vt -Z <../$dep") != 0) {
-               print "Unable to cleanly apply depenency patch -- skipping $diff\n";
+           print "\nApplying dependency patch $dep...\n";
+           if (system("patch -d cvsdir -p0 -b -Vt -Zf <../$dep") != 0) {
+               print "Unable to cleanly apply dependency patch -- skipping $diff\n";
+               system "rm -f cvsdir/*.rej cvsdir/*/*.rej";
                &restore_cvsdir;
                next DIFF;
            }
@@ -67,63 +84,104 @@ foreach my $diff (@ARGV) {
     }
     close IN;
 
-    my $apply = 1;
-    my(@rejects, $default);
+    my $default = apply_patch($diff);
+    if ($default =~ s/^D,// || $default eq 'N') {
+       my $def = generate_new_patch($diff);
+       $default = 'U,N' if $default eq 'N' && $def eq 'E';
+       $default = 'N' if !$minor_updates && $default eq 'U,N';
+    }
+
+    PROMPT:
     while (1) {
-       print "\n----------- $diff ------------\n";
-       if ($apply) {
-           undef @new;
-           $default = 'N';
-           system "rsync -a --delete cvsdir/ workdir";
-           open(IN, "patch -d workdir -p0 --no-backup-if-mismatch -Z <../$diff |") or die $!;
-           while (<IN>) {
-               print $_;
-               chomp;
-               if (s/^patching file //) {
-                   push(@new, $_) unless -f "cvsdir/$_";
-               } elsif (s/.* saving rejects to file //) {
-                   push(@rejects, $_);
-               } elsif (/^Hunk #\d+ FAILED/) {
-                   $default = 'F';
-               } elsif (/^Hunk #\d+ succeeded/) {
-                   $default = 'E' unless $default eq 'F';
-               }
+       print "\n----------- $diff ------------\n",
+           "\nFix rejects, Diff create, Edit both diffs, Update patch,\n",
+           "Apply patch again, !(CMD), Build rsync, Next, Quit: [$default] ";
+       my $ans = <STDIN>;
+       chomp $ans;
+       $ans = $default if $ans eq '';
+       while ($ans =~ s/^\s*(!|\w)((?<!!)[^;,]*|[^;]*)[;,]?//) {
+           my $cmd = "\U$1\E";
+           if ($cmd eq '!') {
+               $cmd = $2 || $ENV{'SHELL'};
+               chdir('workdir') or die $!;
+               system $cmd;
+               chdir('..') or die $!;
+               $default = 'D';
+               next;
+           }
+           if ($cmd eq 'A') {
+               $default = apply_patch($diff);
+               next;
            }
-           close IN;
-           if ($default eq 'N') {
-               generate_new_patch($diff);
-               if (system("diff ../$diff new.patch >/dev/null") == 0) {
-                   print "\n(New patch is identical to old.)\n";
+           if ($cmd eq 'B') {
+               if (!-f 'workdir/Makefile') {
+                   open(IN, '../../Makefile') or die $!;
+                   open(OUT, '>workdir/Makefile') or die $!;
+                   print OUT "srcdir=.\n\n";
+                   while (<IN>) {
+                       last if /^gen:/;
+                   }
+                   print OUT $_;
+                   while (<IN>) {
+                       last if /^clean:/;
+                       print OUT $_;
+                   }
+                   close IN;
+                   close OUT;
+               }
+               my $need_autoconf;
+               my $conf_opts;
+               open(IN, "../$diff") or die $!;
+               while (<IN>) {
+                   if (!defined $conf_opts) {
+                       $conf_opts = '' if /^---/;
+                       if (m#^\s*\./configure( .+)#) {
+                           $conf_opts = $1;
+                       }
+                   }
+                   if (m#^--- orig/(configure\.in|/aclocal\.m4)#) {
+                       $need_autoconf = 1;
+                       last;
+                   }
                }
+               close IN;
+               chdir('workdir') or die $!;
+               system "autoconf; autoheader" if $need_autoconf;
+               system "make proto; ./configure $CONF_OPTS $conf_opts; make";
+               chdir('..') or die $!;
+               $default = '!make test';
+               next;
+           }
+           if ($cmd eq 'D') {
+               $default = generate_new_patch($diff);
+               next;
+           }
+           if ($cmd eq 'E') {
+               chdir('workdir') or die $!;
+               system "vim -d ../../$diff ../new.patch";
+               chdir('..') or die $!;
+               $default = 'U,A,D';
+               next;
+           }
+           if ($cmd eq 'F') {
+               chdir('workdir') or die $!;
+               system "vim @rejects";
+               chdir('..') or die $!;
+               $default = 'D,E';
+               next;
+           }
+           if ($cmd eq 'N') {
+               last PROMPT;
+           }
+           if ($cmd eq 'Q') {
+               exit;
+           }
+           if ($cmd eq 'U') {
+               system "cp -p new.patch ../$diff";
+               print "\nUpdated $diff from new.patch\n";
+               $default = 'A';
+               next;
            }
-           $apply = 0;
-       }
-       print "\nFix rejects, Edit both diffs, Update patch,\n",
-           "Apply patch again, Next, Quit: [$default] ";
-       my $ans = <STDIN>;
-       chomp $ans;
-       $ans = $default unless $ans =~ s/^(\w)/$1/;
-       if ($ans =~ /E/i) {
-           generate_new_patch($diff);
-           chdir('workdir') or die $!;
-           system "vim -d ../../$diff ../new.patch";
-           chdir('..') or die $!;
-           $default = 'U';
-       } elsif ($ans =~ /F/i) {
-           chdir('workdir') or die $!;
-           system "vim @rejects";
-           chdir('..') or die $!;
-           $default = 'E';
-       } elsif ($ans =~ /U/i) {
-           system "cp -p new.patch ../$diff";
-           print "\nUpdated $diff from new.patch\n";
-           $default = 'A';
-       } elsif ($ans =~ /A/i) {
-           $apply = 1;
-       } elsif ($ans =~ /N/i) {
-           last;
-       } elsif ($ans =~ /Q/i) {
-           exit;
        }
     }
 
@@ -133,6 +191,37 @@ foreach my $diff (@ARGV) {
 exit;
 
 
+sub apply_patch
+{
+    my($diff) = @_;
+
+    undef @new;
+    system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
+    print "\nApplying patch $diff...\n";
+    undef @rejects;
+    my($saw_failure, $saw_offset, $saw_fuzz);
+    open(IN, "patch -d workdir -p0 --no-backup-if-mismatch -Zf <../$diff |") or die $!;
+    while (<IN>) {
+       print $_;
+       chomp;
+       if (s/^patching file //) {
+           push(@new, $_) unless -f "cvsdir/$_";
+       } elsif (s/.* saving rejects to file //) {
+           push(@rejects, $_);
+       } elsif (/^Hunk #\d+ FAILED/) {
+           $saw_failure = 1;
+       } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz $interesting_fuzz)?/o) {
+           $saw_fuzz ||= defined $1;
+           $saw_offset = 1;
+       }
+    }
+    close IN;
+    return 'F,D,E' if $saw_failure;
+    return 'D,E' if $saw_fuzz && !$failures_only;
+    return 'D,U,N' if $saw_offset && !$failures_only;
+    'N';
+}
+
 sub generate_new_patch
 {
     my($diff) = @_;
@@ -152,7 +241,7 @@ sub generate_new_patch
        next if /^(diff -|Index: |Only in )/;
        s#^\Q--- cvsdir/\E#--- orig/#;
        s#^\Q+++ workdir/\E#+++ #;
-       s#(\.000000000)? \+0000$##;
+       s#(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)(\.\d\d\d\d\d\d\d\d\d)? \+0000$#$1#;
        print OUT $_;
     }
     close IN;
@@ -160,6 +249,13 @@ sub generate_new_patch
     foreach (@new) {
        unlink("cvsdir/$_");
     }
+    print "\nDiffing... ";
+    if (system("diff ../$diff new.patch >/dev/null") == 0) {
+       print "new patch is identical to old.\n";
+       return 'N';
+    }
+    print "New patch DIFFERS from old.\n";
+    'E';
 }
 
 sub restore_cvsdir
@@ -167,16 +263,27 @@ sub restore_cvsdir
     return unless $has_dependencies;
     $has_dependencies = 0;
 
+    chdir('cvsdir') or die $!;
     foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
        my $fn;
        ($fn = $_) =~ s/\.~1~$//;
        if ($fn eq $_) {
            unlink($_);
-       } elsif (-r $fn) {
-           rename($_,  $fn);
+       } elsif (-r $_) {
+           rename($_, $fn);
        } else {
            unlink($_);
            unlink($fn);
        }
     }
+    chdir('..') or die $!;
+}
+
+sub usage
+{
+    die <<EOT;
+Usage: $0 [OPTS] [DIFF-FILE...]
+ -n, --no-cvs          Don't update tmp/cvsdir at the start of the run
+ -u, --minor-updates   Suggest 'U' for even minor changes in the diff
+EOT
 }