No longer needed after batch-file rewrite.
[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);
7c88f9bf 74 if ($default =~ /[NE]/) {
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";
b1a4cc32
WD
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/) {
a978ee02 178 $def = 'F,D,E';
b1a4cc32 179 } elsif (/^Hunk #\d+ succeeded/) {
a978ee02 180 $def = 'E' unless $def =~ /F/;
b1a4cc32
WD
181 }
182 }
183 close IN;
b1a4cc32
WD
184 $def;
185}
186
3a849c85
WD
187sub 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 }
a978ee02 214 print "\nDiffing... ";
b1a4cc32 215 if (system("diff ../$diff new.patch >/dev/null") == 0) {
a978ee02 216 print "new patch is identical to old.\n";
b1a4cc32
WD
217 return 'N';
218 }
a978ee02 219 print "New patch DIFFERS from old.\n";
b1a4cc32 220 'E';
3a849c85
WD
221}
222
223sub 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}