5 Gitalist::Git::Project - Model of a git repository
9 my $gitrepo = dir('/repo/base/Gitalist');
10 my $project = Gitalist::Git::Project->new($gitrepo);
11 $project->name; # 'Gitalist'
12 $project->path; # '/repo/base/Gitalist/.git'
13 $project->description; # 'Unnamed repository.'
17 This class models a git repository, referred to in Gitalist
22 class Gitalist::Git::Project with Gitalist::Git::HasUtils {
23 # FIXME, use Types::Path::Class and coerce
24 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
25 use MooseX::Types::Path::Class qw/Dir/;
26 use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/;
27 use List::MoreUtils qw/any zip/;
29 use aliased 'Gitalist::Git::Object';
31 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
33 around BUILDARGS (ClassName $class: Dir $dir) {
34 # Allows us to be called as Project->new($dir)
35 # Last path component becomes $self->name
36 # Full path to git objects becomes $self->path
37 my $name = $dir->dir_list(-1);
38 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
39 confess("Can't find a git repository at " . $dir)
40 unless ( -f $dir->file('HEAD') );
41 return $class->$orig(name => $name,
49 The name of the Project. By default, this is derived from the path to the git repository.
52 has name => ( isa => NonEmptySimpleStr,
53 is => 'ro', required => 1 );
57 L<Path::Class:Dir> for the location of the git repository.
60 has path => ( isa => Dir,
61 is => 'ro', required => 1);
65 String containing .git/description
68 has description => ( isa => Str,
75 Owner of the files on disk.
78 has owner => ( isa => NonEmptySimpleStr,
85 L<DateTime> for the time of the last update.
86 undef if the repository has never been used.
89 has last_change => ( isa => Maybe['DateTime'],
96 Bool indicating whether this Project is bare.
99 has is_bare => ( isa => Bool,
103 -d $_[0]->path->parent->subdir->($_[0]->name)
110 ArrayRef of hashes containing the name and sha1 of all heads.
113 has heads => ( isa => ArrayRef[HashRef],
119 Hashref of ArrayRefs for each reference.
122 has references => ( isa => HashRef[ArrayRef[Str]],
127 $self->$_() for qw/last_change owner description/; # Ensure to build early.
132 =head2 head_hash ($head?)
134 Return the sha1 for HEAD, or any specified head.
137 method head_hash (Str $head?) {
138 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
139 confess("No such head: " . $head) unless defined $output;
141 my($sha1) = $output =~ /^($SHA1RE)$/;
145 =head2 list_tree ($sha1?)
147 Return an array of contents for a given tree.
148 The tree is specified by sha1, and defaults to HEAD.
149 Each item is a L<Gitalist::Git::Object>.
152 method list_tree (Str $sha1?) {
153 $sha1 ||= $self->head_hash;
155 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
156 return unless defined $output;
159 for my $line (split /\0/, $output) {
160 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
161 push @ret, Object->new( mode => oct $mode,
171 =head2 get_object ($sha1)
173 Return a L<Gitalist::Git::Object> for the given sha1.
176 method get_object (NonEmptySimpleStr $sha1) {
177 unless ( $self->_is_valid_rev($sha1) ) {
178 $sha1 = $self->head_hash($sha1);
186 =head2 hash_by_path($sha1, $path, $type?)
188 Returns the sha1 for a given path, optionally limited by type.
191 method hash_by_path ($base, $path = '', $type?) {
193 # FIXME should this really just take the first result?
194 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
196 my $line = $paths[0];
198 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
199 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
200 return defined $type && $type ne $2
205 =head2 list_revs($sha1, $count?, $skip?, \%search?, $file?)
207 Returns a list of revs for the given head ($sha1).
210 method list_revs ( NonEmptySimpleStr :$sha1!,
214 NonEmptySimpleStr :$file?
216 $sha1 = $self->head_hash($sha1)
217 if !$sha1 || $sha1 !~ $SHA1RE;
221 $search->{type} = 'grep'
222 if $search->{type} eq 'commit';
224 # This seems a little fragile ...
225 qq[--$search->{type}=$search->{text}],
226 '--regexp-ignore-case',
227 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
231 my $output = $self->run_cmd(
234 (defined $count ? "--max-count=$count" : ()),
235 (defined $skip ? "--skip=$skip" : ()),
239 ($file ? $file : ()),
241 return unless $output;
243 my @revs = $self->_parse_rev_list($output);
248 =head2 diff($commit, $patch?, $parent?, $file?)
252 FIXME this should be a method on the commit object.
256 # XXX Ideally this would return a wee object instead of ad hoc structures.
257 method diff ( Gitalist::Git::Object :$commit!,
259 Maybe[NonEmptySimpleStr] :$parent?,
260 NonEmptySimpleStr :$file? ) {
261 # Use parent if specifed, else take the parent from the commit
262 # if there is only one, otherwise it was a merge commit.
265 : $commit->parents <= 1
266 ? $commit->parent_sha1
269 ( $file ? ('--', $file) : () ),
272 my @out = $self->_raw_diff(
273 ( $patch ? '--patch-with-raw' : () ),
274 ( $parent ? $parent : () ),
278 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
279 my @difftree = $self->_parse_diff_tree(\@out);
284 # The blank line between the tree and the patch.
287 # XXX And no I'm not happy about having diff return tree + patch.
288 return \@difftree, [$self->_parse_diff(@out)];
291 =head2 reflog(@lorgargs)
293 Return a list of hashes representing each reflog entry.
295 FIXME Should this return objects?
298 method reflog (@logargs) {
300 = $self->run_cmd(qw(log -g), @logargs)
301 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
303 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
304 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
305 # Reflog message: push
306 # Author: Foo Barsby <fbarsby@example.com>
307 # Date: Thu Sep 17 12:26:05 2009 +0100
309 # Merge branch 'abc123'
312 # XXX Stuff like this makes me want to switch to Git::PurePerl
313 my($sha1, $type, $author, $date)
315 ^ commit \s+ ($SHA1RE)$
317 Reflog[ ]message: \s+ (.+?)$ \s+
318 Author: \s+ ([^<]+) <.*?$ \s+
322 pos($_) = index($_, $date) + length $date;
324 # Yeah, I just did that.
325 my($msg) = /\G\s+(\S.*)/sg;
331 # XXX Add DateTime goodness.
340 method _build__util {
341 Gitalist::Git::Util->new(
346 method _build_description {
347 return $self->gpp->description;
350 method _build_owner {
351 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
353 return length($gecos) ? $gecos : $name;
356 method _build_last_change {
358 my $output = $self->run_cmd(
359 qw{ for-each-ref --format=%(committer)
360 --sort=-committerdate --count=1 refs/heads
362 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
363 my $dt = DateTime->from_epoch(epoch => $epoch);
364 $dt->set_time_zone($tz);
370 method _build_heads {
371 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
373 for my $line (@revlines) {
374 my ($rev, $head, $commiter) = split /\0/, $line, 3;
375 $head =~ s!^refs/heads/!!;
377 push @ret, { sha1 => $rev, name => $head };
379 #FIXME: That isn't the time I'm looking for..
380 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
381 my $dt = DateTime->from_epoch(epoch => $epoch);
382 $dt->set_time_zone($tz);
383 $ret[-1]->{last_change} = $dt;
390 method _build_references {
391 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
392 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
393 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
397 push @{$refs{$1}}, $2
398 if m!^($SHA1RE)\srefs/(.*)$!;
405 method _is_valid_rev (Str $rev) {
406 return ($rev =~ /^($SHA1RE)$/);
409 method _parse_rev_list ($output) {
411 map $self->get_gpp_object($_),
412 grep $self->_is_valid_rev($_),
413 map split(/\n/, $_, 6), split /\0/, $output;
416 method _parse_diff_tree ($diff) {
417 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
419 while (@$diff and $diff->[0] =~ /^:\d+/) {
420 my $line = shift @$diff;
421 # see. man git-diff-tree for more info
422 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
423 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
424 my %line = zip @keys, @vals;
425 # Some convenience keys
426 $line{file} = $line{src};
427 $line{sha1} = $line{sha1dst};
428 $line{is_new} = $line{sha1src} =~ /^0+$/
430 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
431 if $line{status} =~ /^R/;
437 method _parse_diff (@diff) {
440 # This regex is a little pathological.
441 if(m{^diff --git (a/(.*?)) (b/\2)}) {
452 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
453 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
457 # XXX Somewhat hacky. Ahem.
458 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
464 # gitweb uses the following sort of command for diffing merges:
465 # /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 --
466 # and for regular diffs
467 # /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 --
468 method _raw_diff (@args) {
469 return $self->run_cmd_list(
470 qw(diff-tree -r -M --no-commit-id --full-index),
477 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
479 =head1 AUTHORS AND COPYRIGHT
481 Catalyst application:
482 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
484 Original gitweb.cgi from which this was derived:
485 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
486 (C) 2005, Christian Gierke
490 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
492 This library is free software. You can redistribute it and/or modify
493 it under the same terms as Perl itself.