Applied Matt's fix for a misplaced hunk.
[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";
999cb289 93 if (system("patch -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);
999cb289 217 open(IN, "patch -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, $_);
c1d3a3a6 225 } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz $interesting_fuzz)?/o) {
14191ec6
WD
226 $saw_fuzz ||= defined $1;
227 $saw_offset = 1;
b1a4cc32
WD
228 }
229 }
230 close IN;
263e3c8e 231 return 'F,D,E' if @rejects;
c1d3a3a6
WD
232 return 'D,E' if $saw_fuzz && !$failures_only;
233 return 'D,U,N' if $saw_offset && !$failures_only;
14191ec6 234 'N';
b1a4cc32
WD
235}
236
decfed48
WD
237sub filter_diff
238{
239 my($cmd) = @_;
240 open(IN, '-|', $cmd) or die $!;
241 while (<IN>) {
242 next if /^(diff -|Index: |Only in )/;
243 s#^\Q--- cvsdir/\E([^\t]+).*#--- old/$1#;
244 s#^\Q+++ workdir/\E([^\t]+).*#+++ new/$1#;
245 print OUT $_;
246 }
247 close IN;
248}
249
3a849c85
WD
250sub generate_new_patch
251{
252 my($diff) = @_;
253
254 foreach (@new) {
255 system "touch -r workdir/$_ cvsdir/$_";
256 }
999cb289 257 open(IN, "patches/$diff") or die $!;
3a849c85
WD
258 open(OUT, '>new.patch') or die $!;
259 while (<IN>) {
260 last if /^--- /;
261 print OUT $_;
262 }
263 close IN;
4959107f 264 &filter_diff('diff --exclude-from=exclude -dupr cvsdir workdir');
decfed48
WD
265 if ($prepare_source) {
266 # These are not included in the diff above so that patch will give
267 # generated files a later timestamp than the source files.
268 foreach my $fn (@generated_files) {
4959107f 269 &filter_diff("diff -dup cvsdir/$fn workdir");
decfed48 270 }
3a849c85 271 }
3a849c85
WD
272 close OUT;
273 foreach (@new) {
274 unlink("cvsdir/$_");
275 }
a978ee02 276 print "\nDiffing... ";
999cb289 277 if (system("diff patches/$diff new.patch >/dev/null") == 0) {
a978ee02 278 print "new patch is identical to old.\n";
b1a4cc32
WD
279 return 'N';
280 }
a978ee02 281 print "New patch DIFFERS from old.\n";
b1a4cc32 282 'E';
3a849c85
WD
283}
284
285sub restore_cvsdir
286{
287 return unless $has_dependencies;
288 $has_dependencies = 0;
289
64efc51e 290 chdir('cvsdir') or die $!;
3a849c85
WD
291 foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
292 my $fn;
293 ($fn = $_) =~ s/\.~1~$//;
294 if ($fn eq $_) {
295 unlink($_);
64efc51e
WD
296 } elsif (-r $_) {
297 rename($_, $fn);
3a849c85
WD
298 } else {
299 unlink($_);
300 unlink($fn);
301 }
302 }
64efc51e 303 chdir('..') or die $!;
3a849c85 304}
78b5ec5e
WD
305
306sub usage
307{
308 die <<EOT;
309Usage: $0 [OPTS] [DIFF-FILE...]
b2c06381 310 -a, --auto-cmd=REGEX If default_cmd =~ /^(REGEX)\$/, enter it automatically
4cb494cd 311 -f, --failures-only Suggest skipping patches that don't have failing hunks
78b5ec5e 312 -n, --no-cvs Don't update tmp/cvsdir at the start of the run
4cb494cd 313 -p, --prepare-source Run ./prepare-source and include generated files in diff
78b5ec5e
WD
314 -u, --minor-updates Suggest 'U' for even minor changes in the diff
315EOT
316}