Updated.
[rsync/rsync-patches.git] / verify-patches
CommitLineData
3a849c85
WD
1#!/usr/bin/perl
2
3use strict;
4
5chdir('patches') if -d 'patches';
6
7if (!-f 'verify-patches') {
8 die <<EOT;
9Please run this script from the root of the rsync dir or
10from inside the patches subdir.
11EOT
12}
13
a978ee02 14$| = 1;
b1a4cc32
WD
15$ENV{'TZ'} = 'UTC';
16my $CONF_OPTS = '-C';
3a849c85 17
b1a4cc32 18my($has_dependencies, @new, @rejects);
3a849c85
WD
19
20END {
21 &restore_cvsdir;
b1a4cc32 22 system "rsync -a --delete cvsdir/ workdir/" if -d 'cvsdir';
3a849c85
WD
23};
24
25my $root;
26open(IN, '../CVS/Root') or die $!;
27chomp($root = <IN>);
28close IN;
29
b1a4cc32
WD
30mkdir('tmp', 0777) unless -d 'tmp';
31chdir('tmp') or die "Unable to chdir to 'tmp'";
3a849c85
WD
32
33mkdir('workdir') unless -d 'workdir';
34open(OUT, '>exclude') or die $!;
35print OUT <<EOT;
b1a4cc32 36CVS
3a849c85
WD
37proto.h
38configure
39config.h.in
40rsync.1
41rsyncd.conf.5
42EOT
43close OUT;
44
b1a4cc32 45print "Using CVS to update the tmp/cvsdir copy of the source.\n";
3a849c85
WD
46system qq|cvs -d "$root" co -d cvsdir rsync|;
47
48@ARGV = glob('../*.diff') unless @ARGV;
49
50DIFF:
51foreach 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;
a978ee02 62 print "\nApplying dependency patch $dep...\n";
b1a4cc32 63 if (system("patch -d cvsdir -p0 -b -Vt -Zf <../$dep") != 0) {
3a849c85 64 print "Unable to cleanly apply depenency patch -- skipping $diff\n";
7c88f9bf 65 system "rm -f cvsdir/*.rej cvsdir/*/*.rej";
3a849c85
WD
66 &restore_cvsdir;
67 next DIFF;
68 }
69 }
70 }
71 close IN;
72
b1a4cc32 73 my $default = apply_patch($diff);
fcbecd85 74 if ($default =~ s/^D,// || $default eq 'N') {
a978ee02
WD
75 generate_new_patch($diff);
76 }
b1a4cc32 77
a978ee02 78 PROMPT:
3a849c85 79 while (1) {
b1a4cc32
WD
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] ";
3a849c85
WD
83 my $ans = <STDIN>;
84 chomp $ans;
b1a4cc32 85 $ans = $default if $ans eq '';
a978ee02
WD
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 $!;
7c88f9bf 93 $default = 'D';
a978ee02
WD
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 }
b1a4cc32 108 print OUT $_;
a978ee02
WD
109 while (<IN>) {
110 last if /^clean:/;
111 print OUT $_;
112 }
113 close IN;
114 close OUT;
b1a4cc32 115 }
a978ee02
WD
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;
b1a4cc32 151 }
b1a4cc32 152 }
3a849c85
WD
153 }
154
155 &restore_cvsdir;
156}
157
158exit;
159
160
b1a4cc32
WD
161sub apply_patch
162{
163 my($diff) = @_;
164
165 undef @new;
166 my $def = 'N';
a978ee02
WD
167 system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
168 print "\nApplying patch $diff...\n";
fcbecd85 169 undef @rejects;
b1a4cc32
WD
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/) {
a978ee02 179 $def = 'F,D,E';
b1a4cc32 180 } elsif (/^Hunk #\d+ succeeded/) {
fcbecd85 181 $def = 'D,E' unless $def =~ /F/;
b1a4cc32
WD
182 }
183 }
184 close IN;
b1a4cc32
WD
185 $def;
186}
187
3a849c85
WD
188sub 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 }
a978ee02 215 print "\nDiffing... ";
b1a4cc32 216 if (system("diff ../$diff new.patch >/dev/null") == 0) {
a978ee02 217 print "new patch is identical to old.\n";
b1a4cc32
WD
218 return 'N';
219 }
a978ee02 220 print "New patch DIFFERS from old.\n";
b1a4cc32 221 'E';
3a849c85
WD
222}
223
224sub 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}