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 # Should be in ::Object
227 method get_object_mode_string (Gitalist::Git::Object $object) {
228 return $object->modestr;
231 method get_object_type (NonEmptySimpleStr $sha1) {
232 return $self->get_object($sha1)->type;
235 method cat_file (NonEmptySimpleStr $sha1) {
236 return $self->get_object($sha1)->contents;
239 method hash_by_path ($base, $path?, $type?) {
242 # FIXME should this really just take the first result?
243 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
245 my $line = $paths[0];
247 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
248 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
249 return defined $type && $type ne $2
254 method list_revs ( NonEmptySimpleStr :$sha1!,
258 NonEmptySimpleStr :$file?
260 $sha1 = $self->head_hash($sha1)
261 if !$sha1 || $sha1 !~ $SHA1RE;
265 $search->{type} = 'grep'
266 if $search->{type} eq 'commit';
268 # This seems a little fragile ...
269 qq[--$search->{type}=$search->{text}],
270 '--regexp-ignore-case',
271 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
275 my $output = $self->run_cmd(
278 (defined $count ? "--max-count=$count" : ()),
279 (defined $skip ? "--skip=$skip" : ()),
283 ($file ? $file : ()),
285 return unless $output;
287 my @revs = $self->parse_rev_list($output);
292 method parse_rev_list ($output) {
294 map $self->get_gpp_object($_),
295 grep $self->_is_valid_rev($_),
296 map split(/\n/, $_, 6), split /\0/, $output;
299 # XXX Ideally this would return a wee object instead of ad hoc structures.
300 method diff ( Gitalist::Git::Object :$commit,
302 Maybe[NonEmptySimpleStr] :$parent?,
303 NonEmptySimpleStr :$file? ) {
304 # Use parent if specifed, else take the parent from the commit
305 # if there is only one, otherwise it was a merge commit.
308 : $commit->parents <= 1
309 ? $commit->parent_sha1
312 ( $file ? ('--', $file) : () ),
315 my @out = $self->raw_diff(
316 ( $patch ? '--patch-with-raw' : () ),
317 ( $parent ? $parent : () ),
321 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
322 my @difftree = $self->parse_diff_tree(\@out);
327 # The blank line between the tree and the patch.
330 # XXX And no I'm not happy about having diff return tree + patch.
331 return \@difftree, [$self->parse_diff(@out)];
334 method parse_diff (@diff) {
337 # This regex is a little pathological.
338 if(m{^diff --git (a/(.*?)) (b/\2)}) {
349 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
350 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
354 # XXX Somewhat hacky. Ahem.
355 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
361 # gitweb uses the following sort of command for diffing merges:
362 # /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 --
363 # and for regular diffs
364 # /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 --
366 method raw_diff (@args) {
367 return $self->run_cmd_list(
368 qw(diff-tree -r -M --no-commit-id --full-index),
373 method parse_diff_tree ($diff) {
374 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
376 while (@$diff and $diff->[0] =~ /^:\d+/) {
377 my $line = shift @$diff;
378 # see. man git-diff-tree for more info
379 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
380 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
381 my %line = zip @keys, @vals;
382 # Some convenience keys
383 $line{file} = $line{src};
384 $line{sha1} = $line{sha1dst};
385 $line{is_new} = $line{sha1src} =~ /^0+$/
387 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
388 if $line{status} =~ /^R/;
395 method reflog (@logargs) {
397 = $self->run_cmd(qw(log -g), @logargs)
398 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
400 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
401 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
402 # Reflog message: push
403 # Author: Foo Barsby <fbarsby@example.com>
404 # Date: Thu Sep 17 12:26:05 2009 +0100
406 # Merge branch 'abc123'
409 # XXX Stuff like this makes me want to switch to Git::PurePerl
410 my($sha1, $type, $author, $date)
412 ^ commit \s+ ($SHA1RE)$
414 Reflog[ ]message: \s+ (.+?)$ \s+
415 Author: \s+ ([^<]+) <.*?$ \s+
419 pos($_) = index($_, $date) + length $date;
421 # Yeah, I just did that.
422 my($msg) = /\G\s+(\S.*)/sg;
428 # XXX Add DateTime goodness.
440 Returns a hash containing properties of this project. The keys will
444 description (empty if .git/description is empty/unnamed)
453 description => $self->description,
454 owner => $self->owner,
455 last_change => $self->last_change,
459 method _build__util {
460 Gitalist::Git::Util->new(
465 method _build_description {
466 my $description = "";
468 $description = $self->path->file('description')->slurp;
474 method _build_owner {
475 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
477 return length($gecos) ? $gecos : $name;
480 method _build_last_change {
482 my $output = $self->run_cmd(
483 qw{ for-each-ref --format=%(committer)
484 --sort=-committerdate --count=1 refs/heads
486 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
487 my $dt = DateTime->from_epoch(epoch => $epoch);
488 $dt->set_time_zone($tz);
496 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
498 =head1 AUTHORS AND COPYRIGHT
500 Catalyst application:
501 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
503 Original gitweb.cgi from which this was derived:
504 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
505 (C) 2005, Christian Gierke
509 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
511 This library is free software. You can redistribute it and/or modify
512 it under the same terms as Perl itself.