Commit | Line | Data |
---|---|---|
3a849c85 WD |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
78b5ec5e WD |
4 | use Getopt::Long; |
5 | ||
3f8612ce | 6 | my @generated_files = qw( proto.h configure config.h.in rsync.1 rsyncd.conf.5 ); |
4cb494cd | 7 | |
adf2a1fc | 8 | my($no_cvs, $failures_only, $minor_updates, $prepare_source, $require_quit); |
e2de105f | 9 | my $auto_cmd = 'none'; |
78b5ec5e WD |
10 | |
11 | &Getopt::Long::Configure('bundling'); | |
12 | GetOptions( | |
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, |
adf2a1fc | 17 | 'require-quit|Q' => \$require_quit, |
e2de105f | 18 | 'auto-cmd|a=s' => sub { $auto_cmd .= "|$_[1]" }, |
78b5ec5e | 19 | ) or &usage; |
3a849c85 | 20 | |
e2de105f | 21 | $auto_cmd =~ s/none\|//; |
b2c06381 | 22 | $auto_cmd = qr/^($auto_cmd)$/i; |
228330c2 | 23 | |
c1d3a3a6 WD |
24 | my $interesting_fuzz = $minor_updates ? '\d' : '[2-9]'; |
25 | ||
3a849c85 WD |
26 | chdir('patches') if -d 'patches'; |
27 | ||
28 | if (!-f 'verify-patches') { | |
29 | die <<EOT; | |
30 | Please run this script from the root of the rsync dir or | |
31 | from inside the patches subdir. | |
32 | EOT | |
33 | } | |
34 | ||
a978ee02 | 35 | $| = 1; |
b1a4cc32 | 36 | my $CONF_OPTS = '-C'; |
3a849c85 | 37 | |
b1a4cc32 | 38 | my($has_dependencies, @new, @rejects); |
3a849c85 WD |
39 | |
40 | END { | |
41 | &restore_cvsdir; | |
3f8612ce | 42 | system "rsync -a --del cvsdir/ workdir/" if -d 'cvsdir'; |
3a849c85 WD |
43 | }; |
44 | ||
45 | my $root; | |
46 | open(IN, '../CVS/Root') or die $!; | |
47 | chomp($root = <IN>); | |
48 | close IN; | |
49 | ||
b1a4cc32 WD |
50 | mkdir('tmp', 0777) unless -d 'tmp'; |
51 | chdir('tmp') or die "Unable to chdir to 'tmp'"; | |
3a849c85 WD |
52 | |
53 | mkdir('workdir') unless -d 'workdir'; | |
54 | open(OUT, '>exclude') or die $!; | |
decfed48 | 55 | print OUT join("\n", 'CVS', @generated_files), "\n"; |
3a849c85 WD |
56 | close OUT; |
57 | ||
78b5ec5e WD |
58 | unless ($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 | ||
65 | DIFF: | |
66 | foreach 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 | } |
3f8612ce WD |
87 | if (!defined($conf_opts) && m#^\s*\./configure( .+)#) { |
88 | $conf_opts = $1; | |
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 | |
a978ee02 | 110 | PROMPT: |
3a849c85 | 111 | while (1) { |
b1a4cc32 WD |
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] "; | |
2a6374d5 | 115 | my $ans = $default; |
228330c2 | 116 | if ($default =~ /$auto_cmd/) { |
2a6374d5 WD |
117 | print $default, "\n"; |
118 | } else { | |
119 | my $input = <STDIN>; | |
120 | chomp $input; | |
121 | $ans = $input if $input ne ''; | |
122 | } | |
a978ee02 WD |
123 | while ($ans =~ s/^\s*(!|\w)((?<!!)[^;,]*|[^;]*)[;,]?//) { |
124 | my $cmd = "\U$1\E"; | |
125 | if ($cmd eq '!') { | |
126 | $cmd = $2 || $ENV{'SHELL'}; | |
127 | chdir('workdir') or die $!; | |
128 | system $cmd; | |
129 | chdir('..') or die $!; | |
7c88f9bf | 130 | $default = 'D'; |
a978ee02 WD |
131 | next; |
132 | } | |
133 | if ($cmd eq 'A') { | |
134 | $default = apply_patch($diff); | |
135 | next; | |
136 | } | |
137 | if ($cmd eq 'B') { | |
a978ee02 | 138 | chdir('workdir') or die $!; |
3f8612ce | 139 | system "./prepare-source && ./configure $CONF_OPTS $conf_opts && make"; |
a978ee02 WD |
140 | chdir('..') or die $!; |
141 | $default = '!make test'; | |
142 | next; | |
143 | } | |
144 | if ($cmd eq 'D') { | |
145 | $default = generate_new_patch($diff); | |
146 | next; | |
147 | } | |
148 | if ($cmd eq 'E') { | |
149 | chdir('workdir') or die $!; | |
150 | system "vim -d ../../$diff ../new.patch"; | |
151 | chdir('..') or die $!; | |
152 | $default = 'U,A,D'; | |
153 | next; | |
154 | } | |
155 | if ($cmd eq 'F') { | |
156 | chdir('workdir') or die $!; | |
157 | system "vim @rejects"; | |
158 | chdir('..') or die $!; | |
159 | $default = 'D,E'; | |
160 | next; | |
161 | } | |
162 | if ($cmd eq 'N') { | |
163 | last PROMPT; | |
164 | } | |
165 | if ($cmd eq 'Q') { | |
166 | exit; | |
167 | } | |
168 | if ($cmd eq 'U') { | |
169 | system "cp -p new.patch ../$diff"; | |
170 | print "\nUpdated $diff from new.patch\n"; | |
171 | $default = 'A'; | |
172 | next; | |
b1a4cc32 | 173 | } |
b1a4cc32 | 174 | } |
3a849c85 WD |
175 | } |
176 | ||
177 | &restore_cvsdir; | |
178 | } | |
179 | ||
adf2a1fc | 180 | while ($require_quit) { |
4fe467a2 | 181 | print "\nType 'Q' to quit this session of verify-patches: "; |
adf2a1fc WD |
182 | $_ = <STDIN>; |
183 | exit if /^q/i; | |
184 | } | |
185 | ||
3a849c85 WD |
186 | exit; |
187 | ||
188 | ||
b1a4cc32 WD |
189 | sub apply_patch |
190 | { | |
191 | my($diff) = @_; | |
192 | ||
193 | undef @new; | |
a978ee02 WD |
194 | system "rsync -a --delete --exclude='*~' cvsdir/ workdir/"; |
195 | print "\nApplying patch $diff...\n"; | |
fcbecd85 | 196 | undef @rejects; |
14191ec6 | 197 | my($saw_failure, $saw_offset, $saw_fuzz); |
3f8612ce | 198 | open(IN, "patch -d workdir -p1 --no-backup-if-mismatch <../$diff |") or die $!; |
b1a4cc32 WD |
199 | while (<IN>) { |
200 | print $_; | |
201 | chomp; | |
202 | if (s/^patching file //) { | |
203 | push(@new, $_) unless -f "cvsdir/$_"; | |
204 | } elsif (s/.* saving rejects to file //) { | |
205 | push(@rejects, $_); | |
206 | } elsif (/^Hunk #\d+ FAILED/) { | |
14191ec6 | 207 | $saw_failure = 1; |
c1d3a3a6 | 208 | } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz $interesting_fuzz)?/o) { |
14191ec6 WD |
209 | $saw_fuzz ||= defined $1; |
210 | $saw_offset = 1; | |
b1a4cc32 WD |
211 | } |
212 | } | |
213 | close IN; | |
14191ec6 | 214 | return 'F,D,E' if $saw_failure; |
c1d3a3a6 WD |
215 | return 'D,E' if $saw_fuzz && !$failures_only; |
216 | return 'D,U,N' if $saw_offset && !$failures_only; | |
14191ec6 | 217 | 'N'; |
b1a4cc32 WD |
218 | } |
219 | ||
decfed48 WD |
220 | sub filter_diff |
221 | { | |
222 | my($cmd) = @_; | |
223 | open(IN, '-|', $cmd) or die $!; | |
224 | while (<IN>) { | |
225 | next if /^(diff -|Index: |Only in )/; | |
226 | s#^\Q--- cvsdir/\E([^\t]+).*#--- old/$1#; | |
227 | s#^\Q+++ workdir/\E([^\t]+).*#+++ new/$1#; | |
228 | print OUT $_; | |
229 | } | |
230 | close IN; | |
231 | } | |
232 | ||
3a849c85 WD |
233 | sub generate_new_patch |
234 | { | |
235 | my($diff) = @_; | |
236 | ||
237 | foreach (@new) { | |
238 | system "touch -r workdir/$_ cvsdir/$_"; | |
239 | } | |
240 | open(IN, "../$diff") or die $!; | |
241 | open(OUT, '>new.patch') or die $!; | |
242 | while (<IN>) { | |
243 | last if /^--- /; | |
244 | print OUT $_; | |
245 | } | |
246 | close IN; | |
decfed48 WD |
247 | &filter_diff('diff --exclude-from=exclude -upr cvsdir workdir'); |
248 | if ($prepare_source) { | |
249 | # These are not included in the diff above so that patch will give | |
250 | # generated files a later timestamp than the source files. | |
251 | foreach my $fn (@generated_files) { | |
252 | &filter_diff("diff -up cvsdir/$fn workdir"); | |
253 | } | |
3a849c85 | 254 | } |
3a849c85 WD |
255 | close OUT; |
256 | foreach (@new) { | |
257 | unlink("cvsdir/$_"); | |
258 | } | |
a978ee02 | 259 | print "\nDiffing... "; |
b1a4cc32 | 260 | if (system("diff ../$diff new.patch >/dev/null") == 0) { |
a978ee02 | 261 | print "new patch is identical to old.\n"; |
b1a4cc32 WD |
262 | return 'N'; |
263 | } | |
a978ee02 | 264 | print "New patch DIFFERS from old.\n"; |
b1a4cc32 | 265 | 'E'; |
3a849c85 WD |
266 | } |
267 | ||
268 | sub restore_cvsdir | |
269 | { | |
270 | return unless $has_dependencies; | |
271 | $has_dependencies = 0; | |
272 | ||
64efc51e | 273 | chdir('cvsdir') or die $!; |
3a849c85 WD |
274 | foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) { |
275 | my $fn; | |
276 | ($fn = $_) =~ s/\.~1~$//; | |
277 | if ($fn eq $_) { | |
278 | unlink($_); | |
64efc51e WD |
279 | } elsif (-r $_) { |
280 | rename($_, $fn); | |
3a849c85 WD |
281 | } else { |
282 | unlink($_); | |
283 | unlink($fn); | |
284 | } | |
285 | } | |
64efc51e | 286 | chdir('..') or die $!; |
3a849c85 | 287 | } |
78b5ec5e WD |
288 | |
289 | sub usage | |
290 | { | |
291 | die <<EOT; | |
292 | Usage: $0 [OPTS] [DIFF-FILE...] | |
b2c06381 | 293 | -a, --auto-cmd=REGEX If default_cmd =~ /^(REGEX)\$/, enter it automatically |
4cb494cd | 294 | -f, --failures-only Suggest skipping patches that don't have failing hunks |
78b5ec5e | 295 | -n, --no-cvs Don't update tmp/cvsdir at the start of the run |
4cb494cd | 296 | -p, --prepare-source Run ./prepare-source and include generated files in diff |
2a6374d5 | 297 | -Q, --require-quit Don't auto-exit at the end of the list |
78b5ec5e WD |
298 | -u, --minor-updates Suggest 'U' for even minor changes in the diff |
299 | EOT | |
300 | } |