Added commit object serialization.
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
index c1a5194..78c5448 100644 (file)
@@ -4,10 +4,11 @@ use Moose::Autobox;
 class Gitalist::Git::Object {
     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',
+    with 'Gitalist::Serializeable';
+
+    # repository and sha1 are required initargs
+    has repository => ( isa => 'Gitalist::Git::Repository',
                      required => 1,
                      is => 'ro',
                      weak_ref => 1,
@@ -36,8 +37,8 @@ class Gitalist::Git::Object {
                       required => 1,
                       is => 'ro',
                       lazy_build => 1,
-                      handles => [ 'content',
-                               ],
+                      handles => [ 'content' ],
+                      traits => [qw/ DoNotSerialize /],
                   );
 
     # objects can't determine their mode or filename
@@ -64,13 +65,46 @@ class Gitalist::Git::Object {
         return $v;
     }
 
+    method _cat_file_with_flag ($flag) {
+        $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
+    }
+
     method _build_modestr {
-        my $modestr = mode_to_string($self->mode);
-        return $modestr;
+        return _mode_str($self->mode);
     }
 
-    method _cat_file_with_flag ($flag) {
-        $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})
+    # via gitweb.pm circa line 1305
+    use Fcntl ':mode';
+    use constant {
+        S_IFINVALID => 0030000,
+        S_IFGITLINK => 0160000,
+    };
+
+    # submodule/subrepository, a commit object reference
+    sub S_ISGITLINK($) {
+        return (($_[0] & S_IFMT) == S_IFGITLINK)
+    }
+
+    # 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 (S_ISLNK($mode)) {
+            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 '----------';
+        }
     }
 
 } # end class
@@ -83,7 +117,7 @@ Gitalist::Git::Object - Model of a git object.
 
 =head1 SYNOPSIS
 
-    my $object = Project->get_object($sha1);
+    my $object = Repository->get_object($sha1);
 
 =head1 DESCRIPTION