+ # 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 .* ([0-9]+ [\+-]?[0-9]+)$/) {
+ $buf->[$i] = "author <> $1\n";
+ } elsif ($l =~ /^data ([0-9]+)/) {
+ $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 = $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 ([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;
+ 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 :([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 $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;