3 class Gitalist::Git::Project {
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 Gitalist::Git::Util;
11 use aliased 'Gitalist::Git::Object';
13 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
15 has name => ( isa => NonEmptySimpleStr,
16 is => 'ro', required => 1 );
17 has path => ( isa => Dir,
18 is => 'ro', required => 1);
20 has description => ( isa => Str,
24 has owner => ( isa => NonEmptySimpleStr,
28 has last_change => ( isa => Maybe['DateTime'],
32 has _util => ( isa => 'Gitalist::Git::Util',
35 handles => [ 'run_cmd', 'get_gpp_object' ],
38 has project_dir => ( isa => Dir,
45 : $self->path->subdir('.git')
54 -f $self->path->file('.git', 'HEAD')
56 : -f $self->path->file('HEAD')
58 : confess("Cannot find " . $self->path . "/.git/HEAD or "
59 . $self->path . "/HEAD");
64 $self->$_() for qw/_util last_change owner description/; # Ensure to build early.
68 -f $self->{path}->file('.git', 'HEAD')
69 ? $self->{path}->subdir('.git')
74 Gitalist::Git::Util->new(
79 method _build_description {
82 $description = $self->project_dir->file('description')->slurp;
89 my ($gecos, $name) = (getpwuid $self->project_dir->stat->uid)[6,0];
91 return length($gecos) ? $gecos : $name;
94 method _build_last_change {
96 my $output = $self->run_cmd(
97 qw{ for-each-ref --format=%(committer)
98 --sort=-committerdate --count=1 refs/heads
100 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
101 my $dt = DateTime->from_epoch(epoch => $epoch);
102 $dt->set_time_zone($tz);
109 my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
110 my @output = $cmdout ? split(/\n/, $cmdout) : ();
112 for my $line (@output) {
113 my ($rev, $head, $commiter) = split /\0/, $line, 3;
114 $head =~ s!^refs/heads/!!;
116 push @ret, { sha1 => $rev, name => $head };
118 #FIXME: That isn't the time I'm looking for..
119 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
120 my $dt = DateTime->from_epoch(epoch => $epoch);
121 $dt->set_time_zone($tz);
122 $ret[-1]->{last_change} = $dt;
130 return $self->{references}
131 if $self->{references};
133 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
134 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
135 my $cmdout = $self->run_cmd(qw(show-ref --dereference))
137 my @reflist = $cmdout ? split(/\n/, $cmdout) : ();
140 push @{$refs{$1}}, $2
141 if m!^($SHA1RE)\srefs/(.*)$!;
144 return $self->{references} = \%refs;
147 method valid_rev (Str $rev) {
148 return ($rev =~ /^($SHA1RE)$/);
154 Find the hash of a given head (defaults to HEAD).
158 method head_hash (Str $head?) {
159 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
160 return unless defined $output;
162 my($sha1) = $output =~ /^($SHA1RE)$/;
168 Return an array of contents for a given tree.
169 The tree is specified by sha1, and defaults to HEAD.
170 The keys for each item will be:
179 method list_tree (Str $sha1?) {
180 $sha1 ||= $self->head_hash;
182 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
183 return unless defined $output;
186 for my $line (split /\0/, $output) {
187 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
188 push @ret, Object->new( mode => oct $mode,
198 method get_object (Str $sha1) {
205 # Should be in ::Object
206 method get_object_mode_string (Gitalist::Git::Object $object) {
207 return unless $object && $object->{mode};
208 return $object->{modestr};
211 method get_object_type ($object) {
212 chomp(my $output = $self->run_cmd(qw/cat-file -t/, $object));
213 return unless $output;
218 method cat_file ($object) {
219 my $type = $self->get_object_type($object);
220 die "object `$object' is not a file\n"
221 if (!defined $type || $type ne 'blob');
223 my $output = $self->run_cmd(qw/cat-file -p/, $object);
224 return unless $output;
229 method hash_by_path ($base, $path?, $type?) {
233 my $output = $self->run_cmd('ls-tree', $base, '--', $path)
235 my($line) = $output ? split(/\n/, $output) : ();
237 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
238 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
239 return defined $type && $type ne $2
244 method list_revs ( NonEmptySimpleStr :$sha1!,
248 NonEmptySimpleStr :$file?
250 $sha1 = $self->head_hash($sha1)
251 if !$sha1 || $sha1 !~ $SHA1RE;
255 $search->{type} = 'grep'
256 if $search->{type} eq 'commit';
258 # This seems a little fragile ...
259 qq[--$search->{type}=$search->{text}],
260 '--regexp-ignore-case',
261 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
265 my $output = $self->run_cmd(
268 (defined $count ? "--max-count=$count" : ()),
269 (defined $skip ? "--skip=$skip" : ()),
273 ($file ? $file : ()),
275 return unless $output;
277 my @revs = $self->parse_rev_list($output);
282 method parse_rev_list ($output) {
284 map $self->get_gpp_object($_),
285 grep $self->valid_rev($_),
286 map split(/\n/, $_, 6), split /\0/, $output;
289 # XXX Ideally this would return a wee object instead of ad hoc structures.
290 method diff ( Gitalist::Git::Object :$commit,
292 Maybe[NonEmptySimpleStr] :$parent?,
293 NonEmptySimpleStr :$file? ) {
294 # Use parent if specifed, else take the parent from the commit
295 # if there is only one, otherwise it was a merge commit.
298 : $commit->parents <= 1
299 ? $commit->parent_sha1
302 ( $file ? ('--', $file) : () ),
305 my @out = $self->raw_diff(
306 ( $patch ? '--patch-with-raw' : () ),
307 $parent, $commit->sha1, @etc
310 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
311 my @difftree = $self->parse_diff_tree(\@out);
316 # The blank line between the tree and the patch.
319 # XXX And no I'm not happy about having diff return tree + patch.
320 return \@difftree, [$self->parse_diff(@out)];
323 method parse_diff (@diff) {
326 # This regex is a little pathological.
327 if(m{^diff --git (a/(.*?)) (b/\2)}) {
338 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
339 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
343 # XXX Somewhat hacky. Ahem.
344 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
350 # gitweb uses the following sort of command for diffing merges:
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 --cc 316cf158df3f6207afbae7270bcc5ba0 --
352 # and for regular diffs
353 # /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 --
355 method raw_diff (@args) {
356 my $cmdout = $self->run_cmd(
357 qw(diff-tree -r -M --no-commit-id --full-index),
360 return $cmdout ? split(/\n/, $cmdout) : ();
363 method parse_diff_tree ($diff) {
364 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
366 while (@$diff and $diff->[0] =~ /^:\d+/) {
367 my $line = shift @$diff;
368 # see. man git-diff-tree for more info
369 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
370 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
371 my %line = zip @keys, @vals;
372 # Some convenience keys
373 $line{file} = $line{src};
374 $line{sha1} = $line{sha1dst};
375 $line{is_new} = $line{sha1src} =~ /^0+$/
377 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
378 if $line{status} =~ /^R/;
385 method reflog (@logargs) {
387 = $self->run_cmd(qw(log -g), @logargs)
388 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
391 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
392 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
394 Author: Foo Barsby <fbarsby@example.com>
395 Date: Thu Sep 17 12:26:05 2009 +0100
397 Merge branch 'abc123'
402 # XXX Stuff like this makes me want to switch to Git::PurePerl
403 my($sha1, $type, $author, $date)
405 ^ commit \s+ ($SHA1RE)$
407 Reflog[ ]message: \s+ (.+?)$ \s+
408 Author: \s+ ([^<]+) <.*?$ \s+
412 pos($_) = index($_, $date) + length $date;
414 # Yeah, I just did that.
415 my($msg) = /\G\s+(\S.*)/sg;
421 # XXX Add DateTime goodness.
433 Returns a hash containing properties of this project. The keys will
437 description (empty if .git/description is empty/unnamed)
446 description => $self->description,
447 owner => $self->owner,
448 last_change => $self->last_change,