Fix some fuzz in the --help text hunks.
[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 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 =~ s/^D,// || $default eq 'N') {
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     undef @rejects;
170     open(IN, "patch -d workdir -p0 --no-backup-if-mismatch -Zf <../$diff |") or die $!;
171     while (<IN>) {
172         print $_;
173         chomp;
174         if (s/^patching file //) {
175             push(@new, $_) unless -f "cvsdir/$_";
176         } elsif (s/.* saving rejects to file //) {
177             push(@rejects, $_);
178         } elsif (/^Hunk #\d+ FAILED/) {
179             $def = 'F,D,E';
180         } elsif (/^Hunk #\d+ succeeded/) {
181             $def = 'D,E' unless $def =~ /F/;
182         }
183     }
184     close IN;
185     $def;
186 }
187
188 sub generate_new_patch
189 {
190     my($diff) = @_;
191
192     foreach (@new) {
193         system "touch -r workdir/$_ cvsdir/$_";
194     }
195     open(IN, "../$diff") or die $!;
196     open(OUT, '>new.patch') or die $!;
197     while (<IN>) {
198         last if /^--- /;
199         print OUT $_;
200     }
201     close IN;
202     open(IN, 'diff --exclude-from=exclude -upr cvsdir workdir |') or die $!;
203     while (<IN>) {
204         next if /^(diff -|Index: |Only in )/;
205         s#^\Q--- cvsdir/\E#--- orig/#;
206         s#^\Q+++ workdir/\E#+++ #;
207         s#(\.000000000)? \+0000$##;
208         print OUT $_;
209     }
210     close IN;
211     close OUT;
212     foreach (@new) {
213         unlink("cvsdir/$_");
214     }
215     print "\nDiffing... ";
216     if (system("diff ../$diff new.patch >/dev/null") == 0) {
217         print "new patch is identical to old.\n";
218         return 'N';
219     }
220     print "New patch DIFFERS from old.\n";
221     'E';
222 }
223
224 sub restore_cvsdir
225 {
226     return unless $has_dependencies;
227     $has_dependencies = 0;
228
229     foreach (glob('*.~[1-9]~'), glob('*/*.~[1-9]~')) {
230         my $fn;
231         ($fn = $_) =~ s/\.~1~$//;
232         if ($fn eq $_) {
233             unlink($_);
234         } elsif (-r $fn) {
235             rename($_,  $fn);
236         } else {
237             unlink($_);
238             unlink($fn);
239         }
240     }
241 }