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