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 my $name = $dir->dir_list(-1);
104 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
105 confess("Can't find a git repository at " . $dir)
106 unless ( -f $dir->file('HEAD') );
107 return $class->$orig(name => $name,
111 method _build__util {
112 Gitalist::Git::Util->new(
117 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
119 method _build_description {
120 my $description = "";
122 $description = $self->path->file('description')->slurp;
128 method _build_owner {
129 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
131 return length($gecos) ? $gecos : $name;
134 method _build_last_change {
136 my $output = $self->run_cmd(
137 qw{ for-each-ref --format=%(committer)
138 --sort=-committerdate --count=1 refs/heads
140 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
141 my $dt = DateTime->from_epoch(epoch => $epoch);
142 $dt->set_time_zone($tz);
150 Return an array containing the list of heads.
155 my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
156 my @output = $cmdout ? split(/\n/, $cmdout) : ();
158 for my $line (@output) {
159 my ($rev, $head, $commiter) = split /\0/, $line, 3;
160 $head =~ s!^refs/heads/!!;
162 push @ret, { sha1 => $rev, name => $head };
164 #FIXME: That isn't the time I'm looking for..
165 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
166 my $dt = DateTime->from_epoch(epoch => $epoch);
167 $dt->set_time_zone($tz);
168 $ret[-1]->{last_change} = $dt;
177 Return a hash of references.
181 has references => ( isa => HashRef[Str], is => 'ro', lazy_build => 1 );
183 method _build_references {
185 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
186 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
187 my $cmdout = $self->run_cmd(qw(show-ref --dereference))
189 my @reflist = $cmdout ? split(/\n/, $cmdout) : ();
192 push @{$refs{$1}}, $2
193 if m!^($SHA1RE)\srefs/(.*)$!;
201 Find the hash of a given head (defaults to HEAD).
205 method head_hash (Str $head?) {
206 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
207 return unless defined $output;
209 my($sha1) = $output =~ /^($SHA1RE)$/;
215 Return an array of contents for a given tree.
216 The tree is specified by sha1, and defaults to HEAD.
217 The keys for each item will be:
226 method list_tree (Str $sha1?) {
227 $sha1 ||= $self->head_hash;
229 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
230 return unless defined $output;
233 for my $line (split /\0/, $output) {
234 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
235 push @ret, Object->new( mode => oct $mode,
245 method get_object (NonEmptySimpleStr $sha1) {
246 unless ( $self->_is_valid_rev($sha1) ) {
247 $sha1 = $self->head_hash($sha1);
255 method _is_valid_rev (Str $rev) {
256 return ($rev =~ /^($SHA1RE)$/);
259 # Should be in ::Object
260 method get_object_mode_string (Gitalist::Git::Object $object) {
261 return $object->modestr;
264 method get_object_type (NonEmptySimpleStr $sha1) {
265 return $self->get_object($sha1)->type;
268 method cat_file (NonEmptySimpleStr $sha1) {
269 return $self->get_object($sha1)->contents;
272 method hash_by_path ($base, $path?, $type?) {
276 my $output = $self->run_cmd('ls-tree', $base, '--', $path)
278 my($line) = $output ? split(/\n/, $output) : ();
280 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
281 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
282 return defined $type && $type ne $2
287 method list_revs ( NonEmptySimpleStr :$sha1!,
291 NonEmptySimpleStr :$file?
293 $sha1 = $self->head_hash($sha1)
294 if !$sha1 || $sha1 !~ $SHA1RE;
298 $search->{type} = 'grep'
299 if $search->{type} eq 'commit';
301 # This seems a little fragile ...
302 qq[--$search->{type}=$search->{text}],
303 '--regexp-ignore-case',
304 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
308 my $output = $self->run_cmd(
311 (defined $count ? "--max-count=$count" : ()),
312 (defined $skip ? "--skip=$skip" : ()),
316 ($file ? $file : ()),
318 return unless $output;
320 my @revs = $self->parse_rev_list($output);
325 method parse_rev_list ($output) {
327 map $self->get_gpp_object($_),
328 grep $self->_is_valid_rev($_),
329 map split(/\n/, $_, 6), split /\0/, $output;
332 # XXX Ideally this would return a wee object instead of ad hoc structures.
333 method diff ( Gitalist::Git::Object :$commit,
335 Maybe[NonEmptySimpleStr] :$parent?,
336 NonEmptySimpleStr :$file? ) {
337 # Use parent if specifed, else take the parent from the commit
338 # if there is only one, otherwise it was a merge commit.
341 : $commit->parents <= 1
342 ? $commit->parent_sha1
345 ( $file ? ('--', $file) : () ),
348 my @out = $self->raw_diff(
349 ( $patch ? '--patch-with-raw' : () ),
350 ( $parent ? $parent : () ),
354 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
355 my @difftree = $self->parse_diff_tree(\@out);
360 # The blank line between the tree and the patch.
363 # XXX And no I'm not happy about having diff return tree + patch.
364 return \@difftree, [$self->parse_diff(@out)];
367 method parse_diff (@diff) {
370 # This regex is a little pathological.
371 if(m{^diff --git (a/(.*?)) (b/\2)}) {
382 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
383 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
387 # XXX Somewhat hacky. Ahem.
388 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
394 # gitweb uses the following sort of command for diffing merges:
395 # /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 --
396 # and for regular diffs
397 # /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 --
399 method raw_diff (@args) {
400 my $cmdout = $self->run_cmd(
401 qw(diff-tree -r -M --no-commit-id --full-index),
404 return $cmdout ? split(/\n/, $cmdout) : ();
407 method parse_diff_tree ($diff) {
408 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
410 while (@$diff and $diff->[0] =~ /^:\d+/) {
411 my $line = shift @$diff;
412 # see. man git-diff-tree for more info
413 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
414 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
415 my %line = zip @keys, @vals;
416 # Some convenience keys
417 $line{file} = $line{src};
418 $line{sha1} = $line{sha1dst};
419 $line{is_new} = $line{sha1src} =~ /^0+$/
421 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
422 if $line{status} =~ /^R/;
429 method reflog (@logargs) {
431 = $self->run_cmd(qw(log -g), @logargs)
432 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
434 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
435 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
436 # Reflog message: push
437 # Author: Foo Barsby <fbarsby@example.com>
438 # Date: Thu Sep 17 12:26:05 2009 +0100
440 # Merge branch 'abc123'
443 # XXX Stuff like this makes me want to switch to Git::PurePerl
444 my($sha1, $type, $author, $date)
446 ^ commit \s+ ($SHA1RE)$
448 Reflog[ ]message: \s+ (.+?)$ \s+
449 Author: \s+ ([^<]+) <.*?$ \s+
453 pos($_) = index($_, $date) + length $date;
455 # Yeah, I just did that.
456 my($msg) = /\G\s+(\S.*)/sg;
462 # XXX Add DateTime goodness.
474 Returns a hash containing properties of this project. The keys will
478 description (empty if .git/description is empty/unnamed)
487 description => $self->description,
488 owner => $self->owner,
489 last_change => $self->last_change,
495 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
497 =head1 AUTHORS AND COPYRIGHT
499 Catalyst application:
500 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
502 Original gitweb.cgi from which this was derived:
503 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
504 (C) 2005, Christian Gierke
508 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
510 This library is free software. You can redistribute it and/or modify
511 it under the same terms as Perl itself.