Be able to override the autodetected repo name
[catagits/Gitalist.git] / lib / Gitalist / Git / Repository.pm
index ffc0e89..1da8395 100644 (file)
@@ -15,15 +15,22 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
     use Gitalist::Git::Object::Tree;
     use Gitalist::Git::Object::Commit;
     use Gitalist::Git::Object::Tag;
+    use Gitalist::Git::Head;
+    use Gitalist::Git::Tag;
 
     our $SHA1RE = qr/[0-9a-fA-F]{40}/;
 
-    around BUILDARGS (ClassName $class: Dir $dir) {
+    around BUILDARGS (ClassName $class: Dir $dir, Str $override_name = '') {
         # Allows us to be called as Repository->new($dir)
         # Last path component becomes $self->name
         # Full path to git objects becomes $self->path
-        my $name = $dir->dir_list(-1);
-        $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
+        my $name = ($override_name ne '') ? $override_name : $dir->dir_list(-1);
+        if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git
+            $dir  = $dir->subdir('.git');
+            $name = $dir->dir_list(-2, 1) if (not defined $override_name); # .../name/.git
+        } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
+            $name = $dir->dir_list(-2) if (not defined $override_name);
+        }
         confess("Can't find a git repository at " . $dir)
             unless ( -f $dir->file('HEAD') );
         return $class->$orig(name => $name,
@@ -59,10 +66,10 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
                              ? 1 : 0
                          },
                      );
-    has heads => ( isa => ArrayRef[HashRef],
+    has heads => ( isa => ArrayRef['Gitalist::Git::Head'],
                    is => 'ro',
                    lazy_build => 1);
-    has tags => ( isa => ArrayRef[HashRef],
+    has tags => ( isa => ArrayRef['Gitalist::Git::Tag'],
                    is => 'ro',
                    lazy_build => 1);
     has references => ( isa => HashRef[ArrayRef[Str]],
@@ -75,11 +82,6 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
 
     ## Public methods
 
-    method get_object_or_head (NonEmptySimpleStr $ref) {
-        my $sha1 = is_SHA1($ref) ? $ref : $self->head_hash($ref);
-        $self->get_object($sha1);
-    }
-
     method head_hash (Str $head?) {
         my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
         confess("No such head: " . $head) unless defined $output;
@@ -213,8 +215,8 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
             $description = $self->path->file('description')->slurp;
             chomp $description;
         };
-       $description = "Unnamed repository, edit the .git/description file to set a description"
-           if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
+        $description = "Unnamed repository, edit the .git/description file to set a description"
+            if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
         return $description;
     }
 
@@ -242,19 +244,8 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
         my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
         my @ret;
         for my $line (@revlines) {
-            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;
-            }
+            push @ret, Gitalist::Git::Head->new($line);
         }
-
         return \@ret;
     }
 
@@ -264,24 +255,10 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
           '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
           'refs/tags'
         );
-        my @ret;
-        for my $line (@revlines) {
-            my($refinfo, $creatorinfo) = split /\0/, $line;
-            my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
-            my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
-            $name =~ s!^refs/tags/!!;
-
-            push @ret, { sha1 => $rev, name => $name };
-
-            #FIXME: That isn't the time I'm looking for..
-            if($epoch and $tz) {
-                my $dt = DateTime->from_epoch(epoch => $epoch);
-                $dt->set_time_zone($tz);
-                $ret[-1]->{last_change} = $dt;
-            }
-        }
-
-        return \@ret;
+        return [
+            map  Gitalist::Git::Tag->new($_),
+            grep Gitalist::Git::Tag::is_valid_tag($_), @revlines
+        ];
     }
 
     method _build_references {