3 class Gitalist::Git::Project with Gitalist::Git::HasUtils {
4 # FIXME, use Types::Path::Class and coerce
5 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
6 use MooseX::Types::Path::Class qw/Dir/;
7 use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/;
8 use Gitalist::Git::Types qw/SHA1/;
10 use List::MoreUtils qw/any zip/;
12 use Gitalist::Git::Object::Blob;
13 use Gitalist::Git::Object::Tree;
14 use Gitalist::Git::Object::Commit;
15 use Gitalist::Git::Object::Tag;
17 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
19 around BUILDARGS (ClassName $class: Dir $dir) {
20 # Allows us to be called as Project->new($dir)
21 # Last path component becomes $self->name
22 # Full path to git objects becomes $self->path
23 my $name = $dir->dir_list(-1);
24 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
25 confess("Can't find a git repository at " . $dir)
26 unless ( -f $dir->file('HEAD') );
27 return $class->$orig(name => $name,
31 has name => ( isa => NonEmptySimpleStr,
32 is => 'ro', required => 1 );
34 has path => ( isa => Dir,
35 is => 'ro', required => 1);
37 has description => ( isa => Str,
42 has owner => ( isa => NonEmptySimpleStr,
47 has last_change => ( isa => Maybe['DateTime'],
52 has is_bare => ( isa => Bool,
56 -d $_[0]->path->parent->subdir->($_[0]->name)
60 has heads => ( isa => ArrayRef[HashRef],
63 has tags => ( isa => ArrayRef[HashRef],
66 has references => ( isa => HashRef[ArrayRef[Str]],
71 $self->$_() for qw/last_change owner description/; # Ensure to build early.
75 method head_hash (Str $head?) {
76 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
77 confess("No such head: " . $head) unless defined $output;
79 my($sha1) = $output =~ /^($SHA1RE)$/;
83 method list_tree (SHA1 $sha1?) {
84 $sha1 ||= $self->head_hash;
85 my $object = $self->get_object($sha1);
86 return @{$object->tree};
89 method get_object (NonEmptySimpleStr $sha1) {
90 unless (is_SHA1($sha1)) {
91 $sha1 = $self->head_hash($sha1);
93 my $type = $self->run_cmd('cat-file', '-t', $sha1);
95 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
103 method hash_by_path ($base, $path = '', $type?) {
105 # FIXME should this really just take the first result?
106 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
108 my $line = $paths[0];
110 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
111 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
112 return defined $type && $type ne $2
117 method list_revs ( NonEmptySimpleStr :$sha1!,
121 NonEmptySimpleStr :$file? ) {
122 $sha1 = $self->head_hash($sha1)
123 if !$sha1 || $sha1 !~ $SHA1RE;
127 $search->{type} = 'grep'
128 if $search->{type} eq 'commit';
130 # This seems a little fragile ...
131 qq[--$search->{type}=$search->{text}],
132 '--regexp-ignore-case',
133 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
137 my $output = $self->run_cmd(
140 (defined $count ? "--max-count=$count" : ()),
141 (defined $skip ? "--skip=$skip" : ()),
145 ($file ? $file : ()),
147 return unless $output;
149 my @revs = $self->_parse_rev_list($output);
154 method snapshot (NonEmptySimpleStr :$sha1,
155 NonEmptySimpleStr :$format
157 # TODO - only valid formats are 'tar' and 'zip'
158 my $formats = { tgz => 'tar', zip => 'zip' };
159 unless ($formats->exists($format)) {
160 die("No such format: $format");
162 $format = $formats->{$format};
163 my $name = $self->name;
164 $name =~ s,([^/])/*\.git$,$1,;
165 my $filename = $name;
166 $filename .= "-$sha1.$format";
167 $name =~ s/\047/\047\\\047\047/g;
169 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
170 return ($filename, $self->run_cmd_fh(@cmd));
171 # TODO - support compressed archives
174 method diff ( Gitalist::Git::Object :$commit!,
176 Maybe[NonEmptySimpleStr] :$parent?,
177 NonEmptySimpleStr :$file?
179 return $commit->diff( patch => $patch,
184 method reflog (@logargs) {
186 = $self->run_cmd(qw(log -g), @logargs)
187 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
189 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
190 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
191 # Reflog message: push
192 # Author: Foo Barsby <fbarsby@example.com>
193 # Date: Thu Sep 17 12:26:05 2009 +0100
195 # Merge branch 'abc123'
198 # XXX Stuff like this makes me want to switch to Git::PurePerl
199 my($sha1, $type, $author, $date)
201 ^ commit \s+ ($SHA1RE)$
203 Reflog[ ]message: \s+ (.+?)$ \s+
204 Author: \s+ ([^<]+) <.*?$ \s+
208 pos($_) = index($_, $date) + length $date;
210 # Yeah, I just did that.
211 my($msg) = /\G\s+(\S.*)/sg;
217 # XXX Add DateTime goodness.
227 Gitalist::Git::Util->new(
232 method _build_description {
233 my $description = "";
235 $description = $self->path->file('description')->slurp;
241 method _build_owner {
242 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
244 return length($gecos) ? $gecos : $name;
247 method _build_last_change {
249 my $output = $self->run_cmd(
250 qw{ for-each-ref --format=%(committer)
251 --sort=-committerdate --count=1 refs/heads
253 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
254 my $dt = DateTime->from_epoch(epoch => $epoch);
255 $dt->set_time_zone($tz);
261 method _build_heads {
262 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
264 for my $line (@revlines) {
265 my ($rev, $head, $commiter) = split /\0/, $line, 3;
266 $head =~ s!^refs/heads/!!;
268 push @ret, { sha1 => $rev, name => $head };
270 #FIXME: That isn't the time I'm looking for..
271 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
272 my $dt = DateTime->from_epoch(epoch => $epoch);
273 $dt->set_time_zone($tz);
274 $ret[-1]->{last_change} = $dt;
282 my @revlines = $self->run_cmd_list('for-each-ref',
283 '--sort=-creatordate',
284 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
288 for my $line (@revlines) {
289 my($refinfo, $creatorinfo) = split /\0/, $line;
290 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
291 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
292 $name =~ s!^refs/tags/!!;
294 push @ret, { sha1 => $rev, name => $name };
296 #FIXME: That isn't the time I'm looking for..
298 my $dt = DateTime->from_epoch(epoch => $epoch);
299 $dt->set_time_zone($tz);
300 $ret[-1]->{last_change} = $dt;
307 method _build_references {
308 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
309 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
310 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
314 push @{$refs{$1}}, $2
315 if m!^($SHA1RE)\srefs/(.*)$!;
322 method _parse_rev_list ($output) {
324 map $self->get_gpp_object($_),
326 map split(/\n/, $_, 6), split /\0/, $output;
335 Gitalist::Git::Project - Model of a git repository
339 my $gitrepo = dir('/repo/base/Gitalist');
340 my $project = Gitalist::Git::Project->new($gitrepo);
341 $project->name; # 'Gitalist'
342 $project->path; # '/repo/base/Gitalist/.git'
343 $project->description; # 'Unnamed repository.'
347 This class models a git repository, referred to in Gitalist
355 The name of the Project. If unspecified, this will be derived from the path to the git repository.
359 L<Path::Class:Dir> for the filesystem path to the git repository.
363 The contents of .git/description.
367 Owner of the files on the filesystem.
371 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
375 True if this is a bare git repository.
381 An array of the name and sha1 of all heads/tags in the repository.
385 Hashref of ArrayRefs for each reference.
390 =head2 head_hash ($head?)
392 Return the sha1 for HEAD, or any specified head.
394 =head2 list_tree ($sha1?)
396 Return an array of contents for a given tree.
397 The tree is specified by sha1, and defaults to HEAD.
398 Each item is a L<Gitalist::Git::Object>.
400 =head2 get_object ($sha1)
402 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
404 =head2 hash_by_path ($sha1, $path, $type?)
406 Returns the sha1 for a given path, optionally limited by type.
408 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
410 Returns a list of revs for the given head ($sha1).
412 =head2 snapshot ($sha1, $format)
414 Generate an archived snapshot of the repository.
415 $sha1 should be a commit or tree.
416 Returns a filehandle to read from.
418 =head2 diff ($commit, $patch?, $parent?, $file?)
420 Generate a diff from a given L<Gitalist::Git::Object>.
422 =head2 reflog (@lorgargs)
424 Return a list of hashes representing each reflog entry.
426 FIXME Should this return objects?
431 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
436 See L<Gitalist> for authors.
440 See L<Gitalist> for the license.