A few more improvements to the input-parsing code and a couple
[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
WD
64 print "Unable to cleanly apply depenency patch -- skipping $diff\n";
65 &restore_cvsdir;
66 next DIFF;
67 }
68 }
69 }
70 close IN;
71
b1a4cc32 72 my $default = apply_patch($diff);
a978ee02
WD
73 if ($default eq 'N') {
74 generate_new_patch($diff);
75 }
b1a4cc32 76
a978ee02 77 PROMPT:
3a849c85 78 while (1) {
b1a4cc32
WD
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] ";
3a849c85
WD
82 my $ans = <STDIN>;
83 chomp $ans;
b1a4cc32 84 $ans = $default if $ans eq '';
a978ee02
WD
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 }
b1a4cc32 107 print OUT $_;
a978ee02
WD
108 while (<IN>) {
109 last if /^clean:/;
110 print OUT $_;
111 }
112 close IN;
113 close OUT;
b1a4cc32 114 }
a978ee02
WD
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;
b1a4cc32 150 }
b1a4cc32 151 }
3a849c85
WD
152 }
153
154 &restore_cvsdir;
155}
156
157exit;
158
159
b1a4cc32
WD
160sub apply_patch
161{
162 my($diff) = @_;
163
164 undef @new;
165 my $def = 'N';
a978ee02
WD
166 system "rsync -a --delete --exclude='*~' cvsdir/ workdir/";
167 print "\nApplying patch $diff...\n";
b1a4cc32
WD
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/) {
a978ee02 177 $def = 'F,D,E';
b1a4cc32 178 } elsif (/^Hunk #\d+ succeeded/) {
a978ee02 179 $def = 'E' unless $def =~ /F/;
b1a4cc32
WD
180 }
181 }
182 close IN;
b1a4cc32
WD
183 $def;
184}
185
3a849c85
WD
186sub 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 }
a978ee02 213 print "\nDiffing... ";
b1a4cc32 214 if (system("diff ../$diff new.patch >/dev/null") == 0) {
a978ee02 215 print "new patch is identical to old.\n";
b1a4cc32
WD
216 return 'N';
217 }
a978ee02 218 print "New patch DIFFERS from old.\n";
b1a4cc32 219 'E';
3a849c85
WD
220}
221
222sub 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}