+ # Add a date prefix to prevent a leading '-' in case that trips
+ # up some tools (e.g. if a Message-ID were a expected as a
+ # command-line arg)
+ my $dt = msg_datestamp($hdr);
+ $dt = POSIX::strftime('%Y%m%d%H%M%S', gmtime($dt));
+ "$dt.$b64" . '@z';
+}
+
+sub rewrite_commit ($$$$) {
+ my ($self, $oids, $buf, $mime) = @_;
+ my ($name, $email, $at, $ct, $subject);
+ if ($mime) {
+ ($name, $email, $at, $ct, $subject) = extract_cmt_info($mime);
+ } else {
+ $name = $email = '';
+ $subject = 'purged '.join(' ', @$oids);
+ }
+ @$oids = ();
+ $subject .= "\n";
+ foreach my $i (0..$#$buf) {
+ my $l = $buf->[$i];
+ if ($l =~ /^author .* ([0-9]+ [\+-]?[0-9]+)$/) {
+ $at //= $1;
+ $buf->[$i] = "author $name <$email> $at\n";
+ } elsif ($l =~ /^committer .* ([0-9]+ [\+-]?[0-9]+)$/) {
+ $ct //= $1;
+ $buf->[$i] = "committer $self->{ident} $ct\n";
+ } elsif ($l =~ /^data ([0-9]+)/) {
+ $buf->[$i++] = "data " . length($subject) . "\n";
+ $buf->[$i] = $subject;
+ last;
+ }
+ }
+}
+
+# returns the new commit OID if a replacement was done
+# returns undef if nothing was done
+sub replace_oids {
+ my ($self, $mime, $replace_map) = @_; # oid => raw string
+ my $tmp = "refs/heads/replace-".((keys %$replace_map)[0]);
+ my $old = $self->{'ref'};
+ my $git = $self->{git};
+ my @export = (qw(fast-export --no-data --use-done-feature), $old);
+ my $rd = $git->popen(@export);
+ my ($r, $w) = $self->gfi_start;
+ my @buf;
+ my $nreplace = 0;
+ my @oids;
+ my ($done, $mark);
+ my $tree = $self->{-tree};
+ while (<$rd>) {
+ if (/^reset (?:.+)/) {
+ push @buf, "reset $tmp\n";
+ } elsif (/^commit (?:.+)/) {
+ if (@buf) {
+ $w->print(@buf) or wfail;
+ @buf = ();
+ }
+ push @buf, "commit $tmp\n";
+ } elsif (/^data ([0-9]+)/) {
+ # only commit message, so $len is small:
+ my $len = $1; # + 1 for trailing "\n"
+ push @buf, $_;
+ my $n = read($rd, my $buf, $len) or die "read: $!";
+ $len == $n or die "short read ($n < $len)";
+ push @buf, $buf;
+ } elsif (/^M 100644 ([a-f0-9]+) (\w+)/) {
+ my ($oid, $path) = ($1, $2);
+ $tree->{$path} = 1;
+ my $sref = $replace_map->{$oid};
+ if (defined $sref) {
+ push @oids, $oid;
+ my $n = length($$sref);
+ push @buf, "M 100644 inline $path\ndata $n\n";
+ push @buf, $$sref; # hope CoW works...
+ push @buf, "\n";
+ } else {
+ push @buf, $_;
+ }
+ } elsif (/^D (\w+)/) {
+ my $path = $1;
+ push @buf, $_ if $tree->{$path};
+ } elsif ($_ eq "\n") {
+ if (@oids) {
+ if (!$mime) {
+ my $out = join('', @buf);
+ $out =~ s/^/# /sgm;
+ warn "purge rewriting\n", $out, "\n";
+ }
+ rewrite_commit($self, \@oids, \@buf, $mime);
+ $nreplace++;
+ }
+ $w->print(@buf, "\n") or wfail;
+ @buf = ();
+ } elsif ($_ eq "done\n") {
+ $done = 1;
+ } elsif (/^mark :([0-9]+)$/) {
+ push @buf, $_;
+ $mark = $1;
+ } else {
+ push @buf, $_;
+ }
+ }
+ close $rd or die "close fast-export failed: $?";
+ if (@buf) {
+ $w->print(@buf) or wfail;
+ }
+ die 'done\n not seen from fast-export' unless $done;
+ chomp(my $cmt = $self->get_mark(":$mark")) if $nreplace;
+ $self->{nchg} = 0; # prevent _update_git_info until update-ref:
+ $self->done;
+ my @git = ('git', "--git-dir=$git->{git_dir}");
+
+ run_die([@git, qw(update-ref), $old, $tmp]) if $nreplace;
+
+ run_die([@git, qw(update-ref -d), $tmp]);
+
+ return if $nreplace == 0;
+
+ run_die([@git, qw(-c gc.reflogExpire=now gc --prune=all --quiet)]);
+
+ # check that old OIDs are gone
+ my $err = 0;
+ foreach my $oid (keys %$replace_map) {
+ my @info = $git->check($oid);
+ if (@info) {
+ warn "$oid not replaced\n";
+ $err++;
+ }
+ }
+ _update_git_info($self, 0);
+ die "Failed to replace $err object(s)\n" if $err;
+ $cmt;