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