3 # Script to help with fallout from #849041.
6 # dgit-badcommit-fixup --check
7 # dgit-badcommit-fixup --test
8 # dgit-badcommit-fixup --real
10 # Update procedure, from server operator's point of view:
12 # 1. Test in an offline tree that this DTRT
14 # 2. Announce a transition time. Tell everyone that between
15 # the transition time and their next upload, they must
18 # 3. At the transition time, run this script in every repo.
20 # 4. Run the mirror script to push changes, if necessary.
29 our $our_version = 'UNRELEASED'; ###substituted###
33 foreach my $a (@ARGV) {
36 } elsif ($a eq '--real') {
38 } elsif ($a eq '--check') {
45 die unless defined $real;
47 my $gcfpid = open2 \*GCFO, \*GCFI, 'git cat-file --batch' or die $!;
51 no warnings qw(recursion);
54 system @_ and die "@_ $! $?";
58 my $bare = `git rev-parse --is-bare-repository`;
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 '') {
69 die "git-config --local $k => $v $? $! ?";
74 my ($obj, $type) = @_;
75 print GCFI $obj, "\n" or die $!;
77 my ($gtype, $gsize) = $x =~ m/^\w+ (\w+) (\d+)\n/ or die "$obj ?";
78 $gtype eq $type or die "$obj $gtype != $type ?";
80 (read GCFO, $gdata, $gsize) == $gsize or die "$obj $!";
82 $x eq "\n" or die "$obj ($_) $!";
88 my ($data,$type) = @_;
89 my $gwopid = open2 \*GWO, \*GWI,
90 "git hash-object -w -t $type --stdin"
92 print GWI $data or die $!;
96 waitpid $gwopid,0 == $gwopid or die $!;
98 m/^(\w+)\n/ or die "$_ ?";
99 $count{"rewritten $type"}++;
105 sub rewrite_commit ($);
106 sub rewrite_commit ($) {
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 ?";
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 ?";
122 $count{'fix import'}++;
124 my $newdata = $_.$msg;
126 if ($newdata eq $olddata) {
129 #print STDERR "UNCHANGED $obj\n";
131 $newobj = hashobj $newdata, 'commit';
132 #print STDERR "REWRITTEN $obj $newobj\n";
140 sub filter_updates () {
141 @updates = grep { $_->[1] ne $_->[2] } @updates;
144 sub rewrite_tag ($) {
146 $_ = getobj $obj, 'tag';
147 m/^type (\w+)\n/m or die "$obj ?";
148 if ($1 ne 'commit') {
149 $count{"oddtags $1"}++;
152 m/^object (\w+)\n/m or die "$obj ?";
154 my $newref = rewrite_commit $oldref;
155 if ($oldref eq $newref) {
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';
163 sub edit_rewrite_map ($) {
167 return $old unless @updates;
169 my $td = 'dgit-broken-fixup.tmp';
170 runcmd qw(rm -rf), $td;
171 mkdir $td, 0700 or die "$td $!";
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];
184 runcmd qw(git checkout -q), $old;
185 open M, "map" or die $!;
187 m/^(\w+)(?:\s+(\w+))?$/ or die;
189 $count{rewrite_map_previous}++;
195 foreach my $oldc (keys %memo) {
196 my $newc = $memo{$oldc};
197 next if $oldc eq $newc;
200 foreach my $up (@updates) { # catches tags
201 $map{ $up->[1] } = $up->[2];
204 open M, ">", "map" or die $!;
206 $_, (defined $map{$_} ? " $map{$_}" : "")
212 runcmd qw(git add map);
215 runcmd qw(git commit -q), qw(-m), <<END, qw(map);
218 [dgit-badcommit-fixup $our_version]
222 my $new = `git rev-parse HEAD`;
226 chdir '..' or die $!;
227 runcmd qw(rm -rf), $td;
229 $count{rewrite_map_updated}++;
235 my $refs=`git for-each-ref`;
240 our $org_rewrite_map;
242 foreach my $rline (split /\n/, $refs) {
243 my ($obj, $type, $refname) =
244 $rline =~ m/^(\w+)\s+(\w+)\s+(\S.*)/
246 if ($refname eq 'refs/dgit-rewrite/map') {
247 $org_rewrite_map = $obj;
250 next if $refname =~ m{^refs/dgit-(?:badcommit|badfixuptest)/};
254 'exec >/dev/null git symbolic-ref -q "$1"', qw(x),
257 $count{symrefs_ignored}++;
260 die "$? $!" unless $?==256;
263 if ($type eq 'commit') {
264 $rewrite = rewrite_commit($obj);
265 } elsif ($type eq 'tag') {
266 $rewrite = rewrite_tag($obj);
268 warn "ref $refname refers to $type\n";
271 push @updates, [ $refname, $obj, $rewrite ];
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),
285 print Dumper(\%count), "all is well - nothing to do\n";
289 #print Dumper(\@updates);
291 open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!
294 for my $up (@updates) {
295 my ($ref, $old, $new, $nobackup) = @$up;
297 $otherref =~ s{^refs/}{};
299 print U <<END or die $! unless $nobackup;
300 create refs/dgit-badcommit/$otherref $old
302 print U <<END or die $!;
303 update $ref $new $old
306 print U <<END or die $!;
307 update refs/dgit-badfixuptest/$otherref $new
310 print "found trouble in history of $ref\n" or die $!;
316 close U or die "$? $!";
320 print Dumper(\%count);
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 $!;
327 print STDERR "found work to do, exiting status 2\n";