4 class Gitalist::Git::Object {
5 use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
6 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7 use File::Stat::ModeString qw/mode_to_string/;
8 use List::MoreUtils qw/any zip/;
10 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
12 # project and sha1 are required initargs
13 has project => ( isa => 'Gitalist::Git::Project',
18 _run_cmd => 'run_cmd',
19 _run_cmd_list => 'run_cmd_list',
20 _get_gpp_object => 'get_gpp_object',
23 has sha1 => ( isa => NonEmptySimpleStr,
27 has $_ => ( isa => NonEmptySimpleStr,
31 for qw/type modestr size/;
33 has _gpp_obj => ( isa => 'Git::PurePerl::Object',
37 handles => [ 'parents',
43 # This feels wrong, but current templates assume
44 # these attributes are present on every object.
45 foreach my $key (qw/content/) {
46 has $key => ( isa => Str,
51 method "_build_$key" {
52 confess("Object can't " . $key) unless $self->_gpp_obj->can($key);
53 return $self->_gpp_obj->$key;
57 # objects can't determine their mode or filename
58 has file => ( isa => NonEmptySimpleStr,
61 has mode => ( isa => Int,
66 has tree => ( isa => 'ArrayRef[Gitalist::Git::Object]',
71 method BUILD { $self->$_() for qw/_gpp_obj type size modestr/ }
74 confess("Can't list_tree on a blob object.")
75 if $self->type eq 'blob';
76 my $output = $self->_run_cmd(qw/ls-tree -z/, $self->sha1);
77 return unless defined $output;
80 for my $line (split /\0/, $output) {
81 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
82 push @ret, Gitalist::Git::Object->new( mode => oct $mode,
86 project => $self->project,
92 method diff ( Maybe[Bool] :$patch?,
93 Maybe[NonEmptySimpleStr] :$parent?,
94 Maybe[NonEmptySimpleStr] :$file?
96 # Use parent if specifed, else take the parent from the commit
97 # if there is only one, otherwise it was a merge commit.
100 : $self->parents <= 1
104 ( $file ? ('--', $file) : () ),
107 my @out = $self->_raw_diff(
108 ( $patch ? '--patch-with-raw' : () ),
109 ( $parent ? $parent : () ),
113 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
114 my @difftree = $self->_parse_diff_tree(\@out);
119 # The blank line between the tree and the patch.
122 # XXX And no I'm not happy about having diff return tree + patch.
123 return \@difftree, [$self->_parse_diff(@out)];
127 # gitweb uses the following sort of command for diffing merges:
128 # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index --cc 316cf158df3f6207afbae7270bcc5ba0 --
129 # and for regular diffs
130 # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index 2e3454ca0749641b42f063730b0090e1 316cf158df3f6207afbae7270bcc5ba0 --
131 method _raw_diff (@args) {
132 return $self->_run_cmd_list(
133 qw(diff-tree -r -M --no-commit-id --full-index),
138 method _parse_diff_tree ($diff) {
139 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
141 while (@$diff and $diff->[0] =~ /^:\d+/) {
142 my $line = shift @$diff;
143 # see. man git-diff-tree for more info
144 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
145 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
146 my %line = zip @keys, @vals;
147 # Some convenience keys
148 $line{file} = $line{src};
149 $line{sha1} = $line{sha1dst};
150 $line{is_new} = $line{sha1src} =~ /^0+$/
152 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
153 if $line{status} =~ /^R/;
160 method _parse_diff (@diff) {
163 # This regex is a little pathological.
164 if (m{^diff --git (a/(.*?)) (b/\2)}) {
175 if (/^index (\w+)\.\.(\w+) (\d+)$/) {
176 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
180 # XXX Somewhat hacky. Ahem.
181 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
189 method _build__gpp_obj {
190 return $self->_get_gpp_object($self->sha1)
193 foreach my $key (qw/ type size /) {
194 method "_build_$key" {
195 my $v = $self->_cat_file_with_flag(substr($key, 0, 1));
201 method _build_modestr {
202 my $modestr = mode_to_string($self->mode);
206 method _cat_file_with_flag ($flag) {
207 $self->_run_cmd('cat-file', '-' . $flag, $self->{sha1})