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/;
15 use if $^O ne 'MSWin32', 'I18N::Langinfo', => qw/langinfo CODESET/;
18 *__owner = defined &langinfo
19 ? sub { map { decode(langinfo(CODESET), $_) } (getpwuid $_[0]->path->stat->uid)[6,0] }
20 : sub { return qw/OwnEr GroUp/ }
24 use Gitalist::Git::Object::Blob;
25 use Gitalist::Git::Object::Tree;
26 use Gitalist::Git::Object::Commit;
27 use Gitalist::Git::Object::Tag;
29 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
31 around BUILDARGS (ClassName $class: Dir $dir) {
32 # Allows us to be called as Repository->new($dir)
33 # Last path component becomes $self->name
34 # Full path to git objects becomes $self->path
35 my $name = $dir->dir_list(-1);
36 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
37 confess("Can't find a git repository at " . $dir)
38 unless ( -f $dir->file('HEAD') );
39 return $class->$orig(name => $name,
43 has name => ( isa => NonEmptySimpleStr,
44 is => 'ro', required => 1 );
46 has path => ( isa => Dir,
47 is => 'ro', required => 1);
49 has description => ( isa => Str,
54 has owner => ( isa => NonEmptySimpleStr,
59 has last_change => ( isa => Maybe['DateTime'],
64 has is_bare => ( isa => Bool,
68 -d $_[0]->path->parent->subdir->($_[0]->name)
72 has heads => ( isa => ArrayRef[HashRef],
75 has tags => ( isa => ArrayRef[HashRef],
78 has references => ( isa => HashRef[ArrayRef[Str]],
83 $self->$_() for qw/last_change owner description/; # Ensure to build early.
88 multi method get_object_or_head (SHA1 $sha1) {
89 $self->get_object($sha1);
91 multi method get_object_or_head (NonEmptySimpleStr $ref) {
92 my $sha1 = $self->head_hash($ref);
93 $self->get_object($sha1);
96 method head_hash (Str $head?) {
97 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
98 confess("No such head: " . $head) unless defined $output;
100 my($sha1) = $output =~ /^($SHA1RE)$/;
104 method list_tree (SHA1 $sha1?) {
105 $sha1 ||= $self->head_hash;
106 my $object = $self->get_object($sha1);
107 return @{$object->tree};
110 method get_object (NonEmptySimpleStr $sha1) {
111 unless (is_SHA1($sha1)) {
112 $sha1 = $self->head_hash($sha1);
114 my $type = $self->run_cmd('cat-file', '-t', $sha1);
116 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
124 method hash_by_path ($base, $path = '', $type?) {
126 # FIXME should this really just take the first result?
127 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
129 my $line = $paths[0];
131 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
132 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
133 return defined $type && $type ne $2
138 method list_revs ( NonEmptySimpleStr :$sha1!,
142 NonEmptySimpleStr :$file? ) {
143 $sha1 = $self->head_hash($sha1)
144 if !$sha1 || $sha1 !~ $SHA1RE;
148 $search->{type} = 'grep'
149 if $search->{type} eq 'commit';
150 no warnings; # where's this warning coming from?
152 # This seems a little fragile ...
153 qq[--$search->{type}=$search->{text}],
154 '--regexp-ignore-case',
155 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
159 my $output = $self->run_cmd(
162 (defined $count ? "--max-count=$count" : ()),
163 (defined $skip ? "--skip=$skip" : ()),
167 ($file ? $file : ()),
169 return unless $output;
171 my @revs = $self->_parse_rev_list($output);
176 method snapshot (NonEmptySimpleStr :$sha1,
177 NonEmptySimpleStr :$format
179 # TODO - only valid formats are 'tar' and 'zip'
180 my $formats = { tgz => 'tar', zip => 'zip' };
181 unless ($formats->exists($format)) {
182 die("No such format: $format");
184 $format = $formats->{$format};
185 my $name = $self->name;
186 $name =~ s,([^/])/*\.git$,$1,;
187 my $filename = $name;
188 $filename .= "-$sha1.$format";
189 $name =~ s/\047/\047\\\047\047/g;
191 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
192 return ($filename, $self->run_cmd_fh(@cmd));
193 # TODO - support compressed archives
196 method diff ( Gitalist::Git::Object :$commit!,
198 Maybe[NonEmptySimpleStr] :$parent?,
199 NonEmptySimpleStr :$filename?
201 return $commit->diff( patch => $patch,
203 filename => $filename);
206 method reflog (@logargs) {
208 = $self->run_cmd(qw(log -g), @logargs)
209 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
211 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
212 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
213 # Reflog message: push
214 # Author: Foo Barsby <fbarsby@example.com>
215 # Date: Thu Sep 17 12:26:05 2009 +0100
217 # Merge branch 'abc123'
220 # XXX Stuff like this makes me want to switch to Git::PurePerl
221 my($sha1, $type, $author, $date)
223 ^ commit \s+ ($SHA1RE)$
225 Reflog[ ]message: \s+ (.+?)$ \s+
226 Author: \s+ ([^<]+) <.*?$ \s+
230 pos($_) = index($_, $date) + length $date;
232 # Yeah, I just did that.
233 my($msg) = /\G\s+(\S.*)/sg;
239 # XXX Add DateTime goodness.
249 Gitalist::Git::Util->new(
254 method _build_description {
255 my $description = "";
257 $description = $self->path->file('description')->slurp;
260 $description = "Unnamed repository, edit the .git/description file to set a description"
261 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
265 method _build_owner {
266 my ($gecos, $name) = $self->__owner;
268 return length($gecos) ? $gecos : $name;
271 method _build_last_change {
273 my $output = $self->run_cmd(
274 qw{ for-each-ref --format=%(committer)
275 --sort=-committerdate --count=1 refs/heads
277 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
278 my $dt = DateTime->from_epoch(epoch => $epoch);
279 $dt->set_time_zone($tz);
285 method _build_heads {
286 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
288 for my $line (@revlines) {
289 my ($rev, $head, $commiter) = split /\0/, $line, 3;
290 $head =~ s!^refs/heads/!!;
292 push @ret, { sha1 => $rev, name => $head };
294 #FIXME: That isn't the time I'm looking for..
295 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
296 my $dt = DateTime->from_epoch(epoch => $epoch);
297 $dt->set_time_zone($tz);
298 $ret[-1]->{last_change} = $dt;
306 my @revlines = $self->run_cmd_list('for-each-ref',
307 '--sort=-creatordate',
308 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
312 for my $line (@revlines) {
313 my($refinfo, $creatorinfo) = split /\0/, $line;
314 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
315 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
316 $name =~ s!^refs/tags/!!;
318 push @ret, { sha1 => $rev, name => $name };
320 #FIXME: That isn't the time I'm looking for..
322 my $dt = DateTime->from_epoch(epoch => $epoch);
323 $dt->set_time_zone($tz);
324 $ret[-1]->{last_change} = $dt;
331 method _build_references {
332 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
333 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
334 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
338 push @{$refs{$1}}, $2
339 if m!^($SHA1RE)\srefs/(.*)$!;
346 method _parse_rev_list ($output) {
348 map $self->get_gpp_object($_),
350 map split(/\n/, $_, 6), split /\0/, $output;
359 Gitalist::Git::Repository - Model of a git repository
363 my $gitrepo = dir('/repo/base/Gitalist');
364 my $repository = Gitalist::Git::Repository->new($gitrepo);
365 $repository->name; # 'Gitalist'
366 $repository->path; # '/repo/base/Gitalist/.git'
367 $repository->description; # 'Unnamed repository.'
371 This class models a git repository, referred to in Gitalist
379 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
383 L<Path::Class:Dir> for the filesystem path to the git repository.
387 The contents of .git/description.
391 Owner of the files on the filesystem.
395 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
399 True if this is a bare git repository.
405 An array of the name and sha1 of all heads/tags in the repository.
409 Hashref of ArrayRefs for each reference.
414 =head2 head_hash ($head?)
416 Return the sha1 for HEAD, or any specified head.
418 =head2 list_tree ($sha1?)
420 Return an array of contents for a given tree.
421 The tree is specified by sha1, and defaults to HEAD.
422 Each item is a L<Gitalist::Git::Object>.
424 =head2 get_object ($sha1)
426 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
428 =head2 hash_by_path ($sha1, $path, $type?)
430 Returns the sha1 for a given path, optionally limited by type.
432 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
434 Returns a list of revs for the given head ($sha1).
436 =head2 snapshot ($sha1, $format)
438 Generate an archived snapshot of the repository.
439 $sha1 should be a commit or tree.
440 Returns a filehandle to read from.
442 =head2 diff ($commit, $patch?, $parent?, $file?)
444 Generate a diff from a given L<Gitalist::Git::Object>.
446 =head2 reflog (@lorgargs)
448 Return a list of hashes representing each reflog entry.
450 FIXME Should this return objects?
455 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
460 See L<Gitalist> for authors.
464 See L<Gitalist> for the license.