Merge in master changes.
[catagits/Gitalist.git] / lib / Gitalist / Model / Git.pm
index d5c3491..77f324a 100644 (file)
@@ -1,6 +1,7 @@
 package Gitalist::Model::Git;
 
 use Moose;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
 use namespace::autoclean;
 
 BEGIN { extends 'Catalyst::Model' }
@@ -12,31 +13,32 @@ use File::Find::Rule;
 use DateTime::Format::Mail;
 use File::Stat::ModeString;
 use List::MoreUtils qw/any/;
-use Scalar::Util qw/blessed/;
+use File::Which;
 
-{
-       my $git;
-       sub git {
-               return $git
-                       if $git;
-
-               if (my $config_git = Gitalist->config->{git}) {
-                       $git = $config_git if -x $config_git;
-               }
-               else {
-                       require File::Which;
-                       $git = File::Which::which('git');
-               }
-
-               if (!$git) {
-                       die <<EOR
+has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class
+has git      => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
+
+sub BUILD {
+    my ($self) = @_;
+    $self->git; # Cause lazy value build.
+       $self->repo_dir;
+}
+
+sub _build_git {
+       my $git = File::Which::which('git');
+
+       if (!$git) {
+               die <<EOR
 Could not find a git executable.
 Please specify the which git executable to use in gitweb.yml
 EOR
-               }
-
-               return $git;
        }
+
+       return $git;
+}
+
+sub _build_repo_dir {
+       return Gitalist->config->{repo_dir};
 }
 
 sub is_git_repo {
@@ -88,7 +90,7 @@ sub get_project_properties {
 sub list_projects {
     my ($self) = @_;
 
-    my $base = dir(Gitalist->config->{repo_dir});
+    my $base = dir($self->repo_dir);
 
     my @ret;
     my $dh = $base->open;
@@ -103,7 +105,7 @@ sub list_projects {
 
                my $name = (File::Spec->splitdir($obj))[-1];
         push @ret, {
-            name => ($name . ( $is_bare ? '.git' : '/.git' )),
+            name => ($name . ( $is_bare ? '' : '/.git' )),
             $self->get_project_properties(
                                $is_bare ? $obj : $obj->subdir('.git')
                        ),
@@ -116,7 +118,7 @@ sub list_projects {
 sub run_cmd {
     my ($self, @args) = @_;
 
-    open my $fh, '-|', __PACKAGE__->git, @args
+    open my $fh, '-|', $self->git, @args
         or die "failed to run git command";
     binmode $fh, ':encoding(UTF-8)';
 
@@ -142,7 +144,7 @@ sub run_cmd_in {
 sub git_dir_from_project_name {
     my ($self, $project) = @_;
 
-    return dir(Gitalist->config->{repo_dir})->subdir($project);
+    return dir($self->repo_dir)->subdir($project);
 }
 
 sub get_head_hash {
@@ -195,21 +197,6 @@ sub get_object_type {
     return $output;
 }
 
-sub get_hash_by_path {
-       my($self, $base, $path, $type) = @_;
-
-       $path =~ s{/+$}();
-
-       my $line = $self->run_cmd('ls-tree', $base, '--', $path)
-    or return;
-
-       #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
-       $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
-       return defined $type && $type ne $2
-       ? ()
-            : return $3;
-}
-
 sub cat_file {
     my ($self, $project, $object) = @_;