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';
151 # This seems a little fragile ...
152 qq[--$search->{type}=$search->{text}],
153 '--regexp-ignore-case',
154 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
158 my $output = $self->run_cmd(
161 (defined $count ? "--max-count=$count" : ()),
162 (defined $skip ? "--skip=$skip" : ()),
166 ($file ? $file : ()),
168 return unless $output;
170 my @revs = $self->_parse_rev_list($output);
175 method snapshot (NonEmptySimpleStr :$sha1,
176 NonEmptySimpleStr :$format
178 # TODO - only valid formats are 'tar' and 'zip'
179 my $formats = { tgz => 'tar', zip => 'zip' };
180 unless ($formats->exists($format)) {
181 die("No such format: $format");
183 $format = $formats->{$format};
184 my $name = $self->name;
185 $name =~ s,([^/])/*\.git$,$1,;
186 my $filename = $name;
187 $filename .= "-$sha1.$format";
188 $name =~ s/\047/\047\\\047\047/g;
190 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
191 return ($filename, $self->run_cmd_fh(@cmd));
192 # TODO - support compressed archives
195 method diff ( Gitalist::Git::Object :$commit!,
197 Maybe[NonEmptySimpleStr] :$parent?,
198 NonEmptySimpleStr :$filename?
200 return $commit->diff( patch => $patch,
202 filename => $filename);
205 method reflog (@logargs) {
207 = $self->run_cmd(qw(log -g), @logargs)
208 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
210 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
211 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
212 # Reflog message: push
213 # Author: Foo Barsby <fbarsby@example.com>
214 # Date: Thu Sep 17 12:26:05 2009 +0100
216 # Merge branch 'abc123'
219 # XXX Stuff like this makes me want to switch to Git::PurePerl
220 my($sha1, $type, $author, $date)
222 ^ commit \s+ ($SHA1RE)$
224 Reflog[ ]message: \s+ (.+?)$ \s+
225 Author: \s+ ([^<]+) <.*?$ \s+
229 pos($_) = index($_, $date) + length $date;
231 # Yeah, I just did that.
232 my($msg) = /\G\s+(\S.*)/sg;
238 # XXX Add DateTime goodness.
248 Gitalist::Git::Util->new(
253 method _build_description {
254 my $description = "";
256 $description = $self->path->file('description')->slurp;
259 $description = "Unnamed repository, edit the .git/description file to set a description"
260 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
264 method _build_owner {
265 my ($gecos, $name) = $self->__owner;
267 return length($gecos) ? $gecos : $name;
270 method _build_last_change {
272 my $output = $self->run_cmd(
273 qw{ for-each-ref --format=%(committer)
274 --sort=-committerdate --count=1 refs/heads
276 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
277 my $dt = DateTime->from_epoch(epoch => $epoch);
278 $dt->set_time_zone($tz);
284 method _build_heads {
285 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
287 for my $line (@revlines) {
288 my ($rev, $head, $commiter) = split /\0/, $line, 3;
289 $head =~ s!^refs/heads/!!;
291 push @ret, { sha1 => $rev, name => $head };
293 #FIXME: That isn't the time I'm looking for..
294 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
295 my $dt = DateTime->from_epoch(epoch => $epoch);
296 $dt->set_time_zone($tz);
297 $ret[-1]->{last_change} = $dt;
305 my @revlines = $self->run_cmd_list('for-each-ref',
306 '--sort=-creatordate',
307 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
311 for my $line (@revlines) {
312 my($refinfo, $creatorinfo) = split /\0/, $line;
313 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
314 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
315 $name =~ s!^refs/tags/!!;
317 push @ret, { sha1 => $rev, name => $name };
319 #FIXME: That isn't the time I'm looking for..
321 my $dt = DateTime->from_epoch(epoch => $epoch);
322 $dt->set_time_zone($tz);
323 $ret[-1]->{last_change} = $dt;
330 method _build_references {
331 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
332 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
333 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
337 push @{$refs{$1}}, $2
338 if m!^($SHA1RE)\srefs/(.*)$!;
345 method _parse_rev_list ($output) {
347 map $self->get_gpp_object($_),
349 map split(/\n/, $_, 6), split /\0/, $output;
358 Gitalist::Git::Repository - Model of a git repository
362 my $gitrepo = dir('/repo/base/Gitalist');
363 my $repository = Gitalist::Git::Repository->new($gitrepo);
364 $repository->name; # 'Gitalist'
365 $repository->path; # '/repo/base/Gitalist/.git'
366 $repository->description; # 'Unnamed repository.'
370 This class models a git repository, referred to in Gitalist
378 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
382 L<Path::Class:Dir> for the filesystem path to the git repository.
386 The contents of .git/description.
390 Owner of the files on the filesystem.
394 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
398 True if this is a bare git repository.
404 An array of the name and sha1 of all heads/tags in the repository.
408 Hashref of ArrayRefs for each reference.
413 =head2 head_hash ($head?)
415 Return the sha1 for HEAD, or any specified head.
417 =head2 list_tree ($sha1?)
419 Return an array of contents for a given tree.
420 The tree is specified by sha1, and defaults to HEAD.
421 Each item is a L<Gitalist::Git::Object>.
423 =head2 get_object ($sha1)
425 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
427 =head2 hash_by_path ($sha1, $path, $type?)
429 Returns the sha1 for a given path, optionally limited by type.
431 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
433 Returns a list of revs for the given head ($sha1).
435 =head2 snapshot ($sha1, $format)
437 Generate an archived snapshot of the repository.
438 $sha1 should be a commit or tree.
439 Returns a filehandle to read from.
441 =head2 diff ($commit, $patch?, $parent?, $file?)
443 Generate a diff from a given L<Gitalist::Git::Object>.
445 =head2 reflog (@lorgargs)
447 Return a list of hashes representing each reflog entry.
449 FIXME Should this return objects?
454 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
459 See L<Gitalist> for authors.
463 See L<Gitalist> for the license.