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