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/;
10 use List::MoreUtils qw/any zip/;
12 use Encode qw/decode/;
13 use I18N::Langinfo qw/langinfo CODESET/;
14 use Gitalist::Git::Object::Blob;
15 use Gitalist::Git::Object::Tree;
16 use Gitalist::Git::Object::Commit;
17 use Gitalist::Git::Object::Tag;
19 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
21 around BUILDARGS (ClassName $class: Dir $dir) {
22 # Allows us to be called as Repository->new($dir)
23 # Last path component becomes $self->name
24 # Full path to git objects becomes $self->path
25 my $name = $dir->dir_list(-1);
26 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
27 confess("Can't find a git repository at " . $dir)
28 unless ( -f $dir->file('HEAD') );
29 return $class->$orig(name => $name,
33 has name => ( isa => NonEmptySimpleStr,
34 is => 'ro', required => 1 );
36 has path => ( isa => Dir,
37 is => 'ro', required => 1);
39 has description => ( isa => Str,
44 has owner => ( isa => NonEmptySimpleStr,
49 has last_change => ( isa => Maybe['DateTime'],
54 has is_bare => ( isa => Bool,
58 -d $_[0]->path->parent->subdir->($_[0]->name)
62 has heads => ( isa => ArrayRef[HashRef],
65 has tags => ( isa => ArrayRef[HashRef],
68 has references => ( isa => HashRef[ArrayRef[Str]],
73 $self->$_() for qw/last_change owner description/; # Ensure to build early.
77 method head_hash (Str $head?) {
78 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
79 confess("No such head: " . $head) unless defined $output;
81 my($sha1) = $output =~ /^($SHA1RE)$/;
85 method list_tree (SHA1 $sha1?) {
86 $sha1 ||= $self->head_hash;
87 my $object = $self->get_object($sha1);
88 return @{$object->tree};
91 method get_object (NonEmptySimpleStr $sha1) {
92 unless (is_SHA1($sha1)) {
93 $sha1 = $self->head_hash($sha1);
95 my $type = $self->run_cmd('cat-file', '-t', $sha1);
97 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
105 method hash_by_path ($base, $path = '', $type?) {
107 # FIXME should this really just take the first result?
108 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
110 my $line = $paths[0];
112 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
113 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
114 return defined $type && $type ne $2
119 method list_revs ( NonEmptySimpleStr :$sha1!,
123 NonEmptySimpleStr :$file? ) {
124 $sha1 = $self->head_hash($sha1)
125 if !$sha1 || $sha1 !~ $SHA1RE;
129 $search->{type} = 'grep'
130 if $search->{type} eq 'commit';
132 # This seems a little fragile ...
133 qq[--$search->{type}=$search->{text}],
134 '--regexp-ignore-case',
135 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
139 my $output = $self->run_cmd(
142 (defined $count ? "--max-count=$count" : ()),
143 (defined $skip ? "--skip=$skip" : ()),
147 ($file ? $file : ()),
149 return unless $output;
151 my @revs = $self->_parse_rev_list($output);
156 method snapshot (NonEmptySimpleStr :$sha1,
157 NonEmptySimpleStr :$format
159 # TODO - only valid formats are 'tar' and 'zip'
160 my $formats = { tgz => 'tar', zip => 'zip' };
161 unless ($formats->exists($format)) {
162 die("No such format: $format");
164 $format = $formats->{$format};
165 my $name = $self->name;
166 $name =~ s,([^/])/*\.git$,$1,;
167 my $filename = $name;
168 $filename .= "-$sha1.$format";
169 $name =~ s/\047/\047\\\047\047/g;
171 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
172 return ($filename, $self->run_cmd_fh(@cmd));
173 # TODO - support compressed archives
176 method diff ( Gitalist::Git::Object :$commit!,
178 Maybe[NonEmptySimpleStr] :$parent?,
179 NonEmptySimpleStr :$file?
181 return $commit->diff( patch => $patch,
186 method reflog (@logargs) {
188 = $self->run_cmd(qw(log -g), @logargs)
189 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
191 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
192 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
193 # Reflog message: push
194 # Author: Foo Barsby <fbarsby@example.com>
195 # Date: Thu Sep 17 12:26:05 2009 +0100
197 # Merge branch 'abc123'
200 # XXX Stuff like this makes me want to switch to Git::PurePerl
201 my($sha1, $type, $author, $date)
203 ^ commit \s+ ($SHA1RE)$
205 Reflog[ ]message: \s+ (.+?)$ \s+
206 Author: \s+ ([^<]+) <.*?$ \s+
210 pos($_) = index($_, $date) + length $date;
212 # Yeah, I just did that.
213 my($msg) = /\G\s+(\S.*)/sg;
219 # XXX Add DateTime goodness.
229 Gitalist::Git::Util->new(
234 method _build_description {
235 my $description = "";
237 $description = $self->path->file('description')->slurp;
243 method _build_owner {
244 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
246 return length($gecos) ? $gecos : $name;
249 method _build_last_change {
251 my $output = $self->run_cmd(
252 qw{ for-each-ref --format=%(committer)
253 --sort=-committerdate --count=1 refs/heads
255 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
256 my $dt = DateTime->from_epoch(epoch => $epoch);
257 $dt->set_time_zone($tz);
263 method _build_heads {
264 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
266 for my $line (@revlines) {
267 my ($rev, $head, $commiter) = split /\0/, $line, 3;
268 $head =~ s!^refs/heads/!!;
270 push @ret, { sha1 => $rev, name => $head };
272 #FIXME: That isn't the time I'm looking for..
273 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
274 my $dt = DateTime->from_epoch(epoch => $epoch);
275 $dt->set_time_zone($tz);
276 $ret[-1]->{last_change} = $dt;
284 my @revlines = $self->run_cmd_list('for-each-ref',
285 '--sort=-creatordate',
286 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
290 for my $line (@revlines) {
291 my($refinfo, $creatorinfo) = split /\0/, $line;
292 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
293 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
294 $name =~ s!^refs/tags/!!;
296 push @ret, { sha1 => $rev, name => $name };
298 #FIXME: That isn't the time I'm looking for..
300 my $dt = DateTime->from_epoch(epoch => $epoch);
301 $dt->set_time_zone($tz);
302 $ret[-1]->{last_change} = $dt;
309 method _build_references {
310 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
311 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
312 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
316 push @{$refs{$1}}, $2
317 if m!^($SHA1RE)\srefs/(.*)$!;
324 method _parse_rev_list ($output) {
326 map $self->get_gpp_object($_),
328 map split(/\n/, $_, 6), split /\0/, $output;
337 Gitalist::Git::Repository - Model of a git repository
341 my $gitrepo = dir('/repo/base/Gitalist');
342 my $repository = Gitalist::Git::Repository->new($gitrepo);
343 $repository->name; # 'Gitalist'
344 $repository->path; # '/repo/base/Gitalist/.git'
345 $repository->description; # 'Unnamed repository.'
349 This class models a git repository, referred to in Gitalist
357 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
361 L<Path::Class:Dir> for the filesystem path to the git repository.
365 The contents of .git/description.
369 Owner of the files on the filesystem.
373 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
377 True if this is a bare git repository.
383 An array of the name and sha1 of all heads/tags in the repository.
387 Hashref of ArrayRefs for each reference.
392 =head2 head_hash ($head?)
394 Return the sha1 for HEAD, or any specified head.
396 =head2 list_tree ($sha1?)
398 Return an array of contents for a given tree.
399 The tree is specified by sha1, and defaults to HEAD.
400 Each item is a L<Gitalist::Git::Object>.
402 =head2 get_object ($sha1)
404 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
406 =head2 hash_by_path ($sha1, $path, $type?)
408 Returns the sha1 for a given path, optionally limited by type.
410 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
412 Returns a list of revs for the given head ($sha1).
414 =head2 snapshot ($sha1, $format)
416 Generate an archived snapshot of the repository.
417 $sha1 should be a commit or tree.
418 Returns a filehandle to read from.
420 =head2 diff ($commit, $patch?, $parent?, $file?)
422 Generate a diff from a given L<Gitalist::Git::Object>.
424 =head2 reflog (@lorgargs)
426 Return a list of hashes representing each reflog entry.
428 FIXME Should this return objects?
433 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
438 See L<Gitalist> for authors.
442 See L<Gitalist> for the license.