Added --auto-cmd=STR option.
[rsync/rsync-patches.git] / verify-patches
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Long;
5
6 my @generated_files = qw( proto.h configure config.h.in rsync.1 rsyncd.conf.5 );
7
8 my($no_cvs, $failures_only, $minor_updates, $prepare_source, $require_quit);
9 my $auto_cmd = 'never';
10
11 &Getopt::Long::Configure('bundling');
12 GetOptions(
13     'no-cvs|n' => \$no_cvs,
14     'failures-only|f' => \$failures_only,
15     'minor-updates|u' => \$minor_updates,
16     'prepare-source|p' => \$prepare_source,
17     'require-quit|Q' => \$require_quit,
18     'auto-cmd=s' => \$auto_cmd,
19 ) or &usage;
20
21 my $interesting_fuzz = $minor_updates ? '\d' : '[2-9]';
22
23 chdir('patches') if -d 'patches';
24
25 if (!-f 'verify-patches') {
26     die <<EOT;
27 Please run this script from the root of the rsync dir or
28 from inside the patches subdir.
29 EOT
30 }
31
32 $| = 1;
33 my $CONF_OPTS = '-C';
34
35 my($has_dependencies, @new, @rejects);
36
37 END {
38     &restore_cvsdir;
39     system "rsync -a --del cvsdir/ workdir/" if -d 'cvsdir';
40 };
41
42 my $root;
43 open(IN, '../CVS/Root') or die $!;
44 chomp($root = <IN>);
45 close IN;
46
47 mkdir('tmp', 0777) unless -d 'tmp';
48 chdir('tmp') or die "Unable to chdir to 'tmp'";
49
50 mkdir('workdir') unless -d 'workdir';
51 open(OUT, '>exclude') or die $!;
52 print OUT join("\n", 'CVS', @generated_files), "\n";
53 close OUT;
54
55 unless ($no_cvs) {
56     print "Using CVS to update the tmp/cvsdir copy of the source.\n";
57     system qq|cvs -qd "$root" co -P -d cvsdir rsync|;
58 }
59
60 @ARGV = glob('../*.diff') unless @ARGV;
61
62 DIFF:
63 foreach my $diff (@ARGV) {
64     next unless $diff =~ /\.diff$/;
65     next if $diff =~ /gzip-rsyncable[-_a-z]*\.diff$/;
66     $diff =~ s#^(patches|\.\.)/##;
67
68     my $conf_opts;
69     open(IN, "../$diff") or die $!;
70     while (<IN>) {
71         last if /^--- /;
72         if (/^Depends-On-Patch: (\S+.diff)$/) {
73             my $dep = $1;
74             $has_dependencies = 1;
75             print "\nApplying dependency patch $dep...\n";
76             if (system("patch -d cvsdir -p1 -b -Vt <../$dep") != 0) {
77                 print "Unable to cleanly apply dependency patch -- skipping $diff\n";
78                 system "rm -f cvsdir/*.rej cvsdir/*/*.rej";
79                 &restore_cvsdir;
80                 next DIFF;
81             }
82             sleep(1) if $prepare_source; # Ensure later diffs get later times.
83         }
84         if (!defined($conf_opts) && m#^\s*\./configure( .+)#) {
85             $conf_opts = $1;
86             $conf_opts =~ s/\s+\(.*?\)//;
87         }
88     }
89     close IN;
90     $conf_opts = '' unless defined $conf_opts;
91
92     my $default = apply_patch($diff);
93
94     if ($prepare_source) {
95         print "\nPreparing the source...\n";
96         chdir('workdir') or die $!;
97         system "./prepare-source";
98         chdir('..') or die $!;
99     }
100
101     if ($default =~ s/^D,// || $default eq 'N') {
102         my $def = generate_new_patch($diff);
103         $default = 'U,N' if $default eq 'N' && $def eq 'E';
104         $default = 'N' if !$minor_updates && $default eq 'U,N';
105     }
106
107     PROMPT:
108     while (1) {
109         print "\n----------- $diff ------------\n",
110             "\nFix rejects, Diff create, Edit both diffs, Update patch,\n",
111             "Apply patch again, !(CMD), Build rsync, Next, Quit: [$default] ";
112         my $ans = $default;
113         if ($default eq $auto_cmd) {
114             print $default, "\n";
115         } else {
116             my $input = <STDIN>;
117             chomp $input;
118             $ans = $input if $input ne '';
119         }
120         while ($ans =~ s/^\s*(!|\w)((?<!!)[^;,]*|[^;]*)[;,]?//) {
121             my $cmd = "\U$1\E";
122             if ($cmd eq '!') {
123                 $cmd = $2 || $ENV{'SHELL'};
124                 chdir('workdir') or die $!;
125                 system $cmd;
126                 chdir('..') or die $!;
127                 $default = 'D';
128                 next;
129             }
130             if ($cmd eq 'A') {
131                 $default = apply_patch($diff);
132                 next;
133             }
134             if ($cmd eq 'B') {
135                 chdir('workdir') or die $!;
136                 system "./prepare-source && ./configure $CONF_OPTS $conf_opts && make";
137                 chdir('..') or die $!;
138                 $default = '!make test';
139                 next;
140             }
141             if ($cmd eq 'D') {
142                 $default = generate_new_patch($diff);
143                 next;
144             }
145             if ($cmd eq 'E') {
146                 chdir('workdir') or die $!;
147                 system "vim -d ../../$diff ../new.patch";
148                 chdir('..') or die $!;
149                 $default = 'U,A,D';
150                 next;
151             }
152             if ($cmd eq 'F') {
153                 chdir('workdir') or die $!;
154                 system "vim @rejects";
155                 chdir('..') or die $!;
156                 $default = 'D,E';
157                 next;
158             }
159             if ($cmd eq 'N') {
160                 last PROMPT;
161             }
162             if ($cmd eq 'Q') {
163                 exit;
164             }
165             if ($cmd eq 'U') {
166                 system "cp -p new.patch ../$diff";
167                 print "\nUpdated $diff from new.patch\n";
168                 $default = 'A';
169                 next;
170             }
171         }
172     }
173
174     &restore_cvsdir;
175 }
176
177 while ($require_quit) {
178     print "\nType 'Q' to quit: ";
179     $_ = <STDIN>;
180     exit if /^q/i;
181 }
182
183 exit;
184
185
186 sub apply_patch
187 {
188     my($diff) = @_;
189
190     undef @new;
191     system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
192     print "\nApplying patch $diff...\n";
193     undef @rejects;
194     my($saw_failure, $saw_offset, $saw_fuzz);
195     open(IN, "patch -d workdir -p1 --no-backup-if-mismatch <../$diff |") or die $!;
196     while (<IN>) {
197         print $_;
198         chomp;
199         if (s/^patching file //) {
200             push(@new, $_) unless -f "cvsdir/$_";
201         } elsif (s/.* saving rejects to file //) {
202             push(@rejects, $_);
203         } elsif (/^Hunk #\d+ FAILED/) {
204             $saw_failure = 1;
205         } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz $interesting_fuzz)?/o) {
206             $saw_fuzz ||= defined $1;
207             $saw_offset = 1;
208         }
209     }
210     close IN;
211     return 'F,D,E' if $saw_failure;
212     return 'D,E' if $saw_fuzz && !$failures_only;
213     return 'D,U,N' if $saw_offset && !$failures_only;
214     'N';
215 }
216
217 sub filter_diff
218 {
219     my($cmd) = @_;
220     open(IN, '-|', $cmd) or die $!;
221     while (<IN>) {
222         next if /^(diff -|Index: |Only in )/;
223         s#^\Q--- cvsdir/\E([^\t]+).*#--- old/$1#;
224         s#^\Q+++ workdir/\E([^\t]+).*#+++ new/$1#;
225         print OUT $_;
226     }
227     close IN;
228 }
229
230 sub generate_new_patch
231 {
232     my($diff) = @_;
233
234     foreach (@new) {
235         system "touch -r workdir/$_ cvsdir/$_";
236     }
237     open(IN, "../$diff") or die $!;
238     open(OUT, '>new.patch') or die $!;
239     while (<IN>) {
240         last if /^--- /;
241         print OUT $_;
242     }
243     close IN;
244     &filter_diff('diff --exclude-from=exclude -upr cvsdir workdir');
245     if ($prepare_source) {
246         # These are not included in the diff above so that patch will give
247         # generated files a later timestamp than the source files.
248         foreach my $fn (@generated_files) {
249             &filter_diff("diff -up cvsdir/$fn workdir");
250         }
251     }
252     close OUT;
253     foreach (@new) {
254         unlink("cvsdir/$_");
255     }
256     print "\nDiffing... ";
257     if (system("diff ../$diff new.patch >/dev/null") == 0) {
258         print "new patch is identical to old.\n";
259         return 'N';
260     }
261     print "New patch DIFFERS from old.\n";
262     'E';
263 }
264
265 sub restore_cvsdir
266 {
267     return unless $has_dependencies;
268     $has_dependencies = 0;
269
270     chdir('cvsdir') or die $!;
271     foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
272         my $fn;
273         ($fn = $_) =~ s/\.~1~$//;
274         if ($fn eq $_) {
275             unlink($_);
276         } elsif (-r $_) {
277             rename($_, $fn);
278         } else {
279             unlink($_);
280             unlink($fn);
281         }
282     }
283     chdir('..') or die $!;
284 }
285
286 sub usage
287 {
288     die <<EOT;
289 Usage: $0 [OPTS] [DIFF-FILE...]
290      --auto-cmd=STR    If the suggested command is STR, enter it automatically
291  -f, --failures-only   Suggest skipping patches that don't have failing hunks
292  -n, --no-cvs          Don't update tmp/cvsdir at the start of the run
293  -p, --prepare-source  Run ./prepare-source and include generated files in diff
294  -Q, --require-quit    Don't auto-exit at the end of the list
295  -u, --minor-updates   Suggest 'U' for even minor changes in the diff
296 EOT
297 }