3 # Script to help with fallout from #849041.
6 # dgit-badcommit-fixup --test
7 # dgit-badcommit-fixup --real
9 # Update procedure, from server operator's point of view:
11 # 1. Test in an offline tree that this DTRT
13 # 2. Announce a transition time. Tell everyone that between
14 # the transition time and their next upload, they must
17 # 3. At the transition time, run this script in every repo.
19 # 4. Run the mirror script to push changes, if necessary.
30 foreach my $a (@ARGV) {
33 } elsif ($a eq '--real') {
40 die unless defined $real;
42 my $gcfpid = open2 \*GCFO, \*GCFI, 'git cat-file --batch' or die $!;
46 no warnings qw(recursion);
49 system @_ and die "@_ $! $?";
53 my $bare = `git rev-parse --is-bare-repository`;
58 my ($obj, $type) = @_;
59 print GCFI $obj, "\n" or die $!;
61 my ($gtype, $gsize) = $x =~ m/^\w+ (\w+) (\d+)\n/ or die "$obj ?";
62 $gtype eq $type or die "$obj $gtype != $type ?";
64 (read GCFO, $gdata, $gsize) == $gsize or die "$obj $!";
66 $x eq "\n" or die "$obj ($_) $!";
72 my ($data,$type) = @_;
73 my $gwopid = open2 \*GWO, \*GWI,
74 "git hash-object -w -t $type --stdin"
76 print GWI $data or die $!;
80 waitpid $gwopid,0 == $gwopid or die $!;
82 m/^(\w+)\n/ or die "$_ ?";
83 $count{"rewritten $type"}++;
89 sub rewrite_commit ($);
90 sub rewrite_commit ($) {
92 my $m = \ $memo{$obj};
93 return $$m if defined $$m;
94 my $olddata = getobj $obj, 'commit';
95 $olddata =~ m/(?<=\n)(?=\n)/ or die "$obj ?";
98 s{^(parent )(\w+)$}{ $1 . rewrite_commit($2) }gme;
99 $count{'fix overwrite'} += s{^commiter }{committer }gm;
100 if (!m{^author }m && !m{^committer }m) {
101 m{^parent (\w+)}m or die "$obj ?";
102 my $parent = getobj $1, 'commit';
103 $parent =~ m/^(?:.+\n)+(author .*\ncommitter .*\n)/;
104 m/\n$/ or die "$obj ?";
106 $count{'fix import'}++;
108 my $newdata = $_.$msg;
110 if ($newdata eq $olddata) {
113 #print STDERR "UNCHANGED $obj\n";
115 $newobj = hashobj $newdata, 'commit';
116 #print STDERR "REWRITTEN $obj $newobj\n";
124 sub filter_updates () {
125 @updates = grep { $_->[1] ne $_->[2] } @updates;
128 sub rewrite_tag ($) {
130 $_ = getobj $obj, 'tag';
131 m/^type (\w+)\n/m or die "$obj ?";
132 if ($1 ne 'commit') {
133 $count{"oddtags $1"}++;
136 m/^object (\w+)\n/m or die "$obj ?";
138 my $newref = rewrite_commit $oldref;
139 if ($oldref eq $newref) {
142 s/^(object )\w+$/ $1.$newref /me or die "$obj ($_) ?";
143 s/^-----BEGIN PGP SIGNATURE-----\n.*^-----END PGP SIGNATURE-----\n$//sm;
144 return hashobj $_, 'tag';
147 sub edit_rewrite_map ($) {
151 return $old unless @updates;
153 my $td = 'dgit-broken-fixup.tmp';
154 runcmd qw(rm -rf), $td;
155 mkdir $td, 0700 or die "$td $!";
157 runcmd qw(git init -q);
158 runcmd qw(git config gc.auto 0);
159 runcmd qw(rm -rf .git/objects);
160 symlink "../../objects", ".git/objects" or die $!;
165 runcmd qw(git checkout -q), $old;
166 open M, "map" or die $!;
168 m/^(\w+)(?:\s+(\w+))?$/ or die;
170 $count{rewrite_map_previous}++;
176 foreach my $oldc (keys %memo) {
177 my $newc = $memo{$oldc};
178 next if $oldc eq $newc;
181 foreach my $up (@updates) { # catches tags
182 $map{ $up->[1] } = $up->[2];
185 open M, ">", "map" or die $!;
187 $_, (defined $map{$_} ? " $map{$_}" : "")
193 runcmd qw(git add map);
196 runcmd qw(git commit -q),
197 qw(-m), 'dgit-badcommit-fixup',
201 my $new = `git rev-parse HEAD`;
205 chdir '..' or die $!;
206 runcmd qw(rm -rf), $td;
208 $count{rewrite_map_updated}++;
214 my $refs=`git for-each-ref`;
219 our $org_rewrite_map;
221 foreach my $rline (split /\n/, $refs) {
222 my ($obj, $type, $refname) =
223 $rline =~ m/^(\w+)\s+(\w+)\s+(\S.*)/
225 if ($refname eq 'refs/dgit-rewrite/map') {
226 $org_rewrite_map = $obj;
229 next if $refname =~ m{^refs/dgit-(?:badcommit|badfixuptest)/};
231 if ($type eq 'commit') {
232 $rewrite = rewrite_commit($obj);
233 } elsif ($type eq 'tag') {
234 $rewrite = rewrite_tag($obj);
236 warn "ref $refname refers to $type\n";
239 push @updates, [ $refname, $obj, $rewrite ];
242 if ($bare eq 'true') {
243 my $new_rewrite_map = edit_rewrite_map($org_rewrite_map);
244 push @updates, [ 'refs/dgit-rewrite/map',
245 ($org_rewrite_map // '0'x40),
246 ($new_rewrite_map // '0'x40),
253 print Dumper(\%count), "nothing to do\n";
257 #print Dumper(\@updates);
259 open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!;
261 for my $up (@updates) {
262 my ($ref, $old, $new, $nobackup) = @$up;
264 $otherref =~ s{^refs/}{};
266 print U <<END or die $! unless $nobackup;
267 create refs/dgit-badcommit/$otherref $old
269 print U <<END or die $!;
270 update $ref $new $old
273 print U <<END or die $!;
274 update refs/dgit-badfixuptest/$otherref $new
280 close U or die "$? $!";
283 print Dumper(\%count);
286 print "old values saved in refs/dgit-badcommit/\n" or die $!;
288 print "testing output saved in refs/dgit-badfixuptest/\n" or die $!;