+ _update_git_info($self, 1) if delete $self->{nchg};
+
+ $self->lock_release;
+
+ $self->{git}->cleanup;
+}
+
+sub atfork_child {
+ my ($self) = @_;
+ foreach my $f (qw(in out)) {
+ next unless defined($self->{$f});
+ close $self->{$f} or die "failed to close import[$f]: $!\n";
+ }
+}
+
+sub digest2mid ($$) {
+ my ($dig, $hdr) = @_;
+ my $b64 = $dig->clone->b64digest;
+ # Make our own URLs nicer:
+ # See "Base 64 Encoding with URL and Filename Safe Alphabet" in RFC4648
+ $b64 =~ tr!+/=!-_!d;
+
+ # 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, $_;
+ }