Added a variable to t_stub.c so that "make test" works.
[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                 my $need_autoconf;
118                 my $conf_opts;
119                 open(IN, "../$diff") or die $!;
120                 while (<IN>) {
121                     if (!defined $conf_opts) {
122                         $conf_opts = '' if /^---/;
123                         if (m#^\s*\./configure( .+)#) {
124                             $conf_opts = $1;
125                         }
126                     }
127                     if (m#^--- orig/(configure\.in|/aclocal\.m4)#) {
128                         $need_autoconf = 1;
129                         last;
130                     }
131                 }
132                 close IN;
133                 chdir('workdir') or die $!;
134                 system "autoconf; autoheader" if $need_autoconf;
135                 system "make proto; ./configure $CONF_OPTS $conf_opts; make";
136                 chdir('..') or die $!;
137                 $default = '!make test';
138                 next;
139             }
140             if ($cmd eq 'D') {
141                 $default = generate_new_patch($diff);
142                 next;
143             }
144             if ($cmd eq 'E') {
145                 chdir('workdir') or die $!;
146                 system "vim -d ../../$diff ../new.patch";
147                 chdir('..') or die $!;
148                 $default = 'U,A,D';
149                 next;
150             }
151             if ($cmd eq 'F') {
152                 chdir('workdir') or die $!;
153                 system "vim @rejects";
154                 chdir('..') or die $!;
155                 $default = 'D,E';
156                 next;
157             }
158             if ($cmd eq 'N') {
159                 last PROMPT;
160             }
161             if ($cmd eq 'Q') {
162                 exit;
163             }
164             if ($cmd eq 'U') {
165                 system "cp -p new.patch ../$diff";
166                 print "\nUpdated $diff from new.patch\n";
167                 $default = 'A';
168                 next;
169             }
170         }
171     }
172
173     &restore_cvsdir;
174 }
175
176 exit;
177
178
179 sub apply_patch
180 {
181     my($diff) = @_;
182
183     undef @new;
184     system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
185     print "\nApplying patch $diff...\n";
186     undef @rejects;
187     my($saw_failure, $saw_offset, $saw_fuzz);
188     open(IN, "patch -d workdir -p0 --no-backup-if-mismatch -Zf <../$diff |") or die $!;
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/) {
197             $saw_failure = 1;
198         } elsif (/^Hunk #\d+ succeeded at \d+( with fuzz )?/) {
199             $saw_fuzz ||= defined $1;
200             $saw_offset = 1;
201         }
202     }
203     close IN;
204     return 'F,D,E' if $saw_failure;
205     return 'D,E' if $saw_fuzz;
206     return 'D,U,N' if $saw_offset;
207     'N';
208 }
209
210 sub generate_new_patch
211 {
212     my($diff) = @_;
213
214     foreach (@new) {
215         system "touch -r workdir/$_ cvsdir/$_";
216     }
217     open(IN, "../$diff") or die $!;
218     open(OUT, '>new.patch') or die $!;
219     while (<IN>) {
220         last if /^--- /;
221         print OUT $_;
222     }
223     close IN;
224     open(IN, 'diff --exclude-from=exclude -upr cvsdir workdir |') or die $!;
225     while (<IN>) {
226         next if /^(diff -|Index: |Only in )/;
227         s#^\Q--- cvsdir/\E#--- orig/#;
228         s#^\Q+++ workdir/\E#+++ #;
229         s#(\.000000000)? \+0000$##;
230         print OUT $_;
231     }
232     close IN;
233     close OUT;
234     foreach (@new) {
235         unlink("cvsdir/$_");
236     }
237     print "\nDiffing... ";
238     if (system("diff ../$diff new.patch >/dev/null") == 0) {
239         print "new patch is identical to old.\n";
240         return 'N';
241     }
242     print "New patch DIFFERS from old.\n";
243     'E';
244 }
245
246 sub restore_cvsdir
247 {
248     return unless $has_dependencies;
249     $has_dependencies = 0;
250
251     foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
252         my $fn;
253         ($fn = $_) =~ s/\.~1~$//;
254         if ($fn eq $_) {
255             unlink($_);
256         } elsif (-r $fn) {
257             rename($_,  $fn);
258         } else {
259             unlink($_);
260             unlink($fn);
261         }
262     }
263 }