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