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