A few more improvements to the input-parsing code and a couple
[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 depenency patch -- skipping $diff\n";
65                 &restore_cvsdir;
66                 next DIFF;
67             }
68         }
69     }
70     close IN;
71
72     my $default = apply_patch($diff);
73     if ($default eq 'N') {
74         generate_new_patch($diff);
75     }
76
77     PROMPT:
78     while (1) {
79         print "\n----------- $diff ------------\n",
80             "\nFix rejects, Diff create, Edit both diffs, Update patch,\n",
81             "Apply patch again, !(CMD), Build rsync, Next, Quit: [$default] ";
82         my $ans = <STDIN>;
83         chomp $ans;
84         $ans = $default if $ans eq '';
85         while ($ans =~ s/^\s*(!|\w)((?<!!)[^;,]*|[^;]*)[;,]?//) {
86             my $cmd = "\U$1\E";
87             if ($cmd eq '!') {
88                 $cmd = $2 || $ENV{'SHELL'};
89                 chdir('workdir') or die $!;
90                 system $cmd;
91                 chdir('..') or die $!;
92                 $default = 'D,E';
93                 next;
94             }
95             if ($cmd eq 'A') {
96                 $default = apply_patch($diff);
97                 next;
98             }
99             if ($cmd eq 'B') {
100                 if (!-f 'workdir/Makefile') {
101                     open(IN, '../../Makefile') or die $!;
102                     open(OUT, '>workdir/Makefile') or die $!;
103                     print OUT "srcdir=.\n\n";
104                     while (<IN>) {
105                         last if /^gen:/;
106                     }
107                     print OUT $_;
108                     while (<IN>) {
109                         last if /^clean:/;
110                         print OUT $_;
111                     }
112                     close IN;
113                     close OUT;
114                 }
115                 chdir('workdir') or die $!;
116                 system "make gen; ./configure $CONF_OPTS; make";
117                 chdir('..') or die $!;
118                 $default = '!make test';
119                 next;
120             }
121             if ($cmd eq 'D') {
122                 $default = generate_new_patch($diff);
123                 next;
124             }
125             if ($cmd eq 'E') {
126                 chdir('workdir') or die $!;
127                 system "vim -d ../../$diff ../new.patch";
128                 chdir('..') or die $!;
129                 $default = 'U,A,D';
130                 next;
131             }
132             if ($cmd eq 'F') {
133                 chdir('workdir') or die $!;
134                 system "vim @rejects";
135                 chdir('..') or die $!;
136                 $default = 'D,E';
137                 next;
138             }
139             if ($cmd eq 'N') {
140                 last PROMPT;
141             }
142             if ($cmd eq 'Q') {
143                 exit;
144             }
145             if ($cmd eq 'U') {
146                 system "cp -p new.patch ../$diff";
147                 print "\nUpdated $diff from new.patch\n";
148                 $default = 'A';
149                 next;
150             }
151         }
152     }
153
154     &restore_cvsdir;
155 }
156
157 exit;
158
159
160 sub apply_patch
161 {
162     my($diff) = @_;
163
164     undef @new;
165     my $def = 'N';
166     system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
167     print "\nApplying patch $diff...\n";
168     open(IN, "patch -d workdir -p0 --no-backup-if-mismatch -Zf <../$diff |") or die $!;
169     while (<IN>) {
170         print $_;
171         chomp;
172         if (s/^patching file //) {
173             push(@new, $_) unless -f "cvsdir/$_";
174         } elsif (s/.* saving rejects to file //) {
175             push(@rejects, $_);
176         } elsif (/^Hunk #\d+ FAILED/) {
177             $def = 'F,D,E';
178         } elsif (/^Hunk #\d+ succeeded/) {
179             $def = 'E' unless $def =~ /F/;
180         }
181     }
182     close IN;
183     $def;
184 }
185
186 sub generate_new_patch
187 {
188     my($diff) = @_;
189
190     foreach (@new) {
191         system "touch -r workdir/$_ cvsdir/$_";
192     }
193     open(IN, "../$diff") or die $!;
194     open(OUT, '>new.patch') or die $!;
195     while (<IN>) {
196         last if /^--- /;
197         print OUT $_;
198     }
199     close IN;
200     open(IN, 'diff --exclude-from=exclude -upr cvsdir workdir |') or die $!;
201     while (<IN>) {
202         next if /^(diff -|Index: |Only in )/;
203         s#^\Q--- cvsdir/\E#--- orig/#;
204         s#^\Q+++ workdir/\E#+++ #;
205         s#(\.000000000)? \+0000$##;
206         print OUT $_;
207     }
208     close IN;
209     close OUT;
210     foreach (@new) {
211         unlink("cvsdir/$_");
212     }
213     print "\nDiffing... ";
214     if (system("diff ../$diff new.patch >/dev/null") == 0) {
215         print "new patch is identical to old.\n";
216         return 'N';
217     }
218     print "New patch DIFFERS from old.\n";
219     'E';
220 }
221
222 sub restore_cvsdir
223 {
224     return unless $has_dependencies;
225     $has_dependencies = 0;
226
227     foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
228         my $fn;
229         ($fn = $_) =~ s/\.~1~$//;
230         if ($fn eq $_) {
231             unlink($_);
232         } elsif (-r $fn) {
233             rename($_,  $fn);
234         } else {
235             unlink($_);
236             unlink($fn);
237         }
238     }
239 }