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