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,
48 has name => ( isa => NonEmptySimpleStr,
49 is => 'ro', required => 1 );
53 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
66 has description => ( isa => Str,
73 Owner of the files on disk.
77 has owner => ( isa => NonEmptySimpleStr,
84 L<DateTime> for the time of the last update.
85 undef if the repository has never been used.
89 has last_change => ( isa => Maybe['DateTime'],
96 Bool indicating whether this Project is bare.
100 has is_bare => ( isa => Bool,
104 -d $_[0]->path->parent->subdir->($_[0]->name)
110 $self->$_() for qw/last_change owner description/; # Ensure to build early.
117 Return the sha1 for HEAD, or any specified head.
121 method head_hash (Str $head?) {
122 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
123 confess("No such head: " . $head) unless defined $output;
125 my($sha1) = $output =~ /^($SHA1RE)$/;
132 ArrayRef of hashes containing the name and sha1 of all heads.
136 has heads => ( isa => ArrayRef[HashRef], is => 'ro', lazy_build => 1);
138 method _build_heads {
139 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
141 for my $line (@revlines) {
142 my ($rev, $head, $commiter) = split /\0/, $line, 3;
143 $head =~ s!^refs/heads/!!;
145 push @ret, { sha1 => $rev, name => $head };
147 #FIXME: That isn't the time I'm looking for..
148 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
149 my $dt = DateTime->from_epoch(epoch => $epoch);
150 $dt->set_time_zone($tz);
151 $ret[-1]->{last_change} = $dt;
160 Hashref of ArrayRefs for each reference.
164 has references => ( isa => HashRef[ArrayRef[Str]], is => 'ro', lazy_build => 1 );
166 method _build_references {
167 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
168 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
169 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
173 push @{$refs{$1}}, $2
174 if m!^($SHA1RE)\srefs/(.*)$!;
182 Return an array of contents for a given tree.
183 The tree is specified by sha1, and defaults to HEAD.
184 The keys for each item will be:
193 method list_tree (Str $sha1?) {
194 $sha1 ||= $self->head_hash;
196 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
197 return unless defined $output;
200 for my $line (split /\0/, $output) {
201 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
202 push @ret, Object->new( mode => oct $mode,
212 method get_object (NonEmptySimpleStr $sha1) {
213 unless ( $self->_is_valid_rev($sha1) ) {
214 $sha1 = $self->head_hash($sha1);
222 method _is_valid_rev (Str $rev) {
223 return ($rev =~ /^($SHA1RE)$/);
226 method hash_by_path ($base, $path = '', $type?) {
228 # FIXME should this really just take the first result?
229 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
231 my $line = $paths[0];
233 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
234 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
235 return defined $type && $type ne $2
240 method list_revs ( NonEmptySimpleStr :$sha1!,
244 NonEmptySimpleStr :$file?
246 $sha1 = $self->head_hash($sha1)
247 if !$sha1 || $sha1 !~ $SHA1RE;
251 $search->{type} = 'grep'
252 if $search->{type} eq 'commit';
254 # This seems a little fragile ...
255 qq[--$search->{type}=$search->{text}],
256 '--regexp-ignore-case',
257 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
261 my $output = $self->run_cmd(
264 (defined $count ? "--max-count=$count" : ()),
265 (defined $skip ? "--skip=$skip" : ()),
269 ($file ? $file : ()),
271 return unless $output;
273 my @revs = $self->parse_rev_list($output);
278 method parse_rev_list ($output) {
280 map $self->get_gpp_object($_),
281 grep $self->_is_valid_rev($_),
282 map split(/\n/, $_, 6), split /\0/, $output;
285 # XXX Ideally this would return a wee object instead of ad hoc structures.
286 method diff ( Gitalist::Git::Object :$commit,
288 Maybe[NonEmptySimpleStr] :$parent?,
289 NonEmptySimpleStr :$file? ) {
290 # Use parent if specifed, else take the parent from the commit
291 # if there is only one, otherwise it was a merge commit.
294 : $commit->parents <= 1
295 ? $commit->parent_sha1
298 ( $file ? ('--', $file) : () ),
301 my @out = $self->raw_diff(
302 ( $patch ? '--patch-with-raw' : () ),
303 ( $parent ? $parent : () ),
307 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
308 my @difftree = $self->parse_diff_tree(\@out);
313 # The blank line between the tree and the patch.
316 # XXX And no I'm not happy about having diff return tree + patch.
317 return \@difftree, [$self->parse_diff(@out)];
320 method parse_diff (@diff) {
323 # This regex is a little pathological.
324 if(m{^diff --git (a/(.*?)) (b/\2)}) {
335 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
336 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
340 # XXX Somewhat hacky. Ahem.
341 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
347 # gitweb uses the following sort of command for diffing merges:
348 # /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 --
349 # and for regular diffs
350 # /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 --
352 method raw_diff (@args) {
353 return $self->run_cmd_list(
354 qw(diff-tree -r -M --no-commit-id --full-index),
359 method parse_diff_tree ($diff) {
360 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
362 while (@$diff and $diff->[0] =~ /^:\d+/) {
363 my $line = shift @$diff;
364 # see. man git-diff-tree for more info
365 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
366 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
367 my %line = zip @keys, @vals;
368 # Some convenience keys
369 $line{file} = $line{src};
370 $line{sha1} = $line{sha1dst};
371 $line{is_new} = $line{sha1src} =~ /^0+$/
373 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
374 if $line{status} =~ /^R/;
381 method reflog (@logargs) {
383 = $self->run_cmd(qw(log -g), @logargs)
384 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
386 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
387 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
388 # Reflog message: push
389 # Author: Foo Barsby <fbarsby@example.com>
390 # Date: Thu Sep 17 12:26:05 2009 +0100
392 # Merge branch 'abc123'
395 # XXX Stuff like this makes me want to switch to Git::PurePerl
396 my($sha1, $type, $author, $date)
398 ^ commit \s+ ($SHA1RE)$
400 Reflog[ ]message: \s+ (.+?)$ \s+
401 Author: \s+ ([^<]+) <.*?$ \s+
405 pos($_) = index($_, $date) + length $date;
407 # Yeah, I just did that.
408 my($msg) = /\G\s+(\S.*)/sg;
414 # XXX Add DateTime goodness.
426 Returns a hash containing properties of this project. The keys will
430 description (empty if .git/description is empty/unnamed)
439 description => $self->description,
440 owner => $self->owner,
441 last_change => $self->last_change,
445 method _build__util {
446 Gitalist::Git::Util->new(
451 method _build_description {
452 my $description = "";
454 $description = $self->path->file('description')->slurp;
460 method _build_owner {
461 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
463 return length($gecos) ? $gecos : $name;
466 method _build_last_change {
468 my $output = $self->run_cmd(
469 qw{ for-each-ref --format=%(committer)
470 --sort=-committerdate --count=1 refs/heads
472 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
473 my $dt = DateTime->from_epoch(epoch => $epoch);
474 $dt->set_time_zone($tz);
482 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
484 =head1 AUTHORS AND COPYRIGHT
486 Catalyst application:
487 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
489 Original gitweb.cgi from which this was derived:
490 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
491 (C) 2005, Christian Gierke
495 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
497 This library is free software. You can redistribute it and/or modify
498 it under the same terms as Perl itself.