Converted the 'heads' action to the new model.
Zachary Stevens [Sat, 7 Nov 2009 17:55:53 +0000 (17:55 +0000)]
lib/Gitalist/Controller/Root.pm
lib/Gitalist/Git/Object.pm
lib/Gitalist/Git/Project.pm
t/02git_project.t

index 550dcb8..b8eb602 100644 (file)
@@ -63,7 +63,16 @@ sub _get_commit {
 
   my $h = $haveh || $c->req->param('h') || '';
   my $f = $c->req->param('f');
-  my $m = $c->model();
+
+  # FIXME this can die when everything is migrated
+  my ($m, $pd);
+  if ($c->stash->{current_model} eq 'GitRepos') {
+      $m = $c->model()->project($c->stash->{project});
+      $pd = $m->path;
+  } else {
+      $m = $c->model();
+      ($pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
+  }
 
   # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
   my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
@@ -72,8 +81,6 @@ sub _get_commit {
           # XXX This could definitely use more context.
           || Carp::croak("Couldn't find a hash for the commit object!");
 
-
-  (my $pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
   my $commit = $m->get_object($hash)
     or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
 
@@ -135,10 +142,11 @@ The current list of heads (aka branches) in the repo.
 
 sub heads : Local {
   my ( $self, $c ) = @_;
-
+  $c->stash( current_model => 'GitRepos' );
+  my $project = $c->model()->project( $c->stash->{project} );
   $c->stash(
     commit => $self->_get_commit($c),
-    heads  => [$c->model()->heads],
+    heads  => [$project->heads],
     action => 'heads',
   );
 }
@@ -570,7 +578,12 @@ sub end : ActionClass('RenderView') {
   my ($self, $c) = @_;
   # Give project views the current HEAD.
   if ($c->stash->{project}) {
-      $c->stash->{HEAD} = $c->model()->head_hash;
+      if ($c->stash->{current_model} eq 'GitRepos') {
+          my $project = $c->model()->project($c->stash->{project});
+          $c->stash->{HEAD} = $project->head_hash;
+      } else {
+          $c->stash->{HEAD} = $c->model()->head_hash;
+      }
   }
 }
 
index bfdf55b..0346365 100644 (file)
@@ -9,7 +9,7 @@ class Gitalist::Git::Object {
                      is => 'ro',
                      handles => [ 'run_cmd' ],
                  );
-    has sha1 ( isa => Str,
+    has sha1 => ( isa => Str,
                required => 1,
                is => 'ro' );
 
index 1e612f9..e11da8e 100644 (file)
@@ -38,9 +38,15 @@ class Gitalist::Git::Project {
         $self->$_() for qw/_util last_change owner description/; # Ensure to build early.
     }
 
+    method _project_dir {
+        -f $self->{path}->file('.git', 'HEAD')
+            ? $self->{path}->subdir('.git')
+            : $self->{path};
+    }
+
     method _build__util {
         Gitalist::Git::Util->new(
-            gitdir => $self->path,
+            gitdir => $self->_project_dir($self->path),
         );
     }
 
@@ -73,6 +79,28 @@ class Gitalist::Git::Project {
         return $last_change;
     }
 
+    method heads {
+        my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
+        my @output = $cmdout ? split(/\n/, $cmdout) : ();
+        my @ret;
+        for my $line (@output) {
+            my ($rev, $head, $commiter) = split /\0/, $line, 3;
+            $head =~ s!^refs/heads/!!;
+
+            push @ret, { sha1 => $rev, name => $head };
+
+            #FIXME: That isn't the time I'm looking for..
+            if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
+                my $dt = DateTime->from_epoch(epoch => $epoch);
+                $dt->set_time_zone($tz);
+                $ret[-1]->{last_change} = $dt;
+            }
+        }
+
+        return @ret;
+    }
+
+
 =head2 head_hash
 
 Find the hash of a given head (defaults to HEAD).
index 19409e7..8829731 100644 (file)
@@ -22,6 +22,8 @@ isa_ok($proj->last_change, 'DateTime', 'last_change');
 
 is($proj->info->{name}, qw/repo1/, 'repo name in info hash');
 
+ok($proj->heads, '->heads returns stuff');
+     
 is($proj->head_hash, qw/36c6c6708b8360d7023e8a1649c45bcf9b3bd818/, 'head_hash for HEAD is correct');
 
 is(scalar $proj->list_tree, 2, 'expected number of entries in tree');