]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Feed.pm
feed: unset GIT_DIR and use "git --git-dir=.. log"
[public-inbox.git] / lib / PublicInbox / Feed.pm
index d2bdea0d685886437ea70cbf83560cbdf9f82c63..d535cea93c95e6cb63029a900342c0957ec64fff 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,
@@ -23,7 +23,6 @@ sub generate {
        require POSIX;
        my $max = $args->{max} || MAX_PER_PAGE;
 
-       local $ENV{GIT_DIR} = $args->{git_dir};
        my $feed_opts = get_feedopts($args);
        my $addr = $feed_opts->{address};
        $addr = $addr->[0] if ref($addr);
@@ -39,7 +38,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);
@@ -52,14 +51,13 @@ sub generate_html_index {
        require Mail::Thread;
 
        my $max = $args->{max} || MAX_PER_PAGE;
-       local $ENV{GIT_DIR} = $args->{git_dir};
        my $feed_opts = get_feedopts($args);
 
        my $title = $feed_opts->{description} || '';
        $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
@@ -130,7 +128,8 @@ sub each_recent_blob {
        # get recent messages
        # we could use git log -z, but, we already know ssoma will not
        # leave us with filenames with spaces in them..
-       my @cmd = qw/git log --no-notes --no-color --raw -r/;
+       my @cmd = ('git', "--git-dir=$args->{git_dir}",
+                       qw/log --no-notes --no-color --raw -r/);
        push @cmd, $range;
 
        my $pid = open(my $log, '-|', @cmd) or
@@ -195,7 +194,7 @@ sub get_feedopts {
                if (ref($cgi) eq 'CGI') {
                        $base = $cgi->url(-base);
                } else {
-                       $base = "${$cgi->base}";
+                       $base = $cgi->base->as_string;
                        $base =~ s!/\z!!;
                }
                $url_base = $path_info;
@@ -294,36 +293,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;