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