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/;
28 use List::MoreUtils qw/any zip/;
30 use Gitalist::Util qw(to_utf8);
31 use Gitalist::Git::Object::Blob;
32 use Gitalist::Git::Object::Tree;
33 use Gitalist::Git::Object::Commit;
34 use Gitalist::Git::Object::Tag;
36 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
38 around BUILDARGS (ClassName $class: Dir $dir) {
39 # Allows us to be called as Project->new($dir)
40 # Last path component becomes $self->name
41 # Full path to git objects becomes $self->path
42 my $name = $dir->dir_list(-1);
43 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
44 confess("Can't find a git repository at " . $dir)
45 unless ( -f $dir->file('HEAD') );
46 return $class->$orig(name => $name,
54 The name of the Project. By default, this is derived from the path to the git repository.
57 has name => ( isa => NonEmptySimpleStr,
58 is => 'ro', required => 1 );
62 L<Path::Class:Dir> for the location of the git repository.
65 has path => ( isa => Dir,
66 is => 'ro', required => 1);
70 String containing .git/description
73 has description => ( isa => Str,
80 Owner of the files on disk.
83 has owner => ( isa => NonEmptySimpleStr,
90 L<DateTime> for the time of the last update.
91 undef if the repository has never been used.
94 has last_change => ( isa => Maybe['DateTime'],
101 Bool indicating whether this Project is bare.
104 has is_bare => ( isa => Bool,
108 -d $_[0]->path->parent->subdir->($_[0]->name)
115 ArrayRef of hashes containing the name and sha1 of all heads.
118 has heads => ( isa => ArrayRef[HashRef],
124 Hashref of ArrayRefs for each reference.
127 has references => ( isa => HashRef[ArrayRef[Str]],
132 $self->$_() for qw/last_change owner description/; # Ensure to build early.
137 =head2 head_hash ($head?)
139 Return the sha1 for HEAD, or any specified head.
142 method head_hash (Str $head?) {
143 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
144 confess("No such head: " . $head) unless defined $output;
146 my($sha1) = $output =~ /^($SHA1RE)$/;
150 =head2 list_tree ($sha1?)
152 Return an array of contents for a given tree.
153 The tree is specified by sha1, and defaults to HEAD.
154 Each item is a L<Gitalist::Git::Object>.
157 method list_tree (Str $sha1?) {
158 $sha1 ||= $self->head_hash;
159 my $object = $self->get_object($sha1);
160 return @{$object->tree};
163 =head2 get_object ($sha1)
165 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
168 method get_object (NonEmptySimpleStr $sha1) {
169 unless ( $self->_is_valid_rev($sha1) ) {
170 $sha1 = $self->head_hash($sha1);
172 my $type = $self->run_cmd('cat-file', '-t', $sha1);
174 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
182 =head2 hash_by_path($sha1, $path, $type?)
184 Returns the sha1 for a given path, optionally limited by type.
187 method hash_by_path ($base, $path = '', $type?) {
189 # FIXME should this really just take the first result?
190 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
192 my $line = $paths[0];
194 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
195 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
196 return defined $type && $type ne $2
201 =head2 list_revs($sha1, $count?, $skip?, \%search?, $file?)
203 Returns a list of revs for the given head ($sha1).
206 method list_revs ( NonEmptySimpleStr :$sha1!,
210 NonEmptySimpleStr :$file? ) {
211 $sha1 = $self->head_hash($sha1)
212 if !$sha1 || $sha1 !~ $SHA1RE;
216 $search->{type} = 'grep'
217 if $search->{type} eq 'commit';
219 # This seems a little fragile ...
220 qq[--$search->{type}=$search->{text}],
221 '--regexp-ignore-case',
222 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
226 my $output = $self->run_cmd(
229 (defined $count ? "--max-count=$count" : ()),
230 (defined $skip ? "--skip=$skip" : ()),
234 ($file ? $file : ()),
236 return unless $output;
238 my @revs = $self->_parse_rev_list($output);
243 =head2 snapshot($sha1, $format)
245 Generate an archived snapshot of the repository.
246 $sha1 should be a commit or tree.
247 Returns a filehandle to read from.
251 method snapshot (NonEmptySimpleStr :$sha1,
252 NonEmptySimpleStr :$format
254 # TODO - only valid formats are 'tar' and 'zip'
255 my $formats = { tgz => 'tar', zip => 'zip' };
256 unless ($formats->exists($format)) {
257 die("No such format: $format");
259 $format = $formats->{$format};
260 my $name = $self->name;
261 $name =~ s,([^/])/*\.git$,$1,;
262 my $filename = to_utf8($name);
263 $filename .= "-$sha1.$format";
264 $name =~ s/\047/\047\\\047\047/g;
267 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
268 return $self->run_cmd_fh(@cmd);
269 # TODO - support compressed archives
272 =head2 diff($commit, $patch?, $parent?, $file?)
274 Generate a diff from a given L<Gitalist::Git::Object>.
278 method diff ( Gitalist::Git::Object :$commit!,
280 Maybe[NonEmptySimpleStr] :$parent?,
281 NonEmptySimpleStr :$file?
283 return $commit->diff( patch => $patch,
288 =head2 reflog(@lorgargs)
290 Return a list of hashes representing each reflog entry.
292 FIXME Should this return objects?
295 method reflog (@logargs) {
297 = $self->run_cmd(qw(log -g), @logargs)
298 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
300 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
301 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
302 # Reflog message: push
303 # Author: Foo Barsby <fbarsby@example.com>
304 # Date: Thu Sep 17 12:26:05 2009 +0100
306 # Merge branch 'abc123'
309 # XXX Stuff like this makes me want to switch to Git::PurePerl
310 my($sha1, $type, $author, $date)
312 ^ commit \s+ ($SHA1RE)$
314 Reflog[ ]message: \s+ (.+?)$ \s+
315 Author: \s+ ([^<]+) <.*?$ \s+
319 pos($_) = index($_, $date) + length $date;
321 # Yeah, I just did that.
322 my($msg) = /\G\s+(\S.*)/sg;
328 # XXX Add DateTime goodness.
337 method _build__util {
338 Gitalist::Git::Util->new(
343 method _build_description {
344 my $description = "";
346 $description = $self->path->file('description')->slurp;
352 method _build_owner {
353 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
355 return length($gecos) ? $gecos : $name;
358 method _build_last_change {
360 my $output = $self->run_cmd(
361 qw{ for-each-ref --format=%(committer)
362 --sort=-committerdate --count=1 refs/heads
364 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
365 my $dt = DateTime->from_epoch(epoch => $epoch);
366 $dt->set_time_zone($tz);
372 method _build_heads {
373 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
375 for my $line (@revlines) {
376 my ($rev, $head, $commiter) = split /\0/, $line, 3;
377 $head =~ s!^refs/heads/!!;
379 push @ret, { sha1 => $rev, name => $head };
381 #FIXME: That isn't the time I'm looking for..
382 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
383 my $dt = DateTime->from_epoch(epoch => $epoch);
384 $dt->set_time_zone($tz);
385 $ret[-1]->{last_change} = $dt;
392 method _build_references {
393 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
394 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
395 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
399 push @{$refs{$1}}, $2
400 if m!^($SHA1RE)\srefs/(.*)$!;
407 method _is_valid_rev (Str $rev) {
408 return ($rev =~ /^($SHA1RE)$/);
411 method _parse_rev_list ($output) {
413 map $self->get_gpp_object($_),
414 grep $self->_is_valid_rev($_),
415 map split(/\n/, $_, 6), split /\0/, $output;
420 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
422 =head1 AUTHORS AND COPYRIGHT
424 Catalyst application:
425 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
427 Original gitweb.cgi from which this was derived:
428 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
429 (C) 2005, Christian Gierke
433 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
435 This library is free software. You can redistribute it and/or modify
436 it under the same terms as Perl itself.