A patch from Antti Tapaninen.
[rsync/rsync-patches.git] / verify-patches
index e56d1b0..807c584 100755 (executable)
@@ -6,6 +6,7 @@ use Getopt::Long;
 my @generated_files = qw( proto.h configure config.h.in rsync.1 rsyncd.conf.5 );
 
 my($no_cvs, $failures_only, $minor_updates, $prepare_source);
+my @auto_cmds;
 
 &Getopt::Long::Configure('bundling');
 GetOptions(
@@ -13,9 +14,13 @@ GetOptions(
     'failures-only|f' => \$failures_only,
     'minor-updates|u' => \$minor_updates,
     'prepare-source|p' => \$prepare_source,
+    'auto-cmd|a=s' => sub { push(@auto_cmds, $_[1]) },
 ) or &usage;
 
+$" = '|';
+my $auto_regex = @auto_cmds ? qr/^(@auto_cmds)$/i : qr/^never$/;
 my $interesting_fuzz = $minor_updates ? '\d' : '[2-9]';
+$" = ' ';
 
 chdir('patches') if -d 'patches';
 
@@ -26,6 +31,7 @@ from inside the patches subdir.
 EOT
 }
 
+$ENV{'LC_COLLATE'} = 'C';
 $| = 1;
 my $CONF_OPTS = '-C';
 
@@ -46,10 +52,7 @@ chdir('tmp') or die "Unable to chdir to 'tmp'";
 
 mkdir('workdir') unless -d 'workdir';
 open(OUT, '>exclude') or die $!;
-print OUT "CVS\n";
-if (!$prepare_source) {
-    print OUT join("\n", @generated_files), "\n";
-}
+print OUT join("\n", 'CVS', @generated_files), "\n";
 close OUT;
 
 unless ($no_cvs) {
@@ -79,10 +82,11 @@ foreach my $diff (@ARGV) {
                &restore_cvsdir;
                next DIFF;
            }
+           sleep(1) if $prepare_source; # Ensure later diffs get later times.
        }
-       if (!defined($conf_opts) && m#^\s*\./configure( .+)#) {
-           $conf_opts = $1;
-           $conf_opts =~ s/\s+\(.*?\)//;
+       if (!defined($conf_opts) && s#^\s+\./configure\s+##) {
+           chomp($conf_opts = $_);
+           $conf_opts =~ s/\s*\(.*?\)//;
        }
     }
     close IN;
@@ -103,14 +107,28 @@ foreach my $diff (@ARGV) {
        $default = 'N' if !$minor_updates && $default eq 'U,N';
     }
 
+    my $first_time = 1;
     PROMPT:
     while (1) {
        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 '';
+       my $ans = $default;
+       if ($first_time && $default =~ /$auto_regex/) {
+           print $default, "\n";
+       } else {
+           my $input = <STDIN>;
+           chomp $input;
+           if ($input =~ s/^(-a|--auto-cmd=?)\s*//) {
+               push(@auto_cmds, $input eq '' ? $default : $input);
+               $" = '|';
+               $auto_regex = qr/^(@auto_cmds)$/i;
+               $" = ' ';
+               next;
+           }
+           $ans = $input if $input ne '';
+       }
+       $first_time = 0;
        while ($ans =~ s/^\s*(!|\w)((?<!!)[^;,]*|[^;]*)[;,]?//) {
            my $cmd = "\U$1\E";
            if ($cmd eq '!') {
@@ -127,7 +145,9 @@ foreach my $diff (@ARGV) {
            }
            if ($cmd eq 'B') {
                chdir('workdir') or die $!;
-               system "./prepare-source && ./configure $CONF_OPTS $conf_opts && make";
+               my $cmd = "./prepare-source && ./configure $CONF_OPTS $conf_opts && make";
+               print "Running: $cmd\n";
+               system $cmd;
                chdir('..') or die $!;
                $default = '!make test';
                next;
@@ -179,7 +199,7 @@ sub apply_patch
     system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
     print "\nApplying patch $diff...\n";
     undef @rejects;
-    my($saw_failure, $saw_offset, $saw_fuzz);
+    my($saw_offset, $saw_fuzz);
     open(IN, "patch -d workdir -p1 --no-backup-if-mismatch <../$diff |") or die $!;
     while (<IN>) {
        print $_;
@@ -188,20 +208,31 @@ sub apply_patch
            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 'F,D,E' if @rejects;
     return 'D,E' if $saw_fuzz && !$failures_only;
     return 'D,U,N' if $saw_offset && !$failures_only;
     'N';
 }
 
+sub filter_diff
+{
+    my($cmd) = @_;
+    open(IN, '-|', $cmd) or die $!;
+    while (<IN>) {
+       next if /^(diff -|Index: |Only in )/;
+       s#^\Q--- cvsdir/\E([^\t]+).*#--- old/$1#;
+       s#^\Q+++ workdir/\E([^\t]+).*#+++ new/$1#;
+       print OUT $_;
+    }
+    close IN;
+}
+
 sub generate_new_patch
 {
     my($diff) = @_;
@@ -216,14 +247,14 @@ sub generate_new_patch
        print OUT $_;
     }
     close IN;
-    open(IN, 'diff --exclude-from=exclude -upr cvsdir workdir |') or die $!;
-    while (<IN>) {
-       next if /^(diff -|Index: |Only in )/;
-       s#^\Q--- cvsdir/\E([^\t]+).*#--- old/$1#;
-       s#^\Q+++ workdir/\E([^\t]+).*#+++ new/$1#;
-       print OUT $_;
+    &filter_diff('diff --exclude-from=exclude -upr cvsdir workdir');
+    if ($prepare_source) {
+       # These are not included in the diff above so that patch will give
+       # generated files a later timestamp than the source files.
+       foreach my $fn (@generated_files) {
+           &filter_diff("diff -up cvsdir/$fn workdir");
+       }
     }
-    close IN;
     close OUT;
     foreach (@new) {
        unlink("cvsdir/$_");
@@ -262,6 +293,7 @@ sub usage
 {
     die <<EOT;
 Usage: $0 [OPTS] [DIFF-FILE...]
+ -a, --auto-cmd=REGEX  If default_cmd =~ /^(REGEX)\$/, enter it automatically
  -f, --failures-only   Suggest skipping patches that don't have failing hunks
  -n, --no-cvs          Don't update tmp/cvsdir at the start of the run
  -p, --prepare-source  Run ./prepare-source and include generated files in diff