From 1d9ee468725b3689a20128ddcb19fb70294b340f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 20 Apr 2014 19:18:19 +0000 Subject: [PATCH] use Git.pm for efficient cat_blob if available This reduces the amount of fork+exec and should improve performance for large imports. --- lib/Ssoma/Extractor.pm | 6 ++++-- lib/Ssoma/Git.pm | 27 +++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/Ssoma/Extractor.pm b/lib/Ssoma/Extractor.pm index 2589adb..9c93b43 100644 --- a/lib/Ssoma/Extractor.pm +++ b/lib/Ssoma/Extractor.pm @@ -96,8 +96,9 @@ sub _extract { sub _deliver_each_msg { my ($self, $dest, $tip, $new) = @_; my $git = $self->{git}; + my $git_pm = $git->try_git_pm; foreach my $path (@$new) { - _deliver_die($git->cat_blob("$tip:$path"), $dest); + _deliver_die($git->cat_blob("$tip:$path", $git_pm), $dest); } } @@ -118,8 +119,9 @@ sub _imap_deliver_each_msg { my $git = $self->{git}; require Ssoma::IMAP; my $imap = Ssoma::IMAP->new($git); + my $git_pm = $git->try_git_pm; foreach my $path (@$new) { - $imap->imap_deliver($git->cat_blob("$tip:$path")); + $imap->imap_deliver($git->cat_blob("$tip:$path", $git_pm)); } $imap->quit; } diff --git a/lib/Ssoma/Git.pm b/lib/Ssoma/Git.pm index 5cec7fb..5f5702b 100644 --- a/lib/Ssoma/Git.pm +++ b/lib/Ssoma/Git.pm @@ -199,10 +199,20 @@ sub mid2path { } sub cat_blob { - my ($self, $blob_id) = @_; - my $cmd = "git cat-file blob $blob_id"; - my $str = `$cmd`; - die "$cmd failed: $?\n" if $?; + my ($self, $blob_id, $git_pm) = @_; + my $str; + if ($git_pm) { + open my $fh, '>', \$str or + die "failed to setup string handle: $!\n"; + binmode $fh; + my $bytes = $git_pm->cat_blob($blob_id, $fh); + close $fh or die "failed to close string handle: $!\n"; + die "$blob_id invalid\n" if $bytes <= 0; + } else { + my $cmd = "git cat-file blob $blob_id"; + $str = `$cmd`; + die "$cmd failed: $?\n" if $?; + } $str; } @@ -268,4 +278,13 @@ sub commit_index { system(@cmd) == 0 or die "command: ". join(' ', @cmd) . ": $?\n"; } +# keep Git.pm optional, not all installations of git have it +sub try_git_pm { + my ($self) = @_; + eval { + require Git; + Git->repository(Directory => $self->{git_dir}); + }; +} + 1; -- 2.26.2