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