Revert "Merge remote branch 't0m/json' into json"
[catagits/Gitalist.git] / lib / Gitalist / Git / Object.pm
index 544b473..31b07d6 100644 (file)
@@ -4,10 +4,9 @@ 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',
+    # repository and sha1 are required initargs
+    has repository => ( isa => 'Gitalist::Git::Repository',
                      required => 1,
                      is => 'ro',
                      weak_ref => 1,
@@ -36,8 +35,7 @@ class Gitalist::Git::Object {
                       required => 1,
                       is => 'ro',
                       lazy_build => 1,
-                      handles => [ 'content',
-                               ],
+                      handles => [ 'content' ],
                   );
 
     # objects can't determine their mode or filename
@@ -64,17 +62,69 @@ 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
 
+__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