use Git.pm for efficient cat_blob if available
[ssoma-mda.git] / lib / Ssoma / Git.pm
index 5cec7fb9fd4c93596d91aaccaf5dc40a63a71be2..5f5702bbcaf7cf281c0fe6aa1085e5e61b0725aa 100644 (file)
@@ -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;