Move Repository->hash_by_path to Commit->sha_by_path.
Zachary Stevens [Sat, 26 Jun 2010 22:14:11 +0000 (23:14 +0100)]
lib/Gitalist/Controller/Fragment/Ref.pm
lib/Gitalist/Git/Object/Commit.pm
lib/Gitalist/Git/Repository.pm
lib/Gitalist/URIStructure/Ref.pm
t/02git_Repository.t

index 718d195..e03bb22 100644 (file)
@@ -49,7 +49,7 @@ after tree => sub {
     my $repository = $c->stash->{Repository};
     my $commit  = $c->stash->{Commit};
     my $tree    = $c->stash->{filename}
-      ? $repository->get_object($repository->hash_by_path($commit->sha1, $c->stash->{filename}))
+      ? $repository->get_object($commit->sha_by_path($c->stash->{filename}))
       : $repository->get_object($commit->tree_sha1)
     ;
     $c->stash(
@@ -98,12 +98,8 @@ after history => sub {
        ($filename ? (file => $filename) : ())
     );
 
-    my $file = $repository->get_object(
-        $repository->hash_by_path(
-            $repository->head_hash,
-            $filename
-        )
-    );
+    my $commit = $repository->get_object('HEAD');
+    my $file = $repository->get_object($commit->sha_by_path($filename));
 
     my $page = $c->req->param('pg') || 0;
     $logargs{skip} = $c->req->param('pg') * $logargs{count}
index dde3220..5d3cdd3 100644 (file)
@@ -23,6 +23,19 @@ class Gitalist::Git::Object::Commit
                                       ],
                          );
 
+        method sha_by_path ($path) {
+            $path =~ s{/+$}();
+            # FIXME should this really just take the first result?
+            my @paths = $self->repository->run_cmd('ls-tree', $self->sha1, '--', $path)
+                or return;
+            my $line = $paths[0];
+
+            #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa     panic.c'
+            $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
+            my $sha1 = $3;
+            return $sha1;
+    }
+
         method get_patch ( Maybe[NonEmptySimpleStr] $parent_hash?,
                            Int $patch_count?) {
             # assembling the git command to execute...
@@ -233,6 +246,10 @@ Subclass of C<Gitalist::Git::Object>.
 
 =head1 METHODS
 
+=head2 sha_by_path ($path)
+
+Returns the tree/file sha1 for a given path in a commit.
+
 =head2 get_patch
 
 =head2 diff
index 941a4a0..06d3914 100644 (file)
@@ -108,18 +108,6 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
         );
     }
 
-    method hash_by_path ($base, $path) {
-        $path =~ s{/+$}();
-        # FIXME should this really just take the first result?
-        my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
-            or return;
-        my $line = $paths[0];
-
-        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
-        $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
-        return $3;
-    }
-
     method list_revs ( NonEmptySimpleStr :$sha1!,
                        Int :$count?,
                        Int :$skip?,
@@ -399,10 +387,6 @@ Each item is a L<Gitalist::Git::Object>.
 
 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
 
-=head2 hash_by_path ($commit, $path)
-
-Returns the tree/file sha1 for a given path in a commit.
-
 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
 
 Returns a list of revs for the given head ($sha1).
index 2536dc5..194786f 100644 (file)
@@ -65,7 +65,7 @@ sub find_blob : Action {
     my($repo, $object) = @{$c->{stash}}{qw(Repository Commit)};
     # FIXME - Eugh!
     my $h  = $object->isa('Gitalist::Git::Object::Commit')
-           ? $repo->hash_by_path($object->sha1, $c->stash->{filename})
+           ? $object->sha_by_path($c->stash->{filename})
            : $object->isa('Gitalist::Git::Object::Blob')
              ? $object->sha1
              : die "Unknown object type for '${\$object->sha1}'";
index 749ac12..0d4de4b 100644 (file)
@@ -65,14 +65,15 @@ isa_ok(($proj->list_tree)[1], 'Gitalist::Git::Object');
 my $obj1 = $proj->get_object('729a7c3f6ba5453b42d16a43692205f67fb23bc1');
 isa_ok($obj1, 'Gitalist::Git::Object::Tree');
 
-my $hbp_sha1 = $proj->hash_by_path('36c6c6708b8360d7023e8a1649c45bcf9b3bd818', 'dir1/file2');
+my $obj3 = $proj->get_object($proj->head_hash);
+isa_ok($obj3, 'Gitalist::Git::Object::Commit');
+
+my $hbp_sha1 = $obj3->sha_by_path('dir1/file2');
 my $obj2 = $proj->get_object($hbp_sha1);
 isa_ok($obj2, 'Gitalist::Git::Object::Blob');
-is($obj2->type, 'blob', 'hash_by_path obj is a file');
-is($obj2->content, "foo\n", 'hash_by_path obj is a file');
+is($obj2->type, 'blob', 'sha_by_path obj is a blob');
+is($obj2->content, "foo\n", 'sha_by_path obj content is correct');
 
-my $obj3 = $proj->get_object($proj->head_hash);
-isa_ok($obj3, 'Gitalist::Git::Object::Commit');
 
 like($proj->head_hash('HEAD'), qr/^([0-9a-fA-F]{40})$/, 'head_hash');