+sub edit_rewrite_map ($) {
+ my ($old) = @_;
+
+ filter_updates();
+ return $old unless @updates;
+
+ my $td = 'dgit-broken-fixup.tmp';
+ runcmd qw(rm -rf), $td;
+ mkdir $td, 0700 or die "$td $!";
+ chdir $td or die $!;
+ runcmd qw(git init -q);
+ runcmd qw(git config gc.auto 0);
+ runcmd qw(rm -rf .git/objects);
+ symlink "../../objects", ".git/objects" or die $!;
+ foreach my $c (@configs) {
+ runcmd qw(git config), $c->[0], $c->[1];
+ }
+
+ my %map;
+
+ if ($old) {
+ runcmd qw(git checkout -q), $old;
+ open M, "map" or die $!;
+ while (<M>) {
+ m/^(\w+)(?:\s+(\w+))?$/ or die;
+ $map{$1} = $2;
+ $count{rewrite_map_previous}++;
+ }
+ M->error and die $!;
+ close M or die $!;
+ }
+
+ foreach my $oldc (keys %memo) {
+ my $newc = $memo{$oldc};
+ next if $oldc eq $newc;
+ $map{$oldc} = $newc;
+ }
+ foreach my $up (@updates) { # catches tags
+ $map{ $up->[1] } = $up->[2];
+ }
+
+ open M, ">", "map" or die $!;
+ printf M "%s%s\n",
+ $_, (defined $map{$_} ? " $map{$_}" : "")
+ or die $!
+ foreach keys %map;
+ close M or die $!;
+
+ if (!$old) {
+ runcmd qw(git add map);
+ }
+
+ runcmd qw(git commit -q), qw(-m), <<END, qw(map);
+dgit-badcommit-fixup
+
+[dgit-badcommit-fixup $our_version]
+END
+
+ $!=0; $?=0;
+ my $new = `git rev-parse HEAD`;
+ die "$? $!" if $?;
+ chomp $new or die;
+
+ chdir '..' or die $!;
+ runcmd qw(rm -rf), $td;
+
+ $count{rewrite_map_updated}++;
+
+ return $new;
+}
+