- my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
- '-t', $path);
- my $pid = open my $fh, '-|';
- defined $pid or die "fork failed: $!\n";
-
- if ($pid == 0) {
- open STDERR, '>', '/dev/null'; # ignore errors
- exec @cmd or die "exec failed: $!\n";
- } else {
- my $type = eval { local $/; <$fh> };
- close $fh;
- if ($? == 0 && $type eq "blob\n") {
- return r302($n->{url}, $cmid);
+ # TODO: reuse existing PublicInbox::Git objects to save forks
+ my $git = PublicInbox::Git->new($n->{git_dir});
+ my (undef, $type, undef) = $git->check($path);
+ return r302($n->{url}, $mid) if ($type && $type eq 'blob');
+ }
+
+ # fall back to partial MID matching
+ my $n_partial = 0;
+ my @partial;
+
+ eval { require PublicInbox::Msgmap };
+ my $have_mm = $@ ? 0 : 1;
+ my $base_url = $cgi->base->as_string;
+ if ($have_mm) {
+ my $tmp_mid = $mid;
+ my $url;
+again:
+ $url = $base_url . $inbox;
+ unshift @pfx, { git_dir => $ctx->{git_dir}, url => $url };
+ foreach my $pfx (@pfx) {
+ my $git_dir = delete $pfx->{git_dir} or next;
+ my $mm = eval { PublicInbox::Msgmap->new($git_dir) };
+
+ $mm or next;
+ if (my $res = $mm->mid_prefixes($tmp_mid)) {
+ $n_partial += scalar(@$res);
+ $pfx->{res} = $res;
+ push @partial, $pfx;
+ }
+ }
+ # fixup common errors:
+ if (!$n_partial && $tmp_mid =~ s,/[tTf],,) {
+ goto again;
+ }
+ }
+
+ my $code = 404;
+ my $h = PublicInbox::Hval->new_msgid($mid, 1);
+ my $href = $h->as_href;
+ my $html = $h->as_html;
+ my $title = "Message-ID <$html> not found";
+ my $s = "<html><head><title>$title</title>" .
+ "</head><body><pre><b>$title</b>\n";
+
+ if ($n_partial) {
+ $code = 300;
+ my $es = $n_partial == 1 ? '' : 'es';
+ $s.= "\n$n_partial partial match$es found:\n\n";
+ foreach my $pfx (@partial) {
+ my $u = $pfx->{url};
+ foreach my $m (@{$pfx->{res}}) {
+ my $p = PublicInbox::Hval->new_msgid($m);
+ my $r = $p->as_href;
+ my $t = $p->as_html;
+ $s .= qq{<a\nhref="$u/$r/">$u/$t/</a>\n};