3 class Gitalist::Git::Repository 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/;
9 use MooseX::MultiMethods;
11 use List::MoreUtils qw/any zip/;
13 use Encode qw/decode/;
14 use I18N::Langinfo qw/langinfo CODESET/;
15 use Gitalist::Git::Object::Blob;
16 use Gitalist::Git::Object::Tree;
17 use Gitalist::Git::Object::Commit;
18 use Gitalist::Git::Object::Tag;
20 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
22 around BUILDARGS (ClassName $class: Dir $dir) {
23 # Allows us to be called as Repository->new($dir)
24 # Last path component becomes $self->name
25 # Full path to git objects becomes $self->path
26 my $name = $dir->dir_list(-1);
27 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
28 confess("Can't find a git repository at " . $dir)
29 unless ( -f $dir->file('HEAD') );
30 return $class->$orig(name => $name,
34 has name => ( isa => NonEmptySimpleStr,
35 is => 'ro', required => 1 );
37 has path => ( isa => Dir,
38 is => 'ro', required => 1);
40 has description => ( isa => Str,
45 has owner => ( isa => NonEmptySimpleStr,
50 has last_change => ( isa => Maybe['DateTime'],
55 has is_bare => ( isa => Bool,
59 -d $_[0]->path->parent->subdir->($_[0]->name)
63 has heads => ( isa => ArrayRef[HashRef],
66 has tags => ( isa => ArrayRef[HashRef],
69 has references => ( isa => HashRef[ArrayRef[Str]],
74 $self->$_() for qw/last_change owner description/; # Ensure to build early.
79 multi method get_object_or_head (SHA1 $sha1) {
80 $self->get_object($sha1);
82 multi method get_object_or_head (NonEmptySimpleStr $ref) {
83 my $sha1 = $self->head_hash($ref);
84 $self->get_object($sha1);
87 method head_hash (Str $head?) {
88 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
89 confess("No such head: " . $head) unless defined $output;
91 my($sha1) = $output =~ /^($SHA1RE)$/;
95 method list_tree (SHA1 $sha1?) {
96 $sha1 ||= $self->head_hash;
97 my $object = $self->get_object($sha1);
98 return @{$object->tree};
101 method get_object (NonEmptySimpleStr $sha1) {
102 unless (is_SHA1($sha1)) {
103 $sha1 = $self->head_hash($sha1);
105 my $type = $self->run_cmd('cat-file', '-t', $sha1);
107 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
115 method hash_by_path ($base, $path = '', $type?) {
117 # FIXME should this really just take the first result?
118 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
120 my $line = $paths[0];
122 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
123 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
124 return defined $type && $type ne $2
129 method list_revs ( NonEmptySimpleStr :$sha1!,
133 NonEmptySimpleStr :$file? ) {
134 $sha1 = $self->head_hash($sha1)
135 if !$sha1 || $sha1 !~ $SHA1RE;
139 $search->{type} = 'grep'
140 if $search->{type} eq 'commit';
142 # This seems a little fragile ...
143 qq[--$search->{type}=$search->{text}],
144 '--regexp-ignore-case',
145 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
149 my $output = $self->run_cmd(
152 (defined $count ? "--max-count=$count" : ()),
153 (defined $skip ? "--skip=$skip" : ()),
157 ($file ? $file : ()),
159 return unless $output;
161 my @revs = $self->_parse_rev_list($output);
166 method snapshot (NonEmptySimpleStr :$sha1,
167 NonEmptySimpleStr :$format
169 # TODO - only valid formats are 'tar' and 'zip'
170 my $formats = { tgz => 'tar', zip => 'zip' };
171 unless ($formats->exists($format)) {
172 die("No such format: $format");
174 $format = $formats->{$format};
175 my $name = $self->name;
176 $name =~ s,([^/])/*\.git$,$1,;
177 my $filename = $name;
178 $filename .= "-$sha1.$format";
179 $name =~ s/\047/\047\\\047\047/g;
181 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
182 return ($filename, $self->run_cmd_fh(@cmd));
183 # TODO - support compressed archives
186 method diff ( Gitalist::Git::Object :$commit!,
188 Maybe[NonEmptySimpleStr] :$parent?,
189 NonEmptySimpleStr :$filename?
191 return $commit->diff( patch => $patch,
193 filename => $filename);
196 method reflog (@logargs) {
198 = $self->run_cmd(qw(log -g), @logargs)
199 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
201 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
202 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
203 # Reflog message: push
204 # Author: Foo Barsby <fbarsby@example.com>
205 # Date: Thu Sep 17 12:26:05 2009 +0100
207 # Merge branch 'abc123'
210 # XXX Stuff like this makes me want to switch to Git::PurePerl
211 my($sha1, $type, $author, $date)
213 ^ commit \s+ ($SHA1RE)$
215 Reflog[ ]message: \s+ (.+?)$ \s+
216 Author: \s+ ([^<]+) <.*?$ \s+
220 pos($_) = index($_, $date) + length $date;
222 # Yeah, I just did that.
223 my($msg) = /\G\s+(\S.*)/sg;
229 # XXX Add DateTime goodness.
239 Gitalist::Git::Util->new(
244 method _build_description {
245 my $description = "";
247 $description = $self->path->file('description')->slurp;
253 method _build_owner {
254 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
256 return length($gecos) ? $gecos : $name;
259 method _build_last_change {
261 my $output = $self->run_cmd(
262 qw{ for-each-ref --format=%(committer)
263 --sort=-committerdate --count=1 refs/heads
265 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
266 my $dt = DateTime->from_epoch(epoch => $epoch);
267 $dt->set_time_zone($tz);
273 method _build_heads {
274 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
276 for my $line (@revlines) {
277 my ($rev, $head, $commiter) = split /\0/, $line, 3;
278 $head =~ s!^refs/heads/!!;
280 push @ret, { sha1 => $rev, name => $head };
282 #FIXME: That isn't the time I'm looking for..
283 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
284 my $dt = DateTime->from_epoch(epoch => $epoch);
285 $dt->set_time_zone($tz);
286 $ret[-1]->{last_change} = $dt;
294 my @revlines = $self->run_cmd_list('for-each-ref',
295 '--sort=-creatordate',
296 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
300 for my $line (@revlines) {
301 my($refinfo, $creatorinfo) = split /\0/, $line;
302 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
303 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
304 $name =~ s!^refs/tags/!!;
306 push @ret, { sha1 => $rev, name => $name };
308 #FIXME: That isn't the time I'm looking for..
310 my $dt = DateTime->from_epoch(epoch => $epoch);
311 $dt->set_time_zone($tz);
312 $ret[-1]->{last_change} = $dt;
319 method _build_references {
320 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
321 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
322 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
326 push @{$refs{$1}}, $2
327 if m!^($SHA1RE)\srefs/(.*)$!;
334 method _parse_rev_list ($output) {
336 map $self->get_gpp_object($_),
338 map split(/\n/, $_, 6), split /\0/, $output;
347 Gitalist::Git::Repository - Model of a git repository
351 my $gitrepo = dir('/repo/base/Gitalist');
352 my $repository = Gitalist::Git::Repository->new($gitrepo);
353 $repository->name; # 'Gitalist'
354 $repository->path; # '/repo/base/Gitalist/.git'
355 $repository->description; # 'Unnamed repository.'
359 This class models a git repository, referred to in Gitalist
367 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
371 L<Path::Class:Dir> for the filesystem path to the git repository.
375 The contents of .git/description.
379 Owner of the files on the filesystem.
383 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
387 True if this is a bare git repository.
393 An array of the name and sha1 of all heads/tags in the repository.
397 Hashref of ArrayRefs for each reference.
402 =head2 head_hash ($head?)
404 Return the sha1 for HEAD, or any specified head.
406 =head2 list_tree ($sha1?)
408 Return an array of contents for a given tree.
409 The tree is specified by sha1, and defaults to HEAD.
410 Each item is a L<Gitalist::Git::Object>.
412 =head2 get_object ($sha1)
414 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
416 =head2 hash_by_path ($sha1, $path, $type?)
418 Returns the sha1 for a given path, optionally limited by type.
420 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
422 Returns a list of revs for the given head ($sha1).
424 =head2 snapshot ($sha1, $format)
426 Generate an archived snapshot of the repository.
427 $sha1 should be a commit or tree.
428 Returns a filehandle to read from.
430 =head2 diff ($commit, $patch?, $parent?, $file?)
432 Generate a diff from a given L<Gitalist::Git::Object>.
434 =head2 reflog (@lorgargs)
436 Return a list of hashes representing each reflog entry.
438 FIXME Should this return objects?
443 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
448 See L<Gitalist> for authors.
452 See L<Gitalist> for the license.