Improved the default (suggested) commands depending on how a
[rsync/rsync-patches.git] / verify-patches
1 #!/usr/bin/perl
2
3 use strict;
4
5 chdir('patches') if -d 'patches';
6
7 if (!-f 'verify-patches') {
8     die <<EOT;
9 Please run this script from the root of the rsync dir or
10 from inside the patches subdir.
11 EOT
12 }
13
14 $| = 1;
15 $ENV{'TZ'} = 'UTC';
16 my $CONF_OPTS = '-C';
17
18 my($has_dependencies, @new, @rejects);
19
20 END {
21     &restore_cvsdir;
22     system "rsync -a --delete cvsdir/ workdir/" if -d 'cvsdir';
23 };
24
25 my $root;
26 open(IN, '../CVS/Root') or die $!;
27 chomp($root = <IN>);
28 close IN;
29
30 mkdir('tmp', 0777) unless -d 'tmp';
31 chdir('tmp') or die "Unable to chdir to 'tmp'";
32
33 mkdir('workdir') unless -d 'workdir';
34 open(OUT, '>exclude') or die $!;
35 print OUT <<EOT;
36 CVS
37 proto.h
38 configure
39 config.h.in
40 rsync.1
41 rsyncd.conf.5
42 EOT
43 close OUT;
44
45 print "Using CVS to update the tmp/cvsdir copy of the source.\n";
46 system qq|cvs -d "$root" co -d cvsdir rsync|;
47
48 @ARGV = glob('../*.diff') unless @ARGV;
49
50 DIFF:
51 foreach my $diff (@ARGV) {
52     next unless $diff =~ /\.diff$/;
53     next if $diff =~ /gzip-rsyncable\.diff$/;
54     $diff =~ s#^(patches|\.\.)/##;
55
56     open(IN, "../$diff") or die $!;
57     while (<IN>) {
58         last if /^--- /;
59         if (/^Depends-On-Patch: (\S+.diff)$/) {
60             my $dep = $1;
61             $has_dependencies = 1;
62             print "\nApplying dependency patch $dep...\n";
63             if (system("patch -d cvsdir -p0 -b -Vt -Zf <../$dep") != 0) {
64                 print "Unable to cleanly apply dependency patch -- skipping $diff\n";
65                 system "rm -f cvsdir/*.rej cvsdir/*/*.rej";
66                 &restore_cvsdir;
67                 next DIFF;
68             }
69         }
70     }
71     close IN;
72
73     my $default = apply_patch($diff);
74     if ($default =~ s/^D,// || $default eq 'N') {
75         my $def = generate_new_patch($diff);
76         $default = 'U,N' if $default eq 'N' && $def eq 'E';
77     }
78
79     PROMPT:
80     while (1) {
81         print "\n----------- $diff ------------\n",
82             "\nFix rejects, Diff create, Edit both diffs, Update patch,\n",
83             "Apply patch again, !(CMD), Build rsync, Next, Quit: [$default] ";
84         my $ans = <STDIN>;
85         chomp $ans;
86         $ans = $default if $ans eq '';
87         while ($ans =~ s/^\s*(!|\w)((?<!!)[^;,]*|[^;]*)[;,]?//) {
88             my $cmd = "\U$1\E";
89             if ($cmd eq '!') {
90                 $cmd = $2 || $ENV{'SHELL'};
91                 chdir('workdir') or die $!;
92                 system $cmd;
93                 chdir('..') or die $!;
94                 $default = 'D';
95                 next;
96             }
97             if ($cmd eq 'A') {
98                 $default = apply_patch($diff);
99                 next;
100             }
101             if ($cmd eq 'B') {
102                 if (!-f 'workdir/Makefile') {
103                     open(IN, '../../Makefile') or die $!;
104                     open(OUT, '>workdir/Makefile') or die $!;
105                     print OUT "srcdir=.\n\n";
106                     while (<IN>) {
107                         last if /^gen:/;
108                     }
109                     print OUT $_;
110                     while (<IN>) {
111                         last if /^clean:/;
112                         print OUT $_;
113                     }
114                     close IN;
115                     close OUT;
116                 }
117                 chdir('workdir') or die $!;
118                 system "make gen; ./configure $CONF_OPTS; make";
119                 chdir('..') or die $!;
120                 $default = '!make test';
121                 next;
122             }
123             if ($cmd eq 'D') {
124                 $default = generate_new_patch($diff);
125                 next;
126             }
127             if ($cmd eq 'E') {
128                 chdir('workdir') or die $!;
129                 system "vim -d ../../$diff ../new.patch";
130                 chdir('..') or die $!;
131                 $default = 'U,A,D';
132                 next;
133             }
134             if ($cmd eq 'F') {
135                 chdir('workdir') or die $!;
136                 system "vim @rejects";
137                 chdir('..') or die $!;
138                 $default = 'D,E';
139                 next;
140             }
141             if ($cmd eq 'N') {
142                 last PROMPT;
143             }
144             if ($cmd eq 'Q') {
145                 exit;
146             }
147             if ($cmd eq 'U') {
148                 system "cp -p new.patch ../$diff";
149                 print "\nUpdated $diff from new.patch\n";
150                 $default = 'A';
151                 next;
152             }
153         }
154     }
155
156     &restore_cvsdir;
157 }
158
159 exit;
160
161
162 sub apply_patch
163 {
164     my($diff) = @_;
165
166     undef @new;
167     system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
168     print "\nApplying patch $diff...\n";
169     undef @rejects;
170     my($saw_failure, $saw_offset, $saw_fuzz);
171     open(IN, "patch -d workdir -p0 --no-backup-if-mismatch -Zf <../$diff |") or die $!;
172     while (<IN>) {
173         print $_;
174         chomp;
175         if (s/^patching file //) {
176             push(@new, $_) unless -f "cvsdir/$_";
177         } elsif (s/.* saving rejects to file //) {
178             push(@rejects, $_);
179         } elsif (/^Hunk #\d+ FAILED/) {
180             $saw_failure = 1;
181         } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz )?/) {
182             $saw_fuzz ||= defined $1;
183             $saw_offset = 1;
184         }
185     }
186     close IN;
187     return 'F,D,E' if $saw_failure;
188     return 'D,E' if $saw_fuzz;
189     return 'D,U,N' if $saw_offset;
190     'N';
191 }
192
193 sub generate_new_patch
194 {
195     my($diff) = @_;
196
197     foreach (@new) {
198         system "touch -r workdir/$_ cvsdir/$_";
199     }
200     open(IN, "../$diff") or die $!;
201     open(OUT, '>new.patch') or die $!;
202     while (<IN>) {
203         last if /^--- /;
204         print OUT $_;
205     }
206     close IN;
207     open(IN, 'diff --exclude-from=exclude -upr cvsdir workdir |') or die $!;
208     while (<IN>) {
209         next if /^(diff -|Index: |Only in )/;
210         s#^\Q--- cvsdir/\E#--- orig/#;
211         s#^\Q+++ workdir/\E#+++ #;
212         s#(\.000000000)? \+0000$##;
213         print OUT $_;
214     }
215     close IN;
216     close OUT;
217     foreach (@new) {
218         unlink("cvsdir/$_");
219     }
220     print "\nDiffing... ";
221     if (system("diff ../$diff new.patch >/dev/null") == 0) {
222         print "new patch is identical to old.\n";
223         return 'N';
224     }
225     print "New patch DIFFERS from old.\n";
226     'E';
227 }
228
229 sub restore_cvsdir
230 {
231     return unless $has_dependencies;
232     $has_dependencies = 0;
233
234     foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
235         my $fn;
236         ($fn = $_) =~ s/\.~1~$//;
237         if ($fn eq $_) {
238             unlink($_);
239         } elsif (-r $fn) {
240             rename($_,  $fn);
241         } else {
242             unlink($_);
243             unlink($fn);
244         }
245     }
246 }