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::Git::Object::Blob;
31 use Gitalist::Git::Object::Tree;
32 use Gitalist::Git::Object::Commit;
33 use Gitalist::Git::Object::Tag;
35 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
37 around BUILDARGS (ClassName $class: Dir $dir) {
38 # Allows us to be called as Project->new($dir)
39 # Last path component becomes $self->name
40 # Full path to git objects becomes $self->path
41 my $name = $dir->dir_list(-1);
42 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
43 confess("Can't find a git repository at " . $dir)
44 unless ( -f $dir->file('HEAD') );
45 return $class->$orig(name => $name,
53 The name of the Project. By default, this is derived from the path to the git repository.
56 has name => ( isa => NonEmptySimpleStr,
57 is => 'ro', required => 1 );
61 L<Path::Class:Dir> for the location of the git repository.
64 has path => ( isa => Dir,
65 is => 'ro', required => 1);
69 String containing .git/description
72 has description => ( isa => Str,
79 Owner of the files on disk.
82 has owner => ( isa => NonEmptySimpleStr,
89 L<DateTime> for the time of the last update.
90 undef if the repository has never been used.
93 has last_change => ( isa => Maybe['DateTime'],
100 Bool indicating whether this Project is bare.
103 has is_bare => ( isa => Bool,
107 -d $_[0]->path->parent->subdir->($_[0]->name)
114 ArrayRef of hashes containing the name and sha1 of all heads.
117 has heads => ( isa => ArrayRef[HashRef],
123 Hashref of ArrayRefs for each reference.
126 has references => ( isa => HashRef[ArrayRef[Str]],
131 $self->$_() for qw/last_change owner description/; # Ensure to build early.
136 =head2 head_hash ($head?)
138 Return the sha1 for HEAD, or any specified head.
141 method head_hash (Str $head?) {
142 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
143 confess("No such head: " . $head) unless defined $output;
145 my($sha1) = $output =~ /^($SHA1RE)$/;
149 =head2 list_tree ($sha1?)
151 Return an array of contents for a given tree.
152 The tree is specified by sha1, and defaults to HEAD.
153 Each item is a L<Gitalist::Git::Object>.
156 method list_tree (Str $sha1?) {
157 $sha1 ||= $self->head_hash;
158 my $object = $self->get_object($sha1);
159 return @{$object->tree};
162 =head2 get_object ($sha1)
164 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
167 method get_object (NonEmptySimpleStr $sha1) {
168 unless ( $self->_is_valid_rev($sha1) ) {
169 $sha1 = $self->head_hash($sha1);
171 my $type = $self->run_cmd('cat-file', '-t', $sha1);
173 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
181 =head2 hash_by_path($sha1, $path, $type?)
183 Returns the sha1 for a given path, optionally limited by type.
186 method hash_by_path ($base, $path = '', $type?) {
188 # FIXME should this really just take the first result?
189 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
191 my $line = $paths[0];
193 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
194 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
195 return defined $type && $type ne $2
200 =head2 list_revs($sha1, $count?, $skip?, \%search?, $file?)
202 Returns a list of revs for the given head ($sha1).
205 method list_revs ( NonEmptySimpleStr :$sha1!,
209 NonEmptySimpleStr :$file? ) {
210 $sha1 = $self->head_hash($sha1)
211 if !$sha1 || $sha1 !~ $SHA1RE;
215 $search->{type} = 'grep'
216 if $search->{type} eq 'commit';
218 # This seems a little fragile ...
219 qq[--$search->{type}=$search->{text}],
220 '--regexp-ignore-case',
221 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
225 my $output = $self->run_cmd(
228 (defined $count ? "--max-count=$count" : ()),
229 (defined $skip ? "--skip=$skip" : ()),
233 ($file ? $file : ()),
235 return unless $output;
237 my @revs = $self->_parse_rev_list($output);
242 =head2 snapshot($sha1, $format)
244 Generate an archived snapshot of the repository.
245 $sha1 should be a commit or tree.
246 Returns a filehandle to read from.
250 method snapshot (NonEmptySimpleStr :$sha1,
251 NonEmptySimpleStr :$format
253 # TODO - only valid formats are 'tar' and 'zip'
254 my $formats = { tgz => 'tar', zip => 'zip' };
255 unless ($formats->exists($format)) {
256 die("No such format: $format");
258 $format = $formats->{$format};
259 my $name = $self->name;
260 $name =~ s,([^/])/*\.git$,$1,;
261 my $filename = $name;
262 $filename .= "-$sha1.$format";
263 $name =~ s/\047/\047\\\047\047/g;
265 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
266 return ($filename, $self->run_cmd_fh(@cmd));
267 # TODO - support compressed archives
270 =head2 diff($commit, $patch?, $parent?, $file?)
272 Generate a diff from a given L<Gitalist::Git::Object>.
276 method diff ( Gitalist::Git::Object :$commit!,
278 Maybe[NonEmptySimpleStr] :$parent?,
279 NonEmptySimpleStr :$file?
281 return $commit->diff( patch => $patch,
286 =head2 reflog(@lorgargs)
288 Return a list of hashes representing each reflog entry.
290 FIXME Should this return objects?
293 method reflog (@logargs) {
295 = $self->run_cmd(qw(log -g), @logargs)
296 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
298 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
299 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
300 # Reflog message: push
301 # Author: Foo Barsby <fbarsby@example.com>
302 # Date: Thu Sep 17 12:26:05 2009 +0100
304 # Merge branch 'abc123'
307 # XXX Stuff like this makes me want to switch to Git::PurePerl
308 my($sha1, $type, $author, $date)
310 ^ commit \s+ ($SHA1RE)$
312 Reflog[ ]message: \s+ (.+?)$ \s+
313 Author: \s+ ([^<]+) <.*?$ \s+
317 pos($_) = index($_, $date) + length $date;
319 # Yeah, I just did that.
320 my($msg) = /\G\s+(\S.*)/sg;
326 # XXX Add DateTime goodness.
335 method _build__util {
336 Gitalist::Git::Util->new(
341 method _build_description {
342 my $description = "";
344 $description = $self->path->file('description')->slurp;
350 method _build_owner {
351 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
353 return length($gecos) ? $gecos : $name;
356 method _build_last_change {
358 my $output = $self->run_cmd(
359 qw{ for-each-ref --format=%(committer)
360 --sort=-committerdate --count=1 refs/heads
362 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
363 my $dt = DateTime->from_epoch(epoch => $epoch);
364 $dt->set_time_zone($tz);
370 method _build_heads {
371 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
373 for my $line (@revlines) {
374 my ($rev, $head, $commiter) = split /\0/, $line, 3;
375 $head =~ s!^refs/heads/!!;
377 push @ret, { sha1 => $rev, name => $head };
379 #FIXME: That isn't the time I'm looking for..
380 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
381 my $dt = DateTime->from_epoch(epoch => $epoch);
382 $dt->set_time_zone($tz);
383 $ret[-1]->{last_change} = $dt;
390 method _build_references {
391 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
392 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
393 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
397 push @{$refs{$1}}, $2
398 if m!^($SHA1RE)\srefs/(.*)$!;
405 method _is_valid_rev (Str $rev) {
406 return ($rev =~ /^($SHA1RE)$/);
409 method _parse_rev_list ($output) {
411 map $self->get_gpp_object($_),
412 grep $self->_is_valid_rev($_),
413 map split(/\n/, $_, 6), split /\0/, $output;
418 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
420 =head1 AUTHORS AND COPYRIGHT
422 Catalyst application:
423 (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com>
425 Original gitweb.cgi from which this was derived:
426 (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
427 (C) 2005, Christian Gierke
431 FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken..
433 This library is free software. You can redistribute it and/or modify
434 it under the same terms as Perl itself.