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