- # for compatibility with existing ssoma installations
- # we can probably remove this entirely by 2020
- my $git_dir = $self->{git}->{git_dir};
- my @cmd = ('git', "--git-dir=$git_dir");
- my $index = "$git_dir/ssoma.index";
- if ($nchg && -e $index && !$ENV{FAST}) {
- my $env = { GIT_INDEX_FILE => $index };
- run_die([@cmd, qw(read-tree -m -v -i), $self->{ref}], $env);
+ _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)) {
+ 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 clean_purge_buffer {
+ my ($oids, $buf) = @_;
+ my $cmt_msg = 'purged '.join(' ',@$oids)."\n";
+ @$oids = ();
+
+ foreach my $i (0..$#$buf) {
+ my $l = $buf->[$i];
+ if ($l =~ /^author .* (\d+ [\+-]?\d+)$/) {
+ $buf->[$i] = "author <> $1\n";
+ } elsif ($l =~ /^data (\d+)/) {
+ $buf->[$i++] = "data " . length($cmt_msg) . "\n";
+ $buf->[$i] = $cmt_msg;
+ last;
+ }
+ }
+}
+
+sub purge_oids {
+ my ($self, $purge) = @_;
+ my $tmp = "refs/heads/purge-".((keys %$purge)[0]);
+ my $old = $self->{'ref'};
+ my $git = $self->{git};
+ my @export = (qw(fast-export --no-data --use-done-feature), $old);
+ my ($rd, $pid) = $git->popen(@export);
+ my ($r, $w) = $self->gfi_start;
+ my @buf;
+ my $npurge = 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 (\d+)/) {
+ # 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);
+ if ($purge->{$oid}) {
+ push @oids, $oid;
+ delete $tree->{$path};
+ } else {
+ $tree->{$path} = 1;
+ push @buf, $_;
+ }
+ } elsif (/^D (\w+)/) {
+ my $path = $1;
+ push @buf, $_ if $tree->{$path};
+ } elsif ($_ eq "\n") {
+ if (@oids) {
+ my $out = join('', @buf);
+ $out =~ s/^/# /sgm;
+ warn "purge rewriting\n", $out, "\n";
+ clean_purge_buffer(\@oids, \@buf);
+ $npurge++;
+ }
+ $w->print(@buf, "\n") or wfail;
+ @buf = ();
+ } elsif ($_ eq "done\n") {
+ $done = 1;
+ } elsif (/^mark :(\d+)$/) {
+ push @buf, $_;
+ $mark = $1;
+ } else {
+ push @buf, $_;
+ }