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