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