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