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/;
27 use List::MoreUtils qw/any zip/;
29 use aliased 'Gitalist::Git::Object';
37 has name => ( isa => NonEmptySimpleStr,
38 is => 'ro', required => 1 );
42 L<Path::Class:Dir> for the location of the git repository.
46 has path => ( isa => Dir,
47 is => 'ro', required => 1);
51 String containing .git/description
55 has description => ( isa => Str,
62 Owner of the files on disk.
66 has owner => ( isa => NonEmptySimpleStr,
73 L<DateTime> for the time of the last update.
74 undef if the repository has never been used.
78 has last_change => ( isa => Maybe['DateTime'],
85 Bool indicating whether this Project is bare.
89 has is_bare => ( isa => Bool,
93 -d $_[0]->path->parent->subdir->($_[0]->name)
99 $self->$_() for qw/last_change owner description/; # Ensure to build early.
102 around BUILDARGS (ClassName $class: Dir $dir) {
103 # Allows us to be called as Project->new($dir)
104 # Last path component becomes $self->name
105 # Full path to git objects becomes $self->path
106 my $name = $dir->dir_list(-1);
107 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
108 confess("Can't find a git repository at " . $dir)
109 unless ( -f $dir->file('HEAD') );
110 return $class->$orig(name => $name,
114 method _build__util {
115 Gitalist::Git::Util->new(
120 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
122 method _build_description {
123 my $description = "";
125 $description = $self->path->file('description')->slurp;
131 method _build_owner {
132 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
134 return length($gecos) ? $gecos : $name;
137 method _build_last_change {
139 my $output = $self->run_cmd(
140 qw{ for-each-ref --format=%(committer)
141 --sort=-committerdate --count=1 refs/heads
143 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
144 my $dt = DateTime->from_epoch(epoch => $epoch);
145 $dt->set_time_zone($tz);
153 Return an array containing the list of heads.
158 my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
159 my @output = $cmdout ? split(/\n/, $cmdout) : ();
161 for my $line (@output) {
162 my ($rev, $head, $commiter) = split /\0/, $line, 3;
163 $head =~ s!^refs/heads/!!;
165 push @ret, { sha1 => $rev, name => $head };
167 #FIXME: That isn't the time I'm looking for..
168 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
169 my $dt = DateTime->from_epoch(epoch => $epoch);
170 $dt->set_time_zone($tz);
171 $ret[-1]->{last_change} = $dt;
180 Return a hash of references.
184 has references => ( isa => HashRef[Str], is => 'ro', lazy_build => 1 );
186 method _build_references {
188 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
189 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
190 my $cmdout = $self->run_cmd(qw(show-ref --dereference))
192 my @reflist = $cmdout ? split(/\n/, $cmdout) : ();
195 push @{$refs{$1}}, $2
196 if m!^($SHA1RE)\srefs/(.*)$!;
204 Find the hash of a given head (defaults to HEAD).
208 method head_hash (Str $head?) {
209 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
210 return unless defined $output;
212 my($sha1) = $output =~ /^($SHA1RE)$/;
218 Return an array of contents for a given tree.
219 The tree is specified by sha1, and defaults to HEAD.
220 The keys for each item will be:
229 method list_tree (Str $sha1?) {
230 $sha1 ||= $self->head_hash;
232 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
233 return unless defined $output;
236 for my $line (split /\0/, $output) {
237 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
238 push @ret, Object->new( mode => oct $mode,
248 method get_object (NonEmptySimpleStr $sha1) {
249 unless ( $self->_is_valid_rev($sha1) ) {
250 $sha1 = $self->head_hash($sha1);
258 method _is_valid_rev (Str $rev) {
259 return ($rev =~ /^($SHA1RE)$/);
262 # Should be in ::Object
263 method get_object_mode_string (Gitalist::Git::Object $object) {
264 return $object->modestr;
267 method get_object_type (NonEmptySimpleStr $sha1) {
268 return $self->get_object($sha1)->type;
271 method cat_file (NonEmptySimpleStr $sha1) {
272 return $self->get_object($sha1)->contents;
275 method hash_by_path ($base, $path?, $type?) {
279 my $output = $self->run_cmd('ls-tree', $base, '--', $path)
281 my($line) = $output ? split(/\n/, $output) : ();
283 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
284 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
285 return defined $type && $type ne $2
290 method list_revs ( NonEmptySimpleStr :$sha1!,
294 NonEmptySimpleStr :$file?
296 $sha1 = $self->head_hash($sha1)
297 if !$sha1 || $sha1 !~ $SHA1RE;
301 $search->{type} = 'grep'
302 if $search->{type} eq 'commit';
304 # This seems a little fragile ...
305 qq[--$search->{type}=$search->{text}],
306 '--regexp-ignore-case',
307 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
311 my $output = $self->run_cmd(
314 (defined $count ? "--max-count=$count" : ()),
315 (defined $skip ? "--skip=$skip" : ()),
319 ($file ? $file : ()),
321 return unless $output;
323 my @revs = $self->parse_rev_list($output);
328 method parse_rev_list ($output) {
330 map $self->get_gpp_object($_),
331 grep $self->_is_valid_rev($_),
332 map split(/\n/, $_, 6), split /\0/, $output;
335 # XXX Ideally this would return a wee object instead of ad hoc structures.
336 method diff ( Gitalist::Git::Object :$commit,
338 Maybe[NonEmptySimpleStr] :$parent?,
339 NonEmptySimpleStr :$file? ) {
340 # Use parent if specifed, else take the parent from the commit
341 # if there is only one, otherwise it was a merge commit.
344 : $commit->parents <= 1
345 ? $commit->parent_sha1
348 ( $file ? ('--', $file) : () ),
351 my @out = $self->raw_diff(
352 ( $patch ? '--patch-with-raw' : () ),
353 ( $parent ? $parent : () ),
357 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
358 my @difftree = $self->parse_diff_tree(\@out);
363 # The blank line between the tree and the patch.
366 # XXX And no I'm not happy about having diff return tree + patch.
367 return \@difftree, [$self->parse_diff(@out)];
370 method parse_diff (@diff) {
373 # This regex is a little pathological.
374 if(m{^diff --git (a/(.*?)) (b/\2)}) {
385 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
386 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
390 # XXX Somewhat hacky. Ahem.
391 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
397 # gitweb uses the following sort of command for diffing merges:
398 # /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 --
399 # and for regular diffs
400 # /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 --
402 method raw_diff (@args) {
403 my $cmdout = $self->run_cmd(
404 qw(diff-tree -r -M --no-commit-id --full-index),
407 return $cmdout ? split(/\n/, $cmdout) : ();
410 method parse_diff_tree ($diff) {
411 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
413 while (@$diff and $diff->[0] =~ /^:\d+/) {
414 my $line = shift @$diff;
415 # see. man git-diff-tree for more info
416 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
417 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
418 my %line = zip @keys, @vals;
419 # Some convenience keys
420 $line{file} = $line{src};
421 $line{sha1} = $line{sha1dst};
422 $line{is_new} = $line{sha1src} =~ /^0+$/
424 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
425 if $line{status} =~ /^R/;
432 method reflog (@logargs) {
434 = $self->run_cmd(qw(log -g), @logargs)
435 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
437 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
438 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
439 # Reflog message: push
440 # Author: Foo Barsby <fbarsby@example.com>
441 # Date: Thu Sep 17 12:26:05 2009 +0100
443 # Merge branch 'abc123'
446 # XXX Stuff like this makes me want to switch to Git::PurePerl
447 my($sha1, $type, $author, $date)
449 ^ commit \s+ ($SHA1RE)$
451 Reflog[ ]message: \s+ (.+?)$ \s+
452 Author: \s+ ([^<]+) <.*?$ \s+
456 pos($_) = index($_, $date) + length $date;
458 # Yeah, I just did that.
459 my($msg) = /\G\s+(\S.*)/sg;
465 # XXX Add DateTime goodness.
477 Returns a hash containing properties of this project. The keys will
481 description (empty if .git/description is empty/unnamed)
490 description => $self->description,
491 owner => $self->owner,
492 last_change => $self->last_change,
498 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
500 =head1 AUTHORS AND COPYRIGHT
502 Catalyst application:
503 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
505 Original gitweb.cgi from which this was derived:
506 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
507 (C) 2005, Christian Gierke
511 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
513 This library is free software. You can redistribute it and/or modify
514 it under the same terms as Perl itself.