Ignore ancient/malformed tags.
Dan Brook [Sun, 20 Feb 2011 12:40:57 +0000 (12:40 +0000)]
Previously Gitalist::Git::Tag::BUILDARGS would croak when constructing
DateTime for wonky tags i.e d6602ec5194c87b0fc87103ca4d67251c76f233a
(the first tag in the git repo). Now we just ignore them because I
think it's safe to presume they are very much the exception.

lib/Gitalist/Git/Repository.pm
lib/Gitalist/Git/Tag.pm
root/repository/tags.tt2
t/02git_tag.t

index 571656e..2832505 100644 (file)
@@ -25,12 +25,12 @@ class Gitalist::Git::Repository with Gitalist::Git::HasUtils {
         # Last path component becomes $self->name
         # Full path to git objects becomes $self->path
         my $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); # .../name/.git
-       } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
-           $name = $dir->dir_list(-2);
-       }
+        if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git
+            $dir  = $dir->subdir('.git');
+            $name = $dir->dir_list(-2, 1); # .../name/.git
+        } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
+            $name = $dir->dir_list(-2);
+        }
         confess("Can't find a git repository at " . $dir)
             unless ( -f $dir->file('HEAD') );
         return $class->$orig(name => $name,
@@ -215,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;
     }
 
@@ -255,11 +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) {
-            push @ret, Gitalist::Git::Tag->new($line);
-        }
-        return \@ret;
+        return [
+            map  Gitalist::Git::Tag->new($_),
+            grep Gitalist::Git::Tag::is_valid_tag($_), @revlines
+        ];
     }
 
     method _build_references {
index be53088..a0a8ea6 100644 (file)
@@ -76,4 +76,10 @@ around BUILDARGS => sub {
     }
 };
 
+sub is_valid_tag {
+    local $_ = pop;
+    # Ignore tags like - http://git.kernel.org/?p=git/git.git;a=tag;h=d6602ec
+    return /^\S+ \S+ \S+ (?:\S+)? (?:\S+)?[^\0]+\0.*\s\d+\s+[+-]\d+$/;
+}
+
 1;
index 3518081..c9b0567 100755 (executable)
@@ -1,7 +1 @@
-<div class='content'>
-  <div>
-  [% Repository.name %]
-  </div>
-
-  [% subinclude('/fragment/repository/tags', c.req.captures) %]
-</div>
+[% subinclude('/fragment/repository/tags', c.req.captures) %]
index baa9983..350e9e5 100644 (file)
@@ -13,12 +13,15 @@ use Data::Dumper;
 
 BEGIN { use_ok 'Gitalist::Git::Tag' }
 
+my $ref_sha1 = '3f7567c7bdf7e7ebf410926493b92d398333116e';
 # Create an instance from for-each-ref output
-my $revline="36c6c6708b8360d7023e8a1649c45bcf9b3bd818 commit refs/tags/0.01   add dir1/file2\0Florian Ragwitz <rafl\@debian.org> 1173210275 +0100";
-#my $revline="a92fb1c9282f7319099ce7f783c8be7d5360f6e3\0refs/heads/model-cleanup\0Zachary Stevens <zts\@cryptocracy.com> 1277601094 +0100";
+my $revline="36c6c6708b8360d7023e8a1649c45bcf9b3bd818 commit refs/tags/0.01 $ref_sha1 add dir1/file2\0Florian Ragwitz <rafl\@debian.org> 1173210275 +0100";
+
 my $instance = Gitalist::Git::Tag->new($revline);
 isa_ok($instance, 'Gitalist::Git::Tag');
 
+ok($instance->$_, $_) for $instance->meta->get_attribute_list;
+
 # Create an instance, passing last_change as a DateTime
 use DateTime;
 my $timespec = [1173210275, '+0100'];
@@ -34,3 +37,12 @@ my $head = Gitalist::Git::Tag->new(
     last_change => $dt,
 );
 isa_ok($head, 'Gitalist::Git::Tag');
+
+my $oldtag = "d6602ec5194c87b0fc87103ca4d67251c76f233a tag refs/tags/v0.99 a3eb250f996bf5e12376ec88622c4ccaabf20ea8 commit Test-release for wider distribution.";
+
+ok(!Gitalist::Git::Tag::is_valid_tag($oldtag), 'Gitalist::Git::Tag::is_valid_tag ancient tag');
+ok(Gitalist::Git::Tag::is_valid_tag($revline), 'Gitalist::Git::Tag::is_valid_tag regular tag');
+
+# Tags don't necessarily have a refname, check we deal with its absence.
+$revline =~ s/$ref_sha1//;
+ok(Gitalist::Git::Tag::is_valid_tag($revline), 'Gitalist::Git::Tag::is_valid_tag regular tag sans ref sha1');