]> Sergey Matveev's repositories - public-inbox.git/commitdiff
implement our own cat-file --batch wrapper
authorEric Wong <e@80x24.org>
Tue, 29 Apr 2014 05:10:48 +0000 (05:10 +0000)
committerEric Wong <e@80x24.org>
Tue, 29 Apr 2014 05:10:48 +0000 (05:10 +0000)
We use --git-dir=... instead of $ENV{GIT_DIR} because ENV changes
do not propagate easily with mod_perl.

MANIFEST
lib/PublicInbox/Feed.pm
lib/PublicInbox/GitCatFile.pm [new file with mode: 0644]
public-inbox.cgi

index b212c76b3e27054bebdb0a59b7ac13c9bde22146..ecdc8b5d7b0b48d50e2e8368623ff8a08a2d3664 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -10,6 +10,7 @@ lib/PublicInbox/MDA.pm
 lib/PublicInbox/Config.pm
 lib/PublicInbox/Feed.pm
 lib/PublicInbox/Filter.pm
+lib/PublicInbox/GitCatFile.pm
 lib/PublicInbox/Hval.pm
 lib/PublicInbox/View.pm
 public-inbox-mda
index 87b5b2a937469929aebbd190ccda866185423aa6..ad058395ce6c19294d832fc91ee1a162ebce9611 100644 (file)
@@ -7,7 +7,7 @@ use Email::Address;
 use Email::MIME;
 use Date::Parse qw(strptime str2time);
 use PublicInbox::Hval;
-eval { require Git }; # this is GPLv2+, so we are OK to use it
+use PublicInbox::GitCatFile;
 use constant {
        DATEFMT => '%Y-%m-%dT%H:%M:%SZ',
        MAX_PER_PAGE => 25,
@@ -39,7 +39,7 @@ sub generate {
                updated => POSIX::strftime(DATEFMT, gmtime),
        );
 
-       my $git = try_git_pm($args->{git_dir});
+       my $git = PublicInbox::GitCatFile->new($args->{git_dir});
        each_recent_blob($args, sub {
                my ($add) = @_;
                add_to_feed($feed_opts, $feed, $add, $git);
@@ -59,7 +59,7 @@ sub generate_html_index {
        $title = PublicInbox::Hval->new_oneline($title)->as_html;
 
        my @messages;
-       my $git = try_git_pm($args->{git_dir});
+       my $git = PublicInbox::GitCatFile->new($args->{git_dir});
        my $last = each_recent_blob($args, sub {
                my $mime = do_cat_mail($git, $_[0]) or return 0;
                $mime->body_set(''); # save some memory
@@ -294,36 +294,10 @@ sub dump_html_line {
        dump_html_line($self->next, $level, $html) if $self->next;
 }
 
-sub try_git_pm {
-       my ($dir) = @_;
-       eval { Git->repository(Directory => $dir) };
-};
-
 sub do_cat_mail {
        my ($git, $path) = @_;
-       my $str;
-       if ($git) {
-               open my $fh, '>', \$str or
-                               die "failed to setup string handle: $!\n";
-               binmode $fh;
-               my $err = '';
-               my $bytes;
-               {
-                       local $SIG{__WARN__} = sub { $err .= $_[0] };
-                       $bytes = $git->cat_blob("HEAD:$path", $fh);
-               }
-               close $fh or die "failed to close string handle: $!\n";
-
-               if ($bytes < 0 && $err &&
-                               $err !~ /doesn't exist in the repository/) {
-                       warn $err;
-               }
-               return if $bytes <= 0;
-       } else {
-               $str = `git cat-file blob HEAD:$path`;
-               return if $? != 0 || length($str) == 0;
-       }
-       Email::MIME->new($str);
+       my $str = $git->cat_file("HEAD:$path");
+       Email::MIME->new($$str);
 }
 
 1;
diff --git a/lib/PublicInbox/GitCatFile.pm b/lib/PublicInbox/GitCatFile.pm
new file mode 100644 (file)
index 0000000..bdad2b5
--- /dev/null
@@ -0,0 +1,73 @@
+# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
+# License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)
+# This is based on code in Git.pm which is GPLv2, but modified to avoid
+# dependence on environment variables for compatibility with mod_perl.
+# There are also API changes to simplify our usage and data set.
+package PublicInbox::GitCatFile;
+use strict;
+use warnings;
+use IPC::Open2 qw(open2);
+
+sub new {
+       my ($class, $git_dir) = @_;
+       bless { git_dir => $git_dir }, $class;
+}
+
+sub _cat_file_begin {
+       my ($self) = @_;
+       return if $self->{pid};
+       my ($in, $out);
+       my $pid = open2($in, $out, 'git', '--git-dir', $self->{git_dir},
+                       'cat-file', '--batch');
+
+       $self->{pid} = $pid;
+       $self->{in} = $in;
+       $self->{out} = $out;
+}
+
+sub cat_file {
+       my ($self, $object) = @_;
+
+       $self->_cat_file_begin;
+       print { $self->{out} } $object, "\n" or die "write error: $!\n";
+
+       my $in = $self->{in};
+       my $head = <$in>;
+       $head =~ / missing$/ and return undef;
+       $head =~ /^[0-9a-f]{40} \S+ (\d+)$/ or
+               die "Unexpected result from git cat-file: $head\n";
+
+       my $size = $1;
+       my $bytes_left = $size;
+       my $buf;
+       my $rv = '';
+
+       while ($bytes_left) {
+               my $read = read($in, $buf, $bytes_left);
+               defined($read) or die "read pipe failed: $!\n";
+               $rv .= $buf;
+               $bytes_left -= $read;
+       }
+
+       my $read = read($in, $buf, 1);
+       defined($read) or die "read pipe failed: $!\n";
+       if ($read != 1 || $buf ne "\n") {
+               die "newline missing after blob\n";
+       }
+       \$rv;
+}
+
+sub DESTROY {
+       my ($self) = @_;
+       my $pid = $self->{pid} or return;
+       $self->{pid} = undef;
+       foreach my $f (qw(in out)) {
+               my $fh = $self->{$f};
+               defined $fh or next;
+               close $fh;
+               $self->{$f} = undef;
+       }
+       waitpid $pid, 0;
+}
+
+1;
index 9314712431a54da4c8a345c741ff9c8e13bd59b9..ffd6ec08f4a898f9712469ad45391cc98fe3c379 100755 (executable)
@@ -218,7 +218,6 @@ sub psgi_app {
        require POSIX;
        require XML::Atom::SimpleFeed;
        require Plack::Request;
-       eval { require Git }; # optional
        sub {
                my $req = Plack::Request->new(@_);
                main($req, $req->method);