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 | |
7ffdfa62 WD |
8 | my($no_cvs, $failures_only, $minor_updates, $prepare_source); |
9 | my @auto_cmds; | |
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, |
7ffdfa62 | 17 | 'auto-cmd|a=s' => sub { push(@auto_cmds, $_[1]) }, |
78b5ec5e | 18 | ) or &usage; |
3a849c85 | 19 | |
d25d871b | 20 | $" = '|'; |
7ffdfa62 | 21 | my $auto_regex = @auto_cmds ? qr/^(@auto_cmds)$/i : qr/^never$/; |
c1d3a3a6 | 22 | my $interesting_fuzz = $minor_updates ? '\d' : '[2-9]'; |
d25d871b | 23 | $" = ' '; |
c1d3a3a6 | 24 | |
3a849c85 WD |
25 | chdir('patches') if -d 'patches'; |
26 | ||
27 | if (!-f 'verify-patches') { | |
28 | die <<EOT; | |
29 | Please run this script from the root of the rsync dir or | |
30 | from inside the patches subdir. | |
31 | EOT | |
32 | } | |
33 | ||
a978ee02 | 34 | $| = 1; |
b1a4cc32 | 35 | my $CONF_OPTS = '-C'; |
3a849c85 | 36 | |
b1a4cc32 | 37 | my($has_dependencies, @new, @rejects); |
3a849c85 WD |
38 | |
39 | END { | |
40 | &restore_cvsdir; | |
3f8612ce | 41 | system "rsync -a --del cvsdir/ workdir/" if -d 'cvsdir'; |
3a849c85 WD |
42 | }; |
43 | ||
44 | my $root; | |
45 | open(IN, '../CVS/Root') or die $!; | |
46 | chomp($root = <IN>); | |
47 | close IN; | |
48 | ||
b1a4cc32 WD |
49 | mkdir('tmp', 0777) unless -d 'tmp'; |
50 | chdir('tmp') or die "Unable to chdir to 'tmp'"; | |
3a849c85 WD |
51 | |
52 | mkdir('workdir') unless -d 'workdir'; | |
53 | open(OUT, '>exclude') or die $!; | |
decfed48 | 54 | print OUT join("\n", 'CVS', @generated_files), "\n"; |
3a849c85 WD |
55 | close OUT; |
56 | ||
78b5ec5e WD |
57 | unless ($no_cvs) { |
58 | print "Using CVS to update the tmp/cvsdir copy of the source.\n"; | |
a77ba43c | 59 | system qq|cvs -qd "$root" co -P -d cvsdir rsync|; |
78b5ec5e | 60 | } |
3a849c85 WD |
61 | |
62 | @ARGV = glob('../*.diff') unless @ARGV; | |
63 | ||
64 | DIFF: | |
65 | foreach my $diff (@ARGV) { | |
66 | next unless $diff =~ /\.diff$/; | |
10fbfa25 | 67 | next if $diff =~ /gzip-rsyncable[-_a-z]*\.diff$/; |
3a849c85 WD |
68 | $diff =~ s#^(patches|\.\.)/##; |
69 | ||
3f8612ce | 70 | my $conf_opts; |
3a849c85 WD |
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; | |
a978ee02 | 77 | print "\nApplying dependency patch $dep...\n"; |
3f8612ce | 78 | if (system("patch -d cvsdir -p1 -b -Vt <../$dep") != 0) { |
14191ec6 | 79 | print "Unable to cleanly apply dependency patch -- skipping $diff\n"; |
7c88f9bf | 80 | system "rm -f cvsdir/*.rej cvsdir/*/*.rej"; |
3a849c85 WD |
81 | &restore_cvsdir; |
82 | next DIFF; | |
83 | } | |
decfed48 | 84 | sleep(1) if $prepare_source; # Ensure later diffs get later times. |
3a849c85 | 85 | } |
471c09e4 WD |
86 | if (!defined($conf_opts) && s#^\s+\./configure\s+##) { |
87 | chomp($conf_opts = $_); | |
f53a732c | 88 | $conf_opts =~ s/\s*\(.*?\)//; |
4cb494cd WD |
89 | } |
90 | } | |
91 | close IN; | |
3f8612ce | 92 | $conf_opts = '' unless defined $conf_opts; |
4cb494cd | 93 | |
b1a4cc32 | 94 | my $default = apply_patch($diff); |
4cb494cd | 95 | |
3f8612ce WD |
96 | if ($prepare_source) { |
97 | print "\nPreparing the source...\n"; | |
4cb494cd | 98 | chdir('workdir') or die $!; |
3f8612ce | 99 | system "./prepare-source"; |
4cb494cd WD |
100 | chdir('..') or die $!; |
101 | } | |
102 | ||
fcbecd85 | 103 | if ($default =~ s/^D,// || $default eq 'N') { |
14191ec6 WD |
104 | my $def = generate_new_patch($diff); |
105 | $default = 'U,N' if $default eq 'N' && $def eq 'E'; | |
78b5ec5e | 106 | $default = 'N' if !$minor_updates && $default eq 'U,N'; |
a978ee02 | 107 | } |
b1a4cc32 | 108 | |
7ffdfa62 | 109 | my $first_time = 1; |
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; |
7ffdfa62 | 116 | if ($first_time && $default =~ /$auto_regex/) { |
2a6374d5 WD |
117 | print $default, "\n"; |
118 | } else { | |
119 | my $input = <STDIN>; | |
120 | chomp $input; | |
7ffdfa62 WD |
121 | if ($input =~ s/^(-a|--auto-cmd=?)\s*//) { |
122 | push(@auto_cmds, $input eq '' ? $default : $input); | |
d25d871b | 123 | $" = '|'; |
7ffdfa62 | 124 | $auto_regex = qr/^(@auto_cmds)$/i; |
d25d871b | 125 | $" = ' '; |
7ffdfa62 WD |
126 | next; |
127 | } | |
2a6374d5 WD |
128 | $ans = $input if $input ne ''; |
129 | } | |
7ffdfa62 | 130 | $first_time = 0; |
a978ee02 WD |
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 $!; | |
7c88f9bf | 138 | $default = 'D'; |
a978ee02 WD |
139 | next; |
140 | } | |
141 | if ($cmd eq 'A') { | |
142 | $default = apply_patch($diff); | |
143 | next; | |
144 | } | |
145 | if ($cmd eq 'B') { | |
a978ee02 | 146 | chdir('workdir') or die $!; |
471c09e4 WD |
147 | my $cmd = "./prepare-source && ./configure $CONF_OPTS $conf_opts && make"; |
148 | print "Running: $cmd\n"; | |
149 | system $cmd; | |
a978ee02 WD |
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; | |
b1a4cc32 | 183 | } |
b1a4cc32 | 184 | } |
3a849c85 WD |
185 | } |
186 | ||
187 | &restore_cvsdir; | |
188 | } | |
189 | ||
190 | exit; | |
191 | ||
192 | ||
b1a4cc32 WD |
193 | sub apply_patch |
194 | { | |
195 | my($diff) = @_; | |
196 | ||
197 | undef @new; | |
a978ee02 WD |
198 | system "rsync -a --delete --exclude='*~' cvsdir/ workdir/"; |
199 | print "\nApplying patch $diff...\n"; | |
fcbecd85 | 200 | undef @rejects; |
263e3c8e | 201 | my($saw_offset, $saw_fuzz); |
3f8612ce | 202 | open(IN, "patch -d workdir -p1 --no-backup-if-mismatch <../$diff |") or die $!; |
b1a4cc32 WD |
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, $_); | |
c1d3a3a6 | 210 | } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz $interesting_fuzz)?/o) { |
14191ec6 WD |
211 | $saw_fuzz ||= defined $1; |
212 | $saw_offset = 1; | |
b1a4cc32 WD |
213 | } |
214 | } | |
215 | close IN; | |
263e3c8e | 216 | return 'F,D,E' if @rejects; |
c1d3a3a6 WD |
217 | return 'D,E' if $saw_fuzz && !$failures_only; |
218 | return 'D,U,N' if $saw_offset && !$failures_only; | |
14191ec6 | 219 | 'N'; |
b1a4cc32 WD |
220 | } |
221 | ||
decfed48 WD |
222 | sub filter_diff |
223 | { | |
224 | my($cmd) = @_; | |
225 | open(IN, '-|', $cmd) or die $!; | |
226 | while (<IN>) { | |
227 | next if /^(diff -|Index: |Only in )/; | |
228 | s#^\Q--- cvsdir/\E([^\t]+).*#--- old/$1#; | |
229 | s#^\Q+++ workdir/\E([^\t]+).*#+++ new/$1#; | |
230 | print OUT $_; | |
231 | } | |
232 | close IN; | |
233 | } | |
234 | ||
3a849c85 WD |
235 | sub generate_new_patch |
236 | { | |
237 | my($diff) = @_; | |
238 | ||
239 | foreach (@new) { | |
240 | system "touch -r workdir/$_ cvsdir/$_"; | |
241 | } | |
242 | open(IN, "../$diff") or die $!; | |
243 | open(OUT, '>new.patch') or die $!; | |
244 | while (<IN>) { | |
245 | last if /^--- /; | |
246 | print OUT $_; | |
247 | } | |
248 | close IN; | |
decfed48 WD |
249 | &filter_diff('diff --exclude-from=exclude -upr cvsdir workdir'); |
250 | if ($prepare_source) { | |
251 | # These are not included in the diff above so that patch will give | |
252 | # generated files a later timestamp than the source files. | |
253 | foreach my $fn (@generated_files) { | |
254 | &filter_diff("diff -up cvsdir/$fn workdir"); | |
255 | } | |
3a849c85 | 256 | } |
3a849c85 WD |
257 | close OUT; |
258 | foreach (@new) { | |
259 | unlink("cvsdir/$_"); | |
260 | } | |
a978ee02 | 261 | print "\nDiffing... "; |
b1a4cc32 | 262 | if (system("diff ../$diff new.patch >/dev/null") == 0) { |
a978ee02 | 263 | print "new patch is identical to old.\n"; |
b1a4cc32 WD |
264 | return 'N'; |
265 | } | |
a978ee02 | 266 | print "New patch DIFFERS from old.\n"; |
b1a4cc32 | 267 | 'E'; |
3a849c85 WD |
268 | } |
269 | ||
270 | sub restore_cvsdir | |
271 | { | |
272 | return unless $has_dependencies; | |
273 | $has_dependencies = 0; | |
274 | ||
64efc51e | 275 | chdir('cvsdir') or die $!; |
3a849c85 WD |
276 | foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) { |
277 | my $fn; | |
278 | ($fn = $_) =~ s/\.~1~$//; | |
279 | if ($fn eq $_) { | |
280 | unlink($_); | |
64efc51e WD |
281 | } elsif (-r $_) { |
282 | rename($_, $fn); | |
3a849c85 WD |
283 | } else { |
284 | unlink($_); | |
285 | unlink($fn); | |
286 | } | |
287 | } | |
64efc51e | 288 | chdir('..') or die $!; |
3a849c85 | 289 | } |
78b5ec5e WD |
290 | |
291 | sub usage | |
292 | { | |
293 | die <<EOT; | |
294 | Usage: $0 [OPTS] [DIFF-FILE...] | |
b2c06381 | 295 | -a, --auto-cmd=REGEX If default_cmd =~ /^(REGEX)\$/, enter it automatically |
4cb494cd | 296 | -f, --failures-only Suggest skipping patches that don't have failing hunks |
78b5ec5e | 297 | -n, --no-cvs Don't update tmp/cvsdir at the start of the run |
4cb494cd | 298 | -p, --prepare-source Run ./prepare-source and include generated files in diff |
78b5ec5e WD |
299 | -u, --minor-updates Suggest 'U' for even minor changes in the diff |
300 | EOT | |
301 | } |