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