3 class Gitalist::Git::Project with Gitalist::Git::HasUtils {
4 # FIXME, use Types::Path::Class and coerce
5 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
6 use MooseX::Types::Moose qw/Str Maybe Bool HashRef/;
8 use MooseX::Types::Path::Class qw/Dir/;
9 use List::MoreUtils qw/any zip/;
10 use aliased 'Gitalist::Git::Object';
12 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
14 has name => ( isa => NonEmptySimpleStr,
15 is => 'ro', required => 1 );
16 has path => ( isa => Dir,
17 is => 'ro', required => 1);
19 has description => ( isa => Str,
23 has owner => ( isa => NonEmptySimpleStr,
27 has last_change => ( isa => Maybe['DateTime'],
32 has project_dir => ( isa => Dir,
39 : $self->path->subdir('.git')
48 -f $self->path->file('.git', 'HEAD')
50 : -f $self->path->file('HEAD')
52 : confess("Cannot find " . $self->path . "/.git/HEAD or "
53 . $self->path . "/HEAD");
58 $self->$_() for qw/last_change owner description/; # Ensure to build early.
62 -f $self->{path}->file('.git', 'HEAD')
63 ? $self->{path}->subdir('.git')
68 Gitalist::Git::Util->new(
73 method _build_description {
76 $description = $self->project_dir->file('description')->slurp;
83 my ($gecos, $name) = (getpwuid $self->project_dir->stat->uid)[6,0];
85 return length($gecos) ? $gecos : $name;
88 method _build_last_change {
90 my $output = $self->run_cmd(
91 qw{ for-each-ref --format=%(committer)
92 --sort=-committerdate --count=1 refs/heads
94 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
95 my $dt = DateTime->from_epoch(epoch => $epoch);
96 $dt->set_time_zone($tz);
103 my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
104 my @output = $cmdout ? split(/\n/, $cmdout) : ();
106 for my $line (@output) {
107 my ($rev, $head, $commiter) = split /\0/, $line, 3;
108 $head =~ s!^refs/heads/!!;
110 push @ret, { sha1 => $rev, name => $head };
112 #FIXME: That isn't the time I'm looking for..
113 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
114 my $dt = DateTime->from_epoch(epoch => $epoch);
115 $dt->set_time_zone($tz);
116 $ret[-1]->{last_change} = $dt;
124 return $self->{references}
125 if $self->{references};
127 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
128 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
129 my $cmdout = $self->run_cmd(qw(show-ref --dereference))
131 my @reflist = $cmdout ? split(/\n/, $cmdout) : ();
134 push @{$refs{$1}}, $2
135 if m!^($SHA1RE)\srefs/(.*)$!;
138 return $self->{references} = \%refs;
141 method valid_rev (Str $rev) {
142 return ($rev =~ /^($SHA1RE)$/);
148 Find the hash of a given head (defaults to HEAD).
152 method head_hash (Str $head?) {
153 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
154 return unless defined $output;
156 my($sha1) = $output =~ /^($SHA1RE)$/;
162 Return an array of contents for a given tree.
163 The tree is specified by sha1, and defaults to HEAD.
164 The keys for each item will be:
173 method list_tree (Str $sha1?) {
174 $sha1 ||= $self->head_hash;
176 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
177 return unless defined $output;
180 for my $line (split /\0/, $output) {
181 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
182 push @ret, Object->new( mode => oct $mode,
192 method get_object (NonEmptySimpleStr $sha1) {
193 unless ( $self->valid_rev($sha1) ) {
194 $sha1 = $self->head_hash($sha1);
202 # Should be in ::Object
203 method get_object_mode_string (Gitalist::Git::Object $object) {
204 return unless $object && $object->{mode};
205 return $object->{modestr};
208 method get_object_type ($object) {
209 chomp(my $output = $self->run_cmd(qw/cat-file -t/, $object));
210 return unless $output;
215 method cat_file ($object) {
216 my $type = $self->get_object_type($object);
217 die "object `$object' is not a file\n"
218 if (!defined $type || $type ne 'blob');
220 my $output = $self->run_cmd(qw/cat-file -p/, $object);
221 return unless $output;
226 method hash_by_path ($base, $path?, $type?) {
230 my $output = $self->run_cmd('ls-tree', $base, '--', $path)
232 my($line) = $output ? split(/\n/, $output) : ();
234 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
235 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
236 return defined $type && $type ne $2
241 method list_revs ( NonEmptySimpleStr :$sha1!,
245 NonEmptySimpleStr :$file?
247 $sha1 = $self->head_hash($sha1)
248 if !$sha1 || $sha1 !~ $SHA1RE;
252 $search->{type} = 'grep'
253 if $search->{type} eq 'commit';
255 # This seems a little fragile ...
256 qq[--$search->{type}=$search->{text}],
257 '--regexp-ignore-case',
258 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
262 my $output = $self->run_cmd(
265 (defined $count ? "--max-count=$count" : ()),
266 (defined $skip ? "--skip=$skip" : ()),
270 ($file ? $file : ()),
272 return unless $output;
274 my @revs = $self->parse_rev_list($output);
279 method parse_rev_list ($output) {
281 map $self->get_gpp_object($_),
282 grep $self->valid_rev($_),
283 map split(/\n/, $_, 6), split /\0/, $output;
286 # XXX Ideally this would return a wee object instead of ad hoc structures.
287 method diff ( Gitalist::Git::Object :$commit,
289 Maybe[NonEmptySimpleStr] :$parent?,
290 NonEmptySimpleStr :$file? ) {
291 # Use parent if specifed, else take the parent from the commit
292 # if there is only one, otherwise it was a merge commit.
295 : $commit->parents <= 1
296 ? $commit->parent_sha1
299 ( $file ? ('--', $file) : () ),
302 my @out = $self->raw_diff(
303 ( $patch ? '--patch-with-raw' : () ),
304 ( $parent ? $parent : () ),
308 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
309 my @difftree = $self->parse_diff_tree(\@out);
314 # The blank line between the tree and the patch.
317 # XXX And no I'm not happy about having diff return tree + patch.
318 return \@difftree, [$self->parse_diff(@out)];
321 method parse_diff (@diff) {
324 # This regex is a little pathological.
325 if(m{^diff --git (a/(.*?)) (b/\2)}) {
336 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
337 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
341 # XXX Somewhat hacky. Ahem.
342 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
348 # gitweb uses the following sort of command for diffing merges:
349 # /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 --
350 # and for regular diffs
351 # /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 --
353 method raw_diff (@args) {
354 my $cmdout = $self->run_cmd(
355 qw(diff-tree -r -M --no-commit-id --full-index),
358 return $cmdout ? split(/\n/, $cmdout) : ();
361 method parse_diff_tree ($diff) {
362 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
364 while (@$diff and $diff->[0] =~ /^:\d+/) {
365 my $line = shift @$diff;
366 # see. man git-diff-tree for more info
367 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
368 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
369 my %line = zip @keys, @vals;
370 # Some convenience keys
371 $line{file} = $line{src};
372 $line{sha1} = $line{sha1dst};
373 $line{is_new} = $line{sha1src} =~ /^0+$/
375 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
376 if $line{status} =~ /^R/;
383 method reflog (@logargs) {
385 = $self->run_cmd(qw(log -g), @logargs)
386 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
389 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
390 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
392 Author: Foo Barsby <fbarsby@example.com>
393 Date: Thu Sep 17 12:26:05 2009 +0100
395 Merge branch 'abc123'
400 # XXX Stuff like this makes me want to switch to Git::PurePerl
401 my($sha1, $type, $author, $date)
403 ^ commit \s+ ($SHA1RE)$
405 Reflog[ ]message: \s+ (.+?)$ \s+
406 Author: \s+ ([^<]+) <.*?$ \s+
410 pos($_) = index($_, $date) + length $date;
412 # Yeah, I just did that.
413 my($msg) = /\G\s+(\S.*)/sg;
419 # XXX Add DateTime goodness.
431 Returns a hash containing properties of this project. The keys will
435 description (empty if .git/description is empty/unnamed)
444 description => $self->description,
445 owner => $self->owner,
446 last_change => $self->last_change,