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 my $name = $dir->dir_list(-1);
35 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
36 confess("Can't find a git repository at " . $dir)
37 unless ( -f $dir->file('HEAD') );
38 return $class->$orig(name => $name,
46 The name of the Project. By default, this is derived from the path to the git repository.
49 has name => ( isa => NonEmptySimpleStr,
50 is => 'ro', required => 1 );
54 L<Path::Class:Dir> for the location of the git repository.
57 has path => ( isa => Dir,
58 is => 'ro', required => 1);
62 String containing .git/description
65 has description => ( isa => Str,
72 Owner of the files on disk.
75 has owner => ( isa => NonEmptySimpleStr,
82 L<DateTime> for the time of the last update.
83 undef if the repository has never been used.
86 has last_change => ( isa => Maybe['DateTime'],
93 Bool indicating whether this Project is bare.
96 has is_bare => ( isa => Bool,
100 -d $_[0]->path->parent->subdir->($_[0]->name)
107 ArrayRef of hashes containing the name and sha1 of all heads.
110 has heads => ( isa => ArrayRef[HashRef],
116 Hashref of ArrayRefs for each reference.
119 has references => ( isa => HashRef[ArrayRef[Str]],
124 $self->$_() for qw/last_change owner description/; # Ensure to build early.
129 =head2 head_hash ($head?)
131 Return the sha1 for HEAD, or any specified head.
134 method head_hash (Str $head?) {
135 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
136 confess("No such head: " . $head) unless defined $output;
138 my($sha1) = $output =~ /^($SHA1RE)$/;
142 =head2 list_tree ($sha1?)
144 Return an array of contents for a given tree.
145 The tree is specified by sha1, and defaults to HEAD.
146 Each item is a L<Gitalist::Git::Object>.
149 method list_tree (Str $sha1?) {
150 $sha1 ||= $self->head_hash;
152 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
153 return unless defined $output;
156 for my $line (split /\0/, $output) {
157 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
158 push @ret, Object->new( mode => oct $mode,
168 =head2 get_object ($sha1)
170 Return a L<Gitalist::Git::Object> for the given sha1.
173 method get_object (NonEmptySimpleStr $sha1) {
174 unless ( $self->_is_valid_rev($sha1) ) {
175 $sha1 = $self->head_hash($sha1);
183 =head2 hash_by_path($sha1, $path, $type?)
185 Returns the sha1 for a given path, optionally limited by type.
188 method hash_by_path ($base, $path = '', $type?) {
190 # FIXME should this really just take the first result?
191 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
193 my $line = $paths[0];
195 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
196 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
197 return defined $type && $type ne $2
202 =head2 list_revs($sha1, $count?, $skip?, \%search?, $file?)
204 Returns a list of revs for the given head ($sha1).
207 method list_revs ( NonEmptySimpleStr :$sha1!,
211 NonEmptySimpleStr :$file?
213 $sha1 = $self->head_hash($sha1)
214 if !$sha1 || $sha1 !~ $SHA1RE;
218 $search->{type} = 'grep'
219 if $search->{type} eq 'commit';
221 # This seems a little fragile ...
222 qq[--$search->{type}=$search->{text}],
223 '--regexp-ignore-case',
224 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
228 my $output = $self->run_cmd(
231 (defined $count ? "--max-count=$count" : ()),
232 (defined $skip ? "--skip=$skip" : ()),
236 ($file ? $file : ()),
238 return unless $output;
240 my @revs = $self->_parse_rev_list($output);
245 =head2 diff($commit, $patch?, $parent?, $file?)
249 FIXME this should be a method on the commit object.
253 # XXX Ideally this would return a wee object instead of ad hoc structures.
254 method diff ( Gitalist::Git::Object :$commit!,
256 Maybe[NonEmptySimpleStr] :$parent?,
257 NonEmptySimpleStr :$file? ) {
258 # Use parent if specifed, else take the parent from the commit
259 # if there is only one, otherwise it was a merge commit.
262 : $commit->parents <= 1
263 ? $commit->parent_sha1
266 ( $file ? ('--', $file) : () ),
269 my @out = $self->_raw_diff(
270 ( $patch ? '--patch-with-raw' : () ),
271 ( $parent ? $parent : () ),
275 # XXX Yes, there is much wrongness having _parse_diff_tree be destructive.
276 my @difftree = $self->_parse_diff_tree(\@out);
281 # The blank line between the tree and the patch.
284 # XXX And no I'm not happy about having diff return tree + patch.
285 return \@difftree, [$self->_parse_diff(@out)];
288 =head2 reflog(@lorgargs)
290 Return a list of hashes representing each reflog entry.
292 FIXME Should this return objects?
295 method reflog (@logargs) {
297 = $self->run_cmd(qw(log -g), @logargs)
298 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
300 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
301 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
302 # Reflog message: push
303 # Author: Foo Barsby <fbarsby@example.com>
304 # Date: Thu Sep 17 12:26:05 2009 +0100
306 # Merge branch 'abc123'
309 # XXX Stuff like this makes me want to switch to Git::PurePerl
310 my($sha1, $type, $author, $date)
312 ^ commit \s+ ($SHA1RE)$
314 Reflog[ ]message: \s+ (.+?)$ \s+
315 Author: \s+ ([^<]+) <.*?$ \s+
319 pos($_) = index($_, $date) + length $date;
321 # Yeah, I just did that.
322 my($msg) = /\G\s+(\S.*)/sg;
328 # XXX Add DateTime goodness.
337 method _build__util {
338 Gitalist::Git::Util->new(
343 method _build_description {
344 my $description = "";
346 $description = $self->path->file('description')->slurp;
352 method _build_owner {
353 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
355 return length($gecos) ? $gecos : $name;
358 method _build_last_change {
360 my $output = $self->run_cmd(
361 qw{ for-each-ref --format=%(committer)
362 --sort=-committerdate --count=1 refs/heads
364 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
365 my $dt = DateTime->from_epoch(epoch => $epoch);
366 $dt->set_time_zone($tz);
372 method _build_heads {
373 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
375 for my $line (@revlines) {
376 my ($rev, $head, $commiter) = split /\0/, $line, 3;
377 $head =~ s!^refs/heads/!!;
379 push @ret, { sha1 => $rev, name => $head };
381 #FIXME: That isn't the time I'm looking for..
382 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
383 my $dt = DateTime->from_epoch(epoch => $epoch);
384 $dt->set_time_zone($tz);
385 $ret[-1]->{last_change} = $dt;
392 method _build_references {
393 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
394 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
395 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
399 push @{$refs{$1}}, $2
400 if m!^($SHA1RE)\srefs/(.*)$!;
407 method _is_valid_rev (Str $rev) {
408 return ($rev =~ /^($SHA1RE)$/);
411 method _parse_rev_list ($output) {
413 map $self->get_gpp_object($_),
414 grep $self->_is_valid_rev($_),
415 map split(/\n/, $_, 6), split /\0/, $output;
418 method _parse_diff_tree ($diff) {
419 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
421 while (@$diff and $diff->[0] =~ /^:\d+/) {
422 my $line = shift @$diff;
423 # see. man git-diff-tree for more info
424 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
425 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
426 my %line = zip @keys, @vals;
427 # Some convenience keys
428 $line{file} = $line{src};
429 $line{sha1} = $line{sha1dst};
430 $line{is_new} = $line{sha1src} =~ /^0+$/
432 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
433 if $line{status} =~ /^R/;
439 method _parse_diff (@diff) {
442 # This regex is a little pathological.
443 if(m{^diff --git (a/(.*?)) (b/\2)}) {
454 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
455 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
459 # XXX Somewhat hacky. Ahem.
460 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
466 # gitweb uses the following sort of command for diffing merges:
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 --cc 316cf158df3f6207afbae7270bcc5ba0 --
468 # and for regular diffs
469 # /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 --
470 method _raw_diff (@args) {
471 return $self->run_cmd_list(
472 qw(diff-tree -r -M --no-commit-id --full-index),
479 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
481 =head1 AUTHORS AND COPYRIGHT
483 Catalyst application:
484 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
486 Original gitweb.cgi from which this was derived:
487 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
488 (C) 2005, Christian Gierke
492 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
494 This library is free software. You can redistribute it and/or modify
495 it under the same terms as Perl itself.