Fixed failing hunks.
[rsync/rsync-patches.git] / verify-patches
... / ...
CommitLineData
1#!/usr/bin/perl
2
3use strict;
4use Getopt::Long;
5
6my @generated_files = qw( proto.h configure config.h.in rsync.1 rsyncd.conf.5 );
7
8my($no_cvs, $failures_only, $minor_updates, $prepare_source);
9my @auto_cmds;
10
11&Getopt::Long::Configure('bundling');
12GetOptions(
13 'no-cvs|n' => \$no_cvs,
14 'failures-only|f' => \$failures_only,
15 'minor-updates|u' => \$minor_updates,
16 'prepare-source|p' => \$prepare_source,
17 'auto-cmd|a=s' => sub { push(@auto_cmds, $_[1]) },
18) or &usage;
19
20$" = '|';
21my $auto_regex = @auto_cmds ? qr/^(@auto_cmds)$/i : qr/^never$/;
22my $interesting_fuzz = $minor_updates ? '\d' : '[2-9]';
23$" = ' ';
24
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
34$ENV{'LC_COLLATE'} = 'C';
35$| = 1;
36my $CONF_OPTS = '--cache-file=../config.cache';
37
38my($has_dependencies, @new, @rejects);
39
40END {
41 &restore_cvsdir;
42 system "rsync -a --del cvsdir/ workdir/" if -d 'cvsdir';
43 my $pid = readlink('lock') || 0;
44 unlink('lock') if $pid == $$;
45};
46
47my $root;
48open(IN, '../CVS/Root') or die $!;
49chomp($root = <IN>);
50close IN;
51
52mkdir('tmp', 0777) unless -d 'tmp';
53chdir('tmp') or die "Unable to chdir to 'tmp'";
54symlink('..', 'patches') unless -d 'patches';
55
56symlink($$, 'lock') or die "Unable to create lock file: $!\n";
57
58mkdir('workdir') unless -d 'workdir';
59open(OUT, '>exclude') or die $!;
60print OUT join("\n", 'CVS', @generated_files), "\n";
61close OUT;
62
63unless ($no_cvs) {
64 print "Using CVS to update the tmp/cvsdir copy of the source.\n";
65 system qq|cvs -qd "$root" co -P -d cvsdir rsync|;
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 }
75}
76
77@ARGV = glob('patches/*.diff') unless @ARGV;
78
79DIFF:
80foreach my $diff (@ARGV) {
81 next unless $diff =~ /\.diff$/;
82 next if $diff =~ /gzip-rsyncable[-_a-z]*\.diff$/;
83 $diff =~ s#^(patches|\.\.)/##;
84
85 my $conf_opts;
86 open(IN, "patches/$diff") or die $!;
87 while (<IN>) {
88 last if /^--- /;
89 if (m#^\s+patch -p1 <patches/(\S+.diff)# && $1 ne $diff) {
90 my $dep = $1;
91 $has_dependencies = 1;
92 print "\nApplying dependency patch $dep...\n";
93 if (system("patch -d cvsdir -p1 -b -Vt <patches/$dep") != 0) {
94 print "Unable to cleanly apply dependency patch -- skipping $diff\n";
95 system "rm -f cvsdir/*.rej cvsdir/*/*.rej";
96 &restore_cvsdir;
97 next DIFF;
98 }
99 sleep(1); # Ensure later diffs get later times.
100 }
101 if (!defined($conf_opts) && s#^\s+\./configure\s+##) {
102 chomp($conf_opts = $_);
103 $conf_opts =~ s/\s*\(.*?\)//;
104 }
105 }
106 close IN;
107 $conf_opts = '' unless defined $conf_opts;
108
109 my $default = apply_patch($diff);
110
111 if ($prepare_source) {
112 print "\nPreparing the source...\n";
113 chdir('workdir') or die $!;
114 system "./prepare-source";
115 chdir('..') or die $!;
116 }
117
118 if ($default =~ s/^D,// || $default eq 'N') {
119 my $def = generate_new_patch($diff);
120 $default = 'U,N' if $default eq 'N' && $def eq 'E';
121 $default = 'N' if !$minor_updates && $default eq 'U,N';
122 }
123
124 my $first_time = 1;
125 PROMPT:
126 while (1) {
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] ";
130 my $ans = $default;
131 if ($first_time && $default =~ /$auto_regex/) {
132 print $default, "\n";
133 } else {
134 my $input = <STDIN>;
135 chomp $input;
136 if ($input =~ s/^(-a|--auto-cmd=?)\s*//) {
137 push(@auto_cmds, $input eq '' ? $default : $input);
138 $" = '|';
139 $auto_regex = qr/^(@auto_cmds)$/i;
140 $" = ' ';
141 next;
142 }
143 $ans = $input if $input ne '';
144 }
145 $first_time = 0;
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 $!;
153 $default = 'D';
154 next;
155 }
156 if ($cmd eq 'A') {
157 $default = apply_patch($diff);
158 next;
159 }
160 if ($cmd eq 'B') {
161 chdir('workdir') or die $!;
162 my $cmd = "./prepare-source && ./configure $CONF_OPTS $conf_opts && make";
163 print "Running: $cmd\n";
164 system $cmd;
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 $!;
175 system "vim -d ../patches/$diff ../new.patch";
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') {
194 system "cp -p new.patch patches/$diff";
195 print "\nUpdated $diff from new.patch\n";
196 $default = 'A';
197 next;
198 }
199 }
200 }
201
202 &restore_cvsdir;
203}
204
205exit;
206
207
208sub apply_patch
209{
210 my($diff) = @_;
211
212 undef @new;
213 system "rsync -a --del --exclude='*~' cvsdir/ workdir/";
214 print "\nApplying patch $diff...\n";
215 undef @rejects;
216 my($saw_offset, $saw_fuzz);
217 open(IN, "patch -d workdir -p1 --no-backup-if-mismatch <patches/$diff |") or die $!;
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, $_);
225 } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz $interesting_fuzz)?/o) {
226 $saw_fuzz ||= defined $1;
227 $saw_offset = 1;
228 }
229 }
230 close IN;
231 return 'F,D,E' if @rejects;
232 return 'D,E' if $saw_fuzz && !$failures_only;
233 return 'D,U,N' if $saw_offset && !$failures_only;
234 'N';
235}
236
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
250sub generate_new_patch
251{
252 my($diff) = @_;
253
254 foreach (@new) {
255 system "touch -r workdir/$_ cvsdir/$_";
256 }
257 open(IN, "patches/$diff") or die $!;
258 open(OUT, '>new.patch') or die $!;
259 while (<IN>) {
260 last if /^--- /;
261 print OUT $_;
262 }
263 close IN;
264 &filter_diff('diff --exclude-from=exclude -dupr cvsdir workdir');
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) {
269 &filter_diff("diff -dup cvsdir/$fn workdir");
270 }
271 }
272 close OUT;
273 foreach (@new) {
274 unlink("cvsdir/$_");
275 }
276 print "\nDiffing... ";
277 if (system("diff patches/$diff new.patch >/dev/null") == 0) {
278 print "new patch is identical to old.\n";
279 return 'N';
280 }
281 print "New patch DIFFERS from old.\n";
282 'E';
283}
284
285sub restore_cvsdir
286{
287 return unless $has_dependencies;
288 $has_dependencies = 0;
289
290 chdir('cvsdir') or die $!;
291 foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
292 my $fn;
293 ($fn = $_) =~ s/\.~1~$//;
294 if ($fn eq $_) {
295 unlink($_);
296 } elsif (-r $_) {
297 rename($_, $fn);
298 } else {
299 unlink($_);
300 unlink($fn);
301 }
302 }
303 chdir('..') or die $!;
304}
305
306sub usage
307{
308 die <<EOT;
309Usage: $0 [OPTS] [DIFF-FILE...]
310 -a, --auto-cmd=REGEX If default_cmd =~ /^(REGEX)\$/, enter it automatically
311 -f, --failures-only Suggest skipping patches that don't have failing hunks
312 -n, --no-cvs Don't update tmp/cvsdir at the start of the run
313 -p, --prepare-source Run ./prepare-source and include generated files in diff
314 -u, --minor-updates Suggest 'U' for even minor changes in the diff
315EOT
316}