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.
78 method head_hash (Str $head?) {
79 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
80 confess("No such head: " . $head) unless defined $output;
82 my($sha1) = $output =~ /^($SHA1RE)$/;
86 method get_object (NonEmptySimpleStr $sha1) {
87 unless (is_SHA1($sha1)) {
88 $sha1 = $self->head_hash($sha1);
90 my $type = $self->run_cmd('cat-file', '-t', $sha1);
92 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
100 method list_revs ( NonEmptySimpleStr :$sha1!,
104 NonEmptySimpleStr :$file? ) {
105 $sha1 = $self->head_hash($sha1)
106 if !$sha1 || $sha1 !~ $SHA1RE;
109 if ($search and exists $search->{text}) {
110 $search->{type} = 'grep'
111 if $search->{type} eq 'commit';
113 # This seems a little fragile ...
114 qq[--$search->{type}=$search->{text}],
115 '--regexp-ignore-case',
116 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
120 my $output = $self->run_cmd(
123 (defined $count ? "--max-count=$count" : ()),
124 (defined $skip ? "--skip=$skip" : ()),
128 ($file ? $file : ()),
130 return unless $output;
132 my @revs = $self->_parse_rev_list($output);
137 method snapshot (NonEmptySimpleStr :$sha1,
138 NonEmptySimpleStr :$format
140 # TODO - only valid formats are 'tar' and 'zip'
141 my $formats = { tgz => 'tar', zip => 'zip' };
142 unless ($formats->exists($format)) {
143 die("No such format: $format");
145 $format = $formats->{$format};
146 my $name = $self->name;
147 $name =~ s,([^/])/*\.git$,$1,;
148 my $filename = $name;
149 $filename .= "-$sha1.$format";
150 $name =~ s/\047/\047\\\047\047/g;
152 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
153 return ($filename, $self->run_cmd_fh(@cmd));
154 # TODO - support compressed archives
157 method reflog (@logargs) {
159 = $self->run_cmd(qw(log -g), @logargs)
160 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
162 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
163 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
164 # Reflog message: push
165 # Author: Foo Barsby <fbarsby@example.com>
166 # Date: Thu Sep 17 12:26:05 2009 +0100
168 # Merge branch 'abc123'
171 # XXX Stuff like this makes me want to switch to Git::PurePerl
172 my($sha1, $type, $author, $date)
174 ^ commit \s+ ($SHA1RE)$
176 Reflog[ ]message: \s+ (.+?)$ \s+
177 Author: \s+ ([^<]+) <.*?$ \s+
181 pos($_) = index($_, $date) + length $date;
183 # Yeah, I just did that.
184 my($msg) = /\G\s+(\S.*)/sg;
190 # XXX Add DateTime goodness.
200 Gitalist::Git::Util->new(
205 method _build_description {
206 my $description = "";
208 $description = $self->path->file('description')->slurp;
211 $description = "Unnamed repository, edit the .git/description file to set a description"
212 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
216 method _build_owner {
217 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
219 return length($gecos) ? $gecos : $name;
222 method _build_last_change {
224 my $output = $self->run_cmd(
225 qw{ for-each-ref --format=%(committer)
226 --sort=-committerdate --count=1 refs/heads
228 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
229 my $dt = DateTime->from_epoch(epoch => $epoch);
230 $dt->set_time_zone($tz);
236 method _build_heads {
237 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
239 for my $line (@revlines) {
240 my ($rev, $head, $commiter) = split /\0/, $line, 3;
241 $head =~ s!^refs/heads/!!;
243 push @ret, { sha1 => $rev, name => $head };
245 #FIXME: That isn't the time I'm looking for..
246 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
247 my $dt = DateTime->from_epoch(epoch => $epoch);
248 $dt->set_time_zone($tz);
249 $ret[-1]->{last_change} = $dt;
257 my @revlines = $self->run_cmd_list('for-each-ref',
258 '--sort=-creatordate',
259 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
263 for my $line (@revlines) {
264 my($refinfo, $creatorinfo) = split /\0/, $line;
265 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
266 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
267 $name =~ s!^refs/tags/!!;
269 push @ret, { sha1 => $rev, name => $name };
271 #FIXME: That isn't the time I'm looking for..
273 my $dt = DateTime->from_epoch(epoch => $epoch);
274 $dt->set_time_zone($tz);
275 $ret[-1]->{last_change} = $dt;
282 method _build_references {
283 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
284 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
285 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
289 push @{$refs{$1}}, $2
290 if m!^($SHA1RE)\srefs/(.*)$!;
297 method _parse_rev_list ($output) {
299 map $self->get_gpp_object($_),
301 map split(/\n/, $_, 6), split /\0/, $output;
310 Gitalist::Git::Repository - Model of a git repository
314 my $gitrepo = dir('/repo/base/Gitalist');
315 my $repository = Gitalist::Git::Repository->new($gitrepo);
316 $repository->name; # 'Gitalist'
317 $repository->path; # '/repo/base/Gitalist/.git'
318 $repository->description; # 'Unnamed repository.'
322 This class models a git repository, referred to in Gitalist
330 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
334 L<Path::Class:Dir> for the filesystem path to the git repository.
338 The contents of .git/description.
342 Owner of the files on the filesystem.
346 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
350 True if this is a bare git repository.
356 An array of the name and sha1 of all heads/tags in the repository.
360 Hashref of ArrayRefs for each reference.
365 =head2 head_hash ($head?)
367 Return the sha1 for HEAD, or any specified head.
369 =head2 get_object ($sha1)
371 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
373 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
375 Returns a list of revs for the given head ($sha1).
377 =head2 snapshot ($sha1, $format)
379 Generate an archived snapshot of the repository.
380 $sha1 should be a commit or tree.
381 Returns a filehandle to read from.
383 =head2 diff ($commit, $patch?, $parent?, $file?)
385 Generate a diff from a given L<Gitalist::Git::Object>.
387 =head2 reflog (@lorgargs)
389 Return a list of hashes representing each reflog entry.
391 FIXME Should this return objects?
396 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
401 See L<Gitalist> for authors.
405 See L<Gitalist> for the license.