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