chiark / gitweb /
dgit: factor out build_with_binary_builder()
[dgit.git] / dgit-badcommit-fixup
1 #!/usr/bin/perl -w
2 #
3 # Script to help with fallout from #849041.
4 #
5 # usage:
6 #   dgit-badcommit-fixup --check
7 #   dgit-badcommit-fixup --test
8 #   dgit-badcommit-fixup --real
9
10 # Update procedure, from server operator's point of view:
11 #
12 # 1. Test in an offline tree that this DTRT
13 #
14 # 2. Announce a transition time.  Tell everyone that between
15 #    the transition time and their next upload, they must
16 #    run this script.
17 #
18 # 3. At the transition time, run this script in every repo.
19 #
20 # 4. Run the mirror script to push changes, if necessary.
21
22 END { $? = $Debian::Dgit::ExitStatus::desired // -1; };
23 use Debian::Dgit::ExitStatus;
24
25 use strict;
26
27 use POSIX;
28 use IPC::Open2;
29 use Data::Dumper;
30
31 our $our_version = 'UNRELEASED'; ###substituted###
32
33 my $real;
34
35 foreach my $a (@ARGV) {
36     if ($a eq '--test') {
37         $real = 0;
38     } elsif ($a eq '--real') {
39         $real = 1;
40     } elsif ($a eq '--check') {
41         $real = -1;
42     } else {
43         die "$a ?";
44     }
45 }
46
47 die unless defined $real;
48
49 my $gcfpid = open2 \*GCFO, \*GCFI, 'git cat-file --batch' or die $!;
50
51 our %count;
52
53 no warnings qw(recursion);
54
55 sub runcmd {
56     system @_ and die "@_ $! $?";
57 }
58
59 $!=0; $?=0;
60 my $bare = `git rev-parse --is-bare-repository`;
61 die "$? $!" if $?;
62 chomp $bare or die;
63
64 our @configs;
65 foreach my $k (qw(core.sharedRepository)) {
66     $?=0; $!=0; my $v = `set -x; git config --local $k`;
67     if (defined $v && $?==0 && chomp $v) {
68         push @configs, [ $k, $v ];
69     } elsif (defined $v && $?==256 && $v eq '') {
70     } else {
71         die "git-config --local $k => $v $? $! ?";
72     }
73 }
74
75 sub getobj ($$) {
76     my ($obj, $type) = @_;
77     print GCFI $obj, "\n" or die $!;
78     my $x = <GCFO>;
79     my ($gtype, $gsize) = $x =~ m/^\w+ (\w+) (\d+)\n/ or die "$obj ?";
80     $gtype eq $type or die "$obj $gtype != $type ?";
81     my $gdata;
82     (read GCFO, $gdata, $gsize) == $gsize or die "$obj $!";
83     $x = <GCFO>;
84     $x eq "\n" or die "$obj ($_) $!";
85     $count{inspected}++;
86     return $gdata;
87 }
88
89 sub hashobj ($$) {
90     my ($data,$type) = @_;
91     my $gwopid = open2 \*GWO, \*GWI,
92         "git hash-object -w -t $type --stdin"
93         or die $!;
94     print GWI $data or die $!;
95     close GWI or die $!;
96     $_ = <GWO>;
97     close GWO or die $!;
98     waitpid $gwopid,0 == $gwopid or die $!;
99     die $? if $?;
100     m/^(\w+)\n/ or die "$_ ?";
101     $count{"rewritten $type"}++;
102     return $1;
103 }
104
105 our %memo;
106
107 sub rewrite_commit ($);
108 sub rewrite_commit ($) {
109     my ($obj) = @_;
110     my $m = \ $memo{$obj};
111     return $$m if defined $$m;
112     my $olddata = getobj $obj, 'commit';
113     $olddata =~ m/(?<=\n)(?=\n)/ or die "$obj ?";
114     my $msg = $';
115     local $_ = $`;
116     s{^(parent )(\w+)$}{ $1 . rewrite_commit($2) }gme;
117     $count{'fix overwrite'} += s{^commiter }{committer }gm;
118     if (!m{^author }m && !m{^committer }m) {
119         m{^parent (\w+)}m or die "$obj ?";
120         my $parent = getobj $1, 'commit';
121         $parent =~ m/^(?:.+\n)+(author .*\ncommitter .*\n)/;
122         m/\n$/ or die "$obj ?";
123         $_ .= $1;
124         $count{'fix import'}++;
125     }
126     my $newdata = $_.$msg;
127     my $newobj;
128     if ($newdata eq $olddata) {
129         $newobj = $obj;
130         $count{unchanged}++;
131 #print STDERR "UNCHANGED $obj\n";
132     } else {
133         $newobj = hashobj $newdata, 'commit';
134 #print STDERR "REWRITTEN $obj $newobj\n";
135     }
136     $$m= $newobj;
137     return $newobj;
138 }
139
140 our @updates;
141
142 sub filter_updates () {
143     @updates = grep { $_->[1] ne $_->[2] } @updates;
144 }
145
146 sub rewrite_tag ($) {
147     my ($obj) = @_;
148     $_ = getobj $obj, 'tag';
149     m/^type (\w+)\n/m or die "$obj ?";
150     if ($1 ne 'commit') {
151         $count{"oddtags $1"}++;
152         return $obj;
153     }
154     m/^object (\w+)\n/m or die "$obj ?";
155     my $oldref = $1;
156     my $newref = rewrite_commit $oldref;
157     if ($oldref eq $newref) {
158         return $obj;
159     }
160     s/^(object )\w+$/ $1.$newref /me or die "$obj ($_) ?";
161     s/^-----BEGIN PGP SIGNATURE-----\n.*^-----END PGP SIGNATURE-----\n$//sm;
162     return hashobj $_, 'tag';
163 }
164
165 sub edit_rewrite_map ($) {
166     my ($old) = @_;
167
168     filter_updates();
169     return $old unless @updates;
170
171     my $td = 'dgit-broken-fixup.tmp';
172     runcmd qw(rm -rf), $td;
173     mkdir $td, 0700 or die "$td $!";
174     chdir $td or die $!;
175     runcmd qw(git init -q);
176     runcmd qw(git config gc.auto 0);
177     runcmd qw(rm -rf .git/objects);
178     symlink "../../objects", ".git/objects" or die $!;
179     foreach my $c (@configs) {
180         runcmd qw(git config), $c->[0], $c->[1];
181     }
182
183     my %map;
184
185     if ($old) {
186         runcmd qw(git checkout -q), $old;
187         open M, "map" or die $!;
188         while (<M>) {
189             m/^(\w+)(?:\s+(\w+))?$/ or die;
190             $map{$1} = $2;
191             $count{rewrite_map_previous}++;
192         }
193         M->error and die $!;
194         close M or die $!;
195     }
196
197     foreach my $oldc (keys %memo) {
198         my $newc = $memo{$oldc};
199         next if $oldc eq $newc;
200         $map{$oldc} = $newc;
201     }
202     foreach my $up (@updates) { # catches tags
203         $map{ $up->[1] } = $up->[2];
204     }
205
206     open M, ">", "map" or die $!;
207     printf M "%s%s\n",
208         $_, (defined $map{$_} ? " $map{$_}" : "")
209         or die $!
210         foreach keys %map;
211     close M or die $!;
212
213     if (!$old) {
214         runcmd qw(git add map);
215     }
216
217     runcmd qw(git commit -q), qw(-m), <<END, qw(map);
218 dgit-badcommit-fixup
219
220 [dgit-badcommit-fixup $our_version]
221 END
222
223     $!=0; $?=0;
224     my $new = `git rev-parse HEAD`;
225     die "$? $!" if $?;
226     chomp $new or die;
227
228     chdir '..' or die $!;
229     runcmd qw(rm -rf), $td;
230
231     $count{rewrite_map_updated}++;
232
233     return $new;
234 }
235
236 $!=0; $?=0;
237 my $refs=`git for-each-ref`;
238 die "$? $!" if $?;
239
240 chomp $refs;
241
242 our $org_rewrite_map;
243
244 foreach my $rline (split /\n/, $refs) {
245     my ($obj, $type, $refname) = 
246         $rline =~ m/^(\w+)\s+(\w+)\s+(\S.*)/
247         or die "$_ ?";
248     if ($refname eq 'refs/dgit-rewrite/map') {
249         $org_rewrite_map = $obj;
250         next;
251     }
252     next if $refname =~ m{^refs/dgit-(?:badcommit|badfixuptest)/};
253
254     $!=0; $?=0;
255     system qw(sh -ec),
256         'exec >/dev/null git symbolic-ref -q "$1"', qw(x),
257         $refname;
258     if ($?==0) {
259         $count{symrefs_ignored}++;
260         next;
261     }
262     die "$? $!" unless $?==256;
263
264     my $rewrite;
265     if ($type eq 'commit') {
266         $rewrite = rewrite_commit($obj);
267     } elsif ($type eq 'tag') {
268         $rewrite = rewrite_tag($obj);
269     } else {
270         warn "ref $refname refers to $type\n";
271         next;
272     }
273     push @updates, [ $refname, $obj, $rewrite ];
274 }
275
276 if ($bare eq 'true') {
277     my $new_rewrite_map = edit_rewrite_map($org_rewrite_map);
278     push @updates, [ 'refs/dgit-rewrite/map',
279                      ($org_rewrite_map // '0'x40),
280                      ($new_rewrite_map // '0'x40),
281                      1 ];
282 }
283
284 filter_updates();
285
286 if (!@updates) {
287     print Dumper(\%count), "all is well - nothing to do\n";
288     finish 0;
289 }
290
291 #print Dumper(\@updates);
292
293 open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!
294     if $real >= 0;
295
296 for my $up (@updates) {
297     my ($ref, $old, $new, $nobackup) = @$up;
298     my $otherref = $ref;
299     $otherref =~ s{^refs/}{};
300     if ($real > 0) {
301         print U <<END or die $! unless $nobackup;
302 create refs/dgit-badcommit/$otherref $old
303 END
304         print U <<END or die $!;
305 update $ref $new $old
306 END
307     } elsif ($real==0) {
308         print U <<END or die $!;
309 update refs/dgit-badfixuptest/$otherref $new
310 END
311     } else {
312         print "found trouble in history of $ref\n" or die $!;
313     }
314 }
315
316 if ($real >= 0) {
317     $?=0; $!=0;
318     close U or die "$? $!";
319     die $? if $?;
320 }
321
322 print Dumper(\%count);
323
324 if ($real >= 0) {
325     print "old values saved in refs/dgit-badcommit/\n" or die $!;
326 } elsif ($real == 0) {
327     print "testing output saved in refs/dgit-badfixuptest/\n" or die $!;
328 } else {
329     print STDERR "found work to do, exiting status 2\n";
330     finish 2;
331 }
332
333 finish 0;