]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitAsyncCat.pm
gcf2: require git dir with OID
[public-inbox.git] / lib / PublicInbox / GitAsyncCat.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # internal class used by PublicInbox::Git + PublicInbox::DS
5 # This parses the output pipe of "git cat-file --batch"
6 #
7 # Note: this does NOT set the non-blocking flag, we expect `git cat-file'
8 # to be a local process, and git won't start writing a blob until it's
9 # fully read.  So minimize context switching and read as much as possible
10 # and avoid holding a buffer in our heap any longer than it has to live.
11 package PublicInbox::GitAsyncCat;
12 use strict;
13 use parent qw(PublicInbox::DS Exporter);
14 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
15 our @EXPORT = qw(git_async_cat);
16
17 sub event_step {
18         my ($self) = @_;
19         my $gitish = $self->{gitish};
20         return $self->close if ($gitish->{in} // 0) != ($self->{sock} // 1);
21         my $inflight = $gitish->{inflight};
22         if ($inflight && @$inflight) {
23                 $gitish->cat_async_step($inflight);
24                 $self->requeue if @$inflight || exists $gitish->{cat_rbuf};
25         }
26 }
27
28 sub git_async_cat ($$$$) {
29         my ($git, $oid, $cb, $arg) = @_;
30         my $gitish = $git->{gcf2c}; # PublicInbox::Gcf2Client
31         if ($gitish) {
32                 $oid .= " $git->{git_dir}";
33         } else {
34                 $gitish = $git;
35         }
36         $gitish->cat_async($oid, $cb, $arg);
37         $gitish->{async_cat} //= do {
38                 my $self = bless { gitish => $gitish }, __PACKAGE__;
39                 $self->SUPER::new($gitish->{in}, EPOLLIN|EPOLLET);
40                 \undef; # this is a true ref()
41         };
42 }
43
44 1;