Added very basic blob action.
broquaint [Fri, 18 Sep 2009 14:46:29 +0000 (15:46 +0100)]
lib/Gitalist/Controller/Root.pm
lib/Gitalist/Model/Git.pm
templates/blob.tt2 [new file with mode: 0644]
templates/default.tt2

index a71c03d..b32117f 100644 (file)
@@ -29,7 +29,6 @@ Gitalist::Controller::Root - Root Controller for Gitalist
 use IO::Capture::Stdout;
 use File::Slurp qw(slurp);
 
-#sub default :Path {
 sub run_gitweb {
   my ( $self, $c ) = @_;
 
@@ -72,6 +71,24 @@ sub index :Path :Args(0) {
   );
 }
 
+sub blob : Local {
+  my ( $self, $c ) = @_;
+
+  my $git      = $c->model('Git');
+  my $req      = $c->req;
+  my $filename = $req->param('f')  || $req->param('filename');
+  my $hash     = $req->param('hb') || $req->param('hashbase')
+              || $git->get_head_hash($req->param('p') || $req->param('project'));
+  my $filehash = $git->get_hash_by_path($hash, $filename, 'blob');
+  
+  my $blob = $git->run_cmd('cat-file' => blob => $filehash);
+
+  $c->stash(
+      blob   => encode_entities($blob),
+      action => 'blob',
+  );
+}
+
 sub auto : Private {
     my($self, $c) = @_;
 
@@ -162,10 +179,10 @@ sub header {
 
        if(defined $project) {
     $c->stash(
-      search_text => $c->req->param('s') || $c->req->param('searchtext'),
-      search_hash => $c->req->param('hb') || $c->req->param('hashbase')
+      search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
+      search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
                    || $c->req->param('h')  || $c->req->param('hash')
-                   || 'HEAD'
+                   || 'HEAD' ),
     );
        }
 }
index 009634b..d5c3491 100644 (file)
@@ -195,6 +195,21 @@ 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) = @_;
 
diff --git a/templates/blob.tt2 b/templates/blob.tt2
new file mode 100644 (file)
index 0000000..a4ff6c0
--- /dev/null
@@ -0,0 +1,5 @@
+<div>
+<pre>
+[% blob %]
+</pre>
+</div>
index c476c28..f8db2b2 100644 (file)
@@ -66,8 +66,8 @@
   END;
 
   IF action;
-    # XXX Err ... yeah, slight hack here ... ahem.
-    INCLUDE "index.tt2";
+    SET actmpl = action _ ".tt2";
+    INCLUDE $actmpl;
   ELSE;
     # The output of gitweb.cgi is injected at this point.
     content;