Started on making everything a bit less bleh.
[catagits/Gitalist.git] / lib / Gitalist / Model / Git.pm
index 973de0a..4579f60 100644 (file)
@@ -2,11 +2,14 @@ package Gitalist::Model::Git;
 
 use Moose;
 use namespace::autoclean;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
 use Moose::Autobox;
 
 extends 'Catalyst::Model';
 with 'Catalyst::Component::InstancePerContext';
 
+has repo_dir => ( is => 'ro', required => 1, isa => NonEmptySimpleStr );
+
 =head1 NAME
 
 Gitalist::Model::Git - the model for git interactions
@@ -20,22 +23,21 @@ Gitalist::Model::Git - the model for git interactions
 =cut
 
 use Git::PurePerl;
-
+use Path::Class qw/dir/;
 sub build_per_context_instance {
   my ( $self, $c ) = @_;
 
   my $app = blessed($c) || $c;
-  my $model = Git::Repos->new( repo_dir => $app->config->{repo_dir} );
+  my $model = Git::Repos->new(
+    project => ([$c->req->parameters->{p} || '/']->flatten)[0],
+    repo_dir => $self->repo_dir,
+  );
 
-  if ($c->req->parameters->{p}) {
-      # A Project was passed in
-      my $project = ([$c->req->parameters->{p} || '/']->flatten)[0];
-      $model->project( $project );
+  # This is fugly as fuck. Move Git::PurePerl construction into attribute builders..
+  my ($pd, $gd) = $model->project_dir( $model->project )->resolve =~ m{((.+?)(:?/\/\.git)?$)};
+  $gd .= '/.git' if ($gd !~ /\.git$/ and -d "$gd/.git");
+  $model->gpp( Git::PurePerl->new(gitdir => $gd, directory => $pd) );
 
-      # This is fugly as fu\ack. Move Git::PurePerl construction into attribute builders..
-      (my $pd = $model->project_dir($project)) =~ s{/\.git$}();
-      $model->gpp( Git::PurePerl->new(directory => $pd) );
-  }
   return $model;
 }
 
@@ -249,29 +251,28 @@ each item will contain the contents of L</project_info>.
 =cut
 
 sub list_projects {
-  my ($self, $dir) = @_;
-
-  my $base = dir($dir || $self->repo_dir);
-
-  my @ret;
-  my $dh = $base->open;
-  while (my $file = $dh->read) {
-    next if $file =~ /^.{1,2}$/;
-
-    my $obj = $base->subdir($file);
-    next unless -d $obj;
-    next unless $self->is_git_repo($obj);
-
-    # XXX Leaky abstraction alert!
-    my $is_bare = !-d $obj->subdir('.git');
-
-    my $name = (File::Spec->splitdir($obj))[-1];
-    push @ret, {
-      name => ($name . ( $is_bare ? '' : '/.git' )),
-      $self->get_project_properties(
-        $is_bare ? $obj : $obj->subdir('.git')
-        ),
-      };
+    my ($self, $dir) = @_;
+
+    my $base = dir($dir || $self->repo_dir);
+
+    my @ret;
+    my $dh = $base->open or die("Cannot open dir $base");
+    while (my $file = $dh->read) {
+        next if $file =~ /^.{1,2}$/;
+
+        my $obj = $base->subdir($file);
+        next unless -d $obj;
+        next unless $self->is_git_repo($obj);
+               # XXX Leaky abstraction alert!
+               my $is_bare = !-d $obj->subdir('.git');
+
+               my $name = (File::Spec->splitdir($obj))[-1];
+        push @ret, {
+            name => ($name . ( $is_bare ? '' : '/.git' )),
+            $self->get_project_properties(
+                               $is_bare ? $obj : $obj->subdir('.git')
+                       ),
+        };
   }
 
   return [sort { $a->{name} cmp $b->{name} } @ret];
@@ -576,22 +577,11 @@ Given the output of the C<rev-list> command return a list of hashes.
 
 sub parse_rev_list {
   my ($self, $output) = @_;
-  my @ret;
-
-  my @revs = split /\0/, $output;
-
-  for my $rev (split /\0/, $output) {
-    for my $line (split /\n/, $rev, 6) {
-      chomp $line;
-      next unless $line;
 
-      if ($self->valid_rev($line)) {
-        push @ret, $self->get_object($line);
-      }
-       }
-  }
-
-  return @ret;
+  return
+    map  $self->get_object($_),
+    grep $self->valid_rev($_),
+    map  split(/\n/, $_, 6), split /\0/, $output;
 }
 
 =head2 list_revs
@@ -604,12 +594,27 @@ array of hashes.
 sub list_revs {
   my ($self, %args) = @_;
 
-  $args{sha1} ||= $self->head_hash($args{project});
+  $args{sha1} = $self->head_hash($args{sha1})
+    if !$args{sha1} || $args{sha1} !~ $SHA1RE;
+
+       my @search_opts;
+  if($args{search}) {
+    my $sargs = $args{search};
+    $sargs->{type} = 'grep'
+      if $sargs->{type} eq 'commit';
+    @search_opts = (
+       # This seems a little fragile ...
+       qq[--$sargs->{type}=$sargs->{text}],
+       '--regexp-ignore-case',
+       $sargs->{regexp} ? '--extended-regexp' : '--fixed-strings'
+    );
+  }
 
   my $output = $self->run_cmd_in($args{project} || $self->project, 'rev-list',
     '--header',
-    (defined $args{ count } ? "--max-count=$args{count}" : ()),
-    (defined $args{ skip  } ? "--skip=$args{skip}"       : ()),
+    (defined $args{ count  } ? "--max-count=$args{count}" : ()),
+    (defined $args{ skip   } ? "--skip=$args{skip}"       : ()),
+    @search_opts,
     $args{sha1},
     '--',
     ($args{file} ? $args{file} : ()),