+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);
+ $tree->{$path} = 1;
+ if ($purge->{$oid}) {
+ push @oids, $oid;
+ my $cmd = "M 100644 inline $path\ndata 0\n\n";
+ push @buf, $cmd;
+ } else {
+ 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, $_;
+ }
+ }
+ 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 $npurge;
+ $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 $npurge;
+
+ run_die([@git, qw(update-ref -d), $tmp]);
+
+ return if $npurge == 0;
+
+ run_die([@git, qw(-c gc.reflogExpire=now gc --prune=all)]);
+ my $err = 0;
+ foreach my $oid (keys %$purge) {
+ my @info = $git->check($oid);
+ if (@info) {
+ warn "$oid not purged\n";
+ $err++;
+ }
+ }
+ _update_git_info($self, 0);
+ die "Failed to purge $err object(s)\n" if $err;
+ $cmt;