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],
122 ArrayRef of hashes containing the name and sha1 of all tags.
125 has tags => ( isa => ArrayRef[HashRef],
131 Hashref of ArrayRefs for each reference.
134 has references => ( isa => HashRef[ArrayRef[Str]],
139 $self->$_() for qw/last_change owner description/; # Ensure to build early.
144 =head2 head_hash ($head?)
146 Return the sha1 for HEAD, or any specified head.
149 method head_hash (Str $head?) {
150 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
151 confess("No such head: " . $head) unless defined $output;
153 my($sha1) = $output =~ /^($SHA1RE)$/;
157 =head2 list_tree ($sha1?)
159 Return an array of contents for a given tree.
160 The tree is specified by sha1, and defaults to HEAD.
161 Each item is a L<Gitalist::Git::Object>.
164 method list_tree (Str $sha1?) {
165 $sha1 ||= $self->head_hash;
166 my $object = $self->get_object($sha1);
167 return @{$object->tree};
170 =head2 get_object ($sha1)
172 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
175 method get_object (NonEmptySimpleStr $sha1) {
176 unless ( $self->_is_valid_rev($sha1) ) {
177 $sha1 = $self->head_hash($sha1);
179 my $type = $self->run_cmd('cat-file', '-t', $sha1);
181 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
189 =head2 hash_by_path($sha1, $path, $type?)
191 Returns the sha1 for a given path, optionally limited by type.
194 method hash_by_path ($base, $path = '', $type?) {
196 # FIXME should this really just take the first result?
197 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
199 my $line = $paths[0];
201 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
202 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
203 return defined $type && $type ne $2
208 =head2 list_revs($sha1, $count?, $skip?, \%search?, $file?)
210 Returns a list of revs for the given head ($sha1).
213 method list_revs ( NonEmptySimpleStr :$sha1!,
217 NonEmptySimpleStr :$file? ) {
218 $sha1 = $self->head_hash($sha1)
219 if !$sha1 || $sha1 !~ $SHA1RE;
223 $search->{type} = 'grep'
224 if $search->{type} eq 'commit';
226 # This seems a little fragile ...
227 qq[--$search->{type}=$search->{text}],
228 '--regexp-ignore-case',
229 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
233 my $output = $self->run_cmd(
236 (defined $count ? "--max-count=$count" : ()),
237 (defined $skip ? "--skip=$skip" : ()),
241 ($file ? $file : ()),
243 return unless $output;
245 my @revs = $self->_parse_rev_list($output);
250 =head2 snapshot($sha1, $format)
252 Generate an archived snapshot of the repository.
253 $sha1 should be a commit or tree.
254 Returns a filehandle to read from.
258 method snapshot (NonEmptySimpleStr :$sha1,
259 NonEmptySimpleStr :$format
261 # TODO - only valid formats are 'tar' and 'zip'
262 my $formats = { tgz => 'tar', zip => 'zip' };
263 unless ($formats->exists($format)) {
264 die("No such format: $format");
266 $format = $formats->{$format};
267 my $name = $self->name;
268 $name =~ s,([^/])/*\.git$,$1,;
269 my $filename = $name;
270 $filename .= "-$sha1.$format";
271 $name =~ s/\047/\047\\\047\047/g;
273 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
274 return ($filename, $self->run_cmd_fh(@cmd));
275 # TODO - support compressed archives
278 =head2 diff($commit, $patch?, $parent?, $file?)
280 Generate a diff from a given L<Gitalist::Git::Object>.
284 method diff ( Gitalist::Git::Object :$commit!,
286 Maybe[NonEmptySimpleStr] :$parent?,
287 NonEmptySimpleStr :$file?
289 return $commit->diff( patch => $patch,
294 =head2 reflog(@lorgargs)
296 Return a list of hashes representing each reflog entry.
298 FIXME Should this return objects?
301 method reflog (@logargs) {
303 = $self->run_cmd(qw(log -g), @logargs)
304 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
306 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
307 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
308 # Reflog message: push
309 # Author: Foo Barsby <fbarsby@example.com>
310 # Date: Thu Sep 17 12:26:05 2009 +0100
312 # Merge branch 'abc123'
315 # XXX Stuff like this makes me want to switch to Git::PurePerl
316 my($sha1, $type, $author, $date)
318 ^ commit \s+ ($SHA1RE)$
320 Reflog[ ]message: \s+ (.+?)$ \s+
321 Author: \s+ ([^<]+) <.*?$ \s+
325 pos($_) = index($_, $date) + length $date;
327 # Yeah, I just did that.
328 my($msg) = /\G\s+(\S.*)/sg;
334 # XXX Add DateTime goodness.
344 Gitalist::Git::Util->new(
349 method _build_description {
350 my $description = "";
352 $description = $self->path->file('description')->slurp;
358 method _build_owner {
359 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
361 return length($gecos) ? $gecos : $name;
364 method _build_last_change {
366 my $output = $self->run_cmd(
367 qw{ for-each-ref --format=%(committer)
368 --sort=-committerdate --count=1 refs/heads
370 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
371 my $dt = DateTime->from_epoch(epoch => $epoch);
372 $dt->set_time_zone($tz);
378 method _build_heads {
379 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
381 for my $line (@revlines) {
382 my ($rev, $head, $commiter) = split /\0/, $line, 3;
383 $head =~ s!^refs/heads/!!;
385 push @ret, { sha1 => $rev, name => $head };
387 #FIXME: That isn't the time I'm looking for..
388 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
389 my $dt = DateTime->from_epoch(epoch => $epoch);
390 $dt->set_time_zone($tz);
391 $ret[-1]->{last_change} = $dt;
399 my @revlines = $self->run_cmd_list('for-each-ref',
400 '--sort=-creatordate',
401 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
405 for my $line (@revlines) {
406 my($refinfo, $creatorinfo) = split /\0/, $line;
407 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
408 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
409 $name =~ s!^refs/tags/!!;
411 push @ret, { sha1 => $rev, name => $name };
413 #FIXME: That isn't the time I'm looking for..
415 my $dt = DateTime->from_epoch(epoch => $epoch);
416 $dt->set_time_zone($tz);
417 $ret[-1]->{last_change} = $dt;
424 method _build_references {
425 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
426 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
427 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
431 push @{$refs{$1}}, $2
432 if m!^($SHA1RE)\srefs/(.*)$!;
439 method _is_valid_rev (Str $rev) {
440 return ($rev =~ /^($SHA1RE)$/);
443 method _parse_rev_list ($output) {
445 map $self->get_gpp_object($_),
446 grep $self->_is_valid_rev($_),
447 map split(/\n/, $_, 6), split /\0/, $output;
456 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
460 See L<Gitalist> for authors.
464 See L<Gitalist> for the license.