Win32 does not have reliable symlinks
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
index ff33b8d..7d46632 100644 (file)
 use MooseX::Declare;
 use Moose::Autobox;
 
-class Gitalist::Git::Object {
-    use MooseX::Types::Moose qw/Str Int/;
+class Gitalist::Git::Object with Gitalist::Git::Serializable is dirty {
+    use MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize;
+
+    use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
-    use File::Stat::ModeString qw/mode_to_string/;
-    # project and sha1 are required initargs
-    has project => ( isa => 'Gitalist::Git::Project',
+    use overload '""' => '_to_string', fallback => 1;
+
+    # repository and sha1 are required initargs
+    has repository => ( isa => 'Gitalist::Git::Repository',
                      required => 1,
                      is => 'ro',
                      weak_ref => 1,
                      handles => {
                          _run_cmd => 'run_cmd',
+                         _run_cmd_fh => 'run_cmd_fh',
+                         _run_cmd_list => 'run_cmd_list',
                          _get_gpp_object => 'get_gpp_object',
                      },
                  );
     has sha1 => ( isa => NonEmptySimpleStr,
-               required => 1,
-               is => 'ro' );
-
-    has $_ => ( isa => NonEmptySimpleStr,
                   required => 1,
+                  is => 'ro' );
+
+    has type => ( isa => NonEmptySimpleStr,
                   is => 'ro',
-                  lazy_build => 1 )
-        for qw/type modestr size/;
+                  required => 1 );
 
-    has _gpp_obj => ( isa => 'Git::PurePerl::Object',
-                      required => 1,
-                      is => 'ro',
-                      lazy_build => 1,
-                      handles => [ 'parents',
-                                   'parent_sha1',
-                                   'author',
-                                   'authored_time',
-                                   'committer',
-                                   'committed_time',
-                               ],
-                  );
+    has $_ => ( isa => NonEmptySimpleStr,
+                required => 1,
+                is => 'ro',
+                lazy_build => 1 )
+        for qw/modestr size/;
 
-    # This feels wrong, but current templates assume
-    # these attributes are present on every object.
-    foreach my $key (qw/tree_sha1 comment/) {
-        has $key => ( isa => Str,
-                      required => 1,
-                      is => 'ro',
+    has _gpp_obj => ( isa        => 'Git::PurePerl::Object',
+                      required   => 1,
+                      is         => 'ro',
                       lazy_build => 1,
+                      handles    => [ 'content' ],
+                      traits     => ['DoNotSerialize']
                   );
-        method "_build_$key" {
-            return '' unless $self->_gpp_obj->can($key);
-            return $self->_gpp_obj->$key;
-        }
-    }
 
     # objects can't determine their mode or filename
     has file => ( isa => NonEmptySimpleStr,
                   required => 0,
                   is => 'ro' );
     has mode => ( isa => Int,
-                required => 1,
-                default => 0,
-                is => 'ro' );
+                  required => 1,
+                  default => 0,
+                  is => 'ro' );
+
+    method BUILD { $self->$_() for qw/_gpp_obj size modestr/ }
 
-    method BUILD { $self->$_() for qw/_gpp_obj type size modestr/ }
+## Private methods
+    method _to_string {
+        return $self->sha1;
+    };
 
+## Builders
     method _build__gpp_obj {
         return $self->_get_gpp_object($self->sha1)
     }
 
-    foreach my $key (qw/ type size /) {
-        method "_build_$key" {
-            my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
-            chomp($v);
-            return $v;
-        }
-    }
-
-    method _build_modestr {
-        my $modestr = mode_to_string($self->mode);
-        return $modestr;
+    method "_build_size" {
+        my $v = $self->_cat_file_with_flag('s');
+        chomp($v);
+        return $v;
     }
 
     method _cat_file_with_flag ($flag) {
         $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
     }
 
-=head2 contents
+    method _build_modestr {
+        return _mode_str($self->mode);
+    }
 
-Return the contents of a given file.
+    # via gitweb.pm circa line 1305
+    use Fcntl ':mode';
+    use constant {
+        S_IFINVALID => 0030000,
+        S_IFGITLINK => 0160000,
+    };
 
-=cut
+    # submodule/subrepository, a commit object reference
+    sub S_ISGITLINK($) {
+        return (($_[0] & S_IFMT) == S_IFGITLINK)
+    }
 
-    # FIXME - Should be an attribute so it gets cached?
-    method contents {
-        if ( $self->type ne 'blob' ) {
-            die "object $self->sha1 is not a file\n"
+    # convert file mode in octal to symbolic file mode string
+    sub _mode_str {
+        my $mode = shift;
+
+        if (S_ISGITLINK($mode)) {
+            return 'm---------';
+        } elsif (S_ISDIR($mode & S_IFMT)) {
+            return 'drwxr-xr-x';
+        } elsif ($^O ne 'MSWin32' and S_ISLNK($mode)) { # this is ENOLINKS country, we can't stop here!
+            return 'lrwxrwxrwx';
+        } elsif (S_ISREG($mode)) {
+            # git cares only about the executable bit
+            if ($mode & S_IXUSR) {
+                return '-rwxr-xr-x';
+            } else {
+                return '-rw-r--r--';
+            }
+        } else {
+            return '----------';
         }
-
-        $self->_cat_file_with_flag('p');
     }
 
 } # end class
+
+__END__
+
+=head1 NAME
+
+Gitalist::Git::Object - Model of a git object.
+
+=head1 SYNOPSIS
+
+    my $object = Repository->get_object($sha1);
+
+=head1 DESCRIPTION
+
+Abstract base class for git objects.
+
+
+=head1 ATTRIBUTES
+
+
+=head1 METHODS
+
+
+=head1 AUTHORS
+
+See L<Gitalist> for authors.
+
+=head1 LICENSE
+
+See L<Gitalist> for the license.
+
+=cut