5 Gitalist::Git::Project - Model of a git repository
9 my $project = Gitalist::Git::Project->new( name => 'Gitalist',
10 path => $project_dir );
11 my $project = Gitalist::Git::Project->new($project_dir);
15 This class models a git repository, referred to in Gitalist
20 class Gitalist::Git::Project with Gitalist::Git::HasUtils {
21 # FIXME, use Types::Path::Class and coerce
22 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
23 use MooseX::Types::Path::Class qw/Dir/;
24 use MooseX::Types::Moose qw/Str Maybe Bool HashRef/;
25 use List::MoreUtils qw/any zip/;
27 use aliased 'Gitalist::Git::Object';
35 has name => ( isa => NonEmptySimpleStr,
36 is => 'ro', required => 1 );
40 L<Path::Class:Dir> for the location of the git repository.
44 has path => ( isa => Dir,
45 is => 'ro', required => 1);
49 String containing .git/description
53 has description => ( isa => Str,
60 Owner of the files on disk.
64 has owner => ( isa => NonEmptySimpleStr,
71 L<DateTime> for the time of the last update.
72 undef if the repository has never been used.
76 has last_change => ( isa => Maybe['DateTime'],
83 Bool indicating whether this Project is bare.
87 has is_bare => ( isa => Bool,
91 -d $_[0]->path->parent->subdir->($_[0]->name)
97 $self->$_() for qw/last_change owner description/; # Ensure to build early.
100 around BUILDARGS (ClassName $class: Dir $dir) {
101 my $name = $dir->dir_list(-1);
102 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
103 confess("Can't find a git repository at " . $dir)
104 unless ( -f $dir->file('HEAD') );
105 return $class->$orig(name => $name,
109 method _build__util {
110 Gitalist::Git::Util->new(
115 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
117 method _build_description {
118 my $description = "";
120 $description = $self->{path}->file('description')->slurp;
126 method _build_owner {
127 my ($gecos, $name) = (getpwuid $self->{path}->stat->uid)[6,0];
129 return length($gecos) ? $gecos : $name;
132 method _build_last_change {
134 my $output = $self->run_cmd(
135 qw{ for-each-ref --format=%(committer)
136 --sort=-committerdate --count=1 refs/heads
138 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
139 my $dt = DateTime->from_epoch(epoch => $epoch);
140 $dt->set_time_zone($tz);
148 Return an array containing the list of heads.
153 my $cmdout = $self->run_cmd(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
154 my @output = $cmdout ? split(/\n/, $cmdout) : ();
156 for my $line (@output) {
157 my ($rev, $head, $commiter) = split /\0/, $line, 3;
158 $head =~ s!^refs/heads/!!;
160 push @ret, { sha1 => $rev, name => $head };
162 #FIXME: That isn't the time I'm looking for..
163 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
164 my $dt = DateTime->from_epoch(epoch => $epoch);
165 $dt->set_time_zone($tz);
166 $ret[-1]->{last_change} = $dt;
175 Return a hash of references.
180 return $self->{references}
181 if $self->{references};
183 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
184 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
185 my $cmdout = $self->run_cmd(qw(show-ref --dereference))
187 my @reflist = $cmdout ? split(/\n/, $cmdout) : ();
190 push @{$refs{$1}}, $2
191 if m!^($SHA1RE)\srefs/(.*)$!;
194 return $self->{references} = \%refs;
199 Find the hash of a given head (defaults to HEAD).
203 method head_hash (Str $head?) {
204 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
205 return unless defined $output;
207 my($sha1) = $output =~ /^($SHA1RE)$/;
213 Return an array of contents for a given tree.
214 The tree is specified by sha1, and defaults to HEAD.
215 The keys for each item will be:
224 method list_tree (Str $sha1?) {
225 $sha1 ||= $self->head_hash;
227 my $output = $self->run_cmd(qw/ls-tree -z/, $sha1);
228 return unless defined $output;
231 for my $line (split /\0/, $output) {
232 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
233 push @ret, Object->new( mode => oct $mode,
243 method get_object (NonEmptySimpleStr $sha1) {
244 unless ( $self->_is_valid_rev($sha1) ) {
245 $sha1 = $self->head_hash($sha1);
253 method _is_valid_rev (Str $rev) {
254 return ($rev =~ /^($SHA1RE)$/);
257 # Should be in ::Object
258 method get_object_mode_string (Gitalist::Git::Object $object) {
259 return $object->modestr;
262 method get_object_type (NonEmptySimpleStr $sha1) {
263 return $self->get_object($sha1)->type;
266 method cat_file (NonEmptySimpleStr $sha1) {
267 return $self->get_object($sha1)->contents;
270 method hash_by_path ($base, $path?, $type?) {
274 my $output = $self->run_cmd('ls-tree', $base, '--', $path)
276 my($line) = $output ? split(/\n/, $output) : ();
278 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
279 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
280 return defined $type && $type ne $2
285 method list_revs ( NonEmptySimpleStr :$sha1!,
289 NonEmptySimpleStr :$file?
291 $sha1 = $self->head_hash($sha1)
292 if !$sha1 || $sha1 !~ $SHA1RE;
296 $search->{type} = 'grep'
297 if $search->{type} eq 'commit';
299 # This seems a little fragile ...
300 qq[--$search->{type}=$search->{text}],
301 '--regexp-ignore-case',
302 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
306 my $output = $self->run_cmd(
309 (defined $count ? "--max-count=$count" : ()),
310 (defined $skip ? "--skip=$skip" : ()),
314 ($file ? $file : ()),
316 return unless $output;
318 my @revs = $self->parse_rev_list($output);
323 method parse_rev_list ($output) {
325 map $self->get_gpp_object($_),
326 grep $self->_is_valid_rev($_),
327 map split(/\n/, $_, 6), split /\0/, $output;
330 # XXX Ideally this would return a wee object instead of ad hoc structures.
331 method diff ( Gitalist::Git::Object :$commit,
333 Maybe[NonEmptySimpleStr] :$parent?,
334 NonEmptySimpleStr :$file? ) {
335 # Use parent if specifed, else take the parent from the commit
336 # if there is only one, otherwise it was a merge commit.
339 : $commit->parents <= 1
340 ? $commit->parent_sha1
343 ( $file ? ('--', $file) : () ),
346 my @out = $self->raw_diff(
347 ( $patch ? '--patch-with-raw' : () ),
348 ( $parent ? $parent : () ),
352 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
353 my @difftree = $self->parse_diff_tree(\@out);
358 # The blank line between the tree and the patch.
361 # XXX And no I'm not happy about having diff return tree + patch.
362 return \@difftree, [$self->parse_diff(@out)];
365 method parse_diff (@diff) {
368 # This regex is a little pathological.
369 if(m{^diff --git (a/(.*?)) (b/\2)}) {
380 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
381 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
385 # XXX Somewhat hacky. Ahem.
386 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
392 # gitweb uses the following sort of command for diffing merges:
393 # /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 --
394 # and for regular diffs
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 2e3454ca0749641b42f063730b0090e1 316cf158df3f6207afbae7270bcc5ba0 --
397 method raw_diff (@args) {
398 my $cmdout = $self->run_cmd(
399 qw(diff-tree -r -M --no-commit-id --full-index),
402 return $cmdout ? split(/\n/, $cmdout) : ();
405 method parse_diff_tree ($diff) {
406 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
408 while (@$diff and $diff->[0] =~ /^:\d+/) {
409 my $line = shift @$diff;
410 # see. man git-diff-tree for more info
411 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
412 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
413 my %line = zip @keys, @vals;
414 # Some convenience keys
415 $line{file} = $line{src};
416 $line{sha1} = $line{sha1dst};
417 $line{is_new} = $line{sha1src} =~ /^0+$/
419 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
420 if $line{status} =~ /^R/;
427 method reflog (@logargs) {
429 = $self->run_cmd(qw(log -g), @logargs)
430 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
433 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
434 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
436 Author: Foo Barsby <fbarsby@example.com>
437 Date: Thu Sep 17 12:26:05 2009 +0100
439 Merge branch 'abc123'
444 # XXX Stuff like this makes me want to switch to Git::PurePerl
445 my($sha1, $type, $author, $date)
447 ^ commit \s+ ($SHA1RE)$
449 Reflog[ ]message: \s+ (.+?)$ \s+
450 Author: \s+ ([^<]+) <.*?$ \s+
454 pos($_) = index($_, $date) + length $date;
456 # Yeah, I just did that.
457 my($msg) = /\G\s+(\S.*)/sg;
463 # XXX Add DateTime goodness.
475 Returns a hash containing properties of this project. The keys will
479 description (empty if .git/description is empty/unnamed)
488 description => $self->description,
489 owner => $self->owner,
490 last_change => $self->last_change,
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.