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