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;
18 use Gitalist::Git::Head;
19 use Gitalist::Git::Tag;
21 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
23 around BUILDARGS (ClassName $class: Dir $dir) {
24 # Allows us to be called as Repository->new($dir)
25 # Last path component becomes $self->name
26 # Full path to git objects becomes $self->path
27 my $name = $dir->dir_list(-1);
28 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
29 confess("Can't find a git repository at " . $dir)
30 unless ( -f $dir->file('HEAD') );
31 return $class->$orig(name => $name,
35 has name => ( isa => NonEmptySimpleStr,
36 is => 'ro', required => 1 );
38 has path => ( isa => Dir,
39 is => 'ro', required => 1);
41 has description => ( isa => Str,
46 has owner => ( isa => NonEmptySimpleStr,
51 has last_change => ( isa => Maybe['DateTime'],
56 has is_bare => ( isa => Bool,
60 -d $_[0]->path->parent->subdir($_[0]->name)
64 has heads => ( isa => ArrayRef['Gitalist::Git::Head'],
67 has tags => ( isa => ArrayRef['Gitalist::Git::Tag'],
70 has references => ( isa => HashRef[ArrayRef[Str]],
75 $self->$_() for qw/last_change owner description /; # Ensure to build early.
80 method head_hash (Str $head?) {
81 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
82 confess("No such head: " . $head) unless defined $output;
84 my($sha1) = $output =~ /^($SHA1RE)$/;
88 method get_object (NonEmptySimpleStr $sha1) {
89 unless (is_SHA1($sha1)) {
90 $sha1 = $self->head_hash($sha1);
92 my $type = $self->run_cmd('cat-file', '-t', $sha1);
94 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
102 method list_revs ( NonEmptySimpleStr :$sha1!,
106 NonEmptySimpleStr :$file? ) {
107 $sha1 = $self->head_hash($sha1)
108 if !$sha1 || $sha1 !~ $SHA1RE;
111 if ($search and exists $search->{text}) {
112 $search->{type} = 'grep'
113 if $search->{type} eq 'commit';
115 # This seems a little fragile ...
116 qq[--$search->{type}=$search->{text}],
117 '--regexp-ignore-case',
118 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
122 my $output = $self->run_cmd(
125 (defined $count ? "--max-count=$count" : ()),
126 (defined $skip ? "--skip=$skip" : ()),
130 ($file ? $file : ()),
132 return unless $output;
134 my @revs = $self->_parse_rev_list($output);
139 method snapshot (NonEmptySimpleStr :$sha1,
140 NonEmptySimpleStr :$format
142 # TODO - only valid formats are 'tar' and 'zip'
143 my $formats = { tgz => 'tar', zip => 'zip' };
144 unless ($formats->exists($format)) {
145 die("No such format: $format");
147 $format = $formats->{$format};
148 my $name = $self->name;
149 $name =~ s,([^/])/*\.git$,$1,;
150 my $filename = $name;
151 $filename .= "-$sha1.$format";
152 $name =~ s/\047/\047\\\047\047/g;
154 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
155 return ($filename, $self->run_cmd_fh(@cmd));
156 # TODO - support compressed archives
159 method reflog (@logargs) {
161 = $self->run_cmd(qw(log -g), @logargs)
162 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
164 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
165 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
166 # Reflog message: push
167 # Author: Foo Barsby <fbarsby@example.com>
168 # Date: Thu Sep 17 12:26:05 2009 +0100
170 # Merge branch 'abc123'
173 # XXX Stuff like this makes me want to switch to Git::PurePerl
174 my($sha1, $type, $author, $date)
176 ^ commit \s+ ($SHA1RE)$
178 Reflog[ ]message: \s+ (.+?)$ \s+
179 Author: \s+ ([^<]+) <.*?$ \s+
183 pos($_) = index($_, $date) + length $date;
185 # Yeah, I just did that.
186 my($msg) = /\G\s+(\S.*)/sg;
192 # XXX Add DateTime goodness.
202 Gitalist::Git::Util->new(
207 method _build_description {
208 my $description = "";
210 $description = $self->path->file('description')->slurp;
213 $description = "Unnamed repository, edit the .git/description file to set a description"
214 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
218 method _build_owner {
219 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
221 return length($gecos) ? $gecos : $name;
224 method _build_last_change {
226 my $output = $self->run_cmd(
227 qw{ for-each-ref --format=%(committer)
228 --sort=-committerdate --count=1 refs/heads
230 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
231 my $dt = DateTime->from_epoch(epoch => $epoch);
232 $dt->set_time_zone($tz);
238 method _build_heads {
239 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
241 for my $line (@revlines) {
242 push @ret, Gitalist::Git::Head->new($line);
248 my @revlines = $self->run_cmd_list('for-each-ref',
249 '--sort=-creatordate',
250 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
254 for my $line (@revlines) {
255 push @ret, Gitalist::Git::Tag->new($line);
260 method _build_references {
261 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
262 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
263 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
267 push @{$refs{$1}}, $2
268 if m!^($SHA1RE)\srefs/(.*)$!;
275 method _parse_rev_list ($output) {
277 map $self->get_gpp_object($_),
279 map split(/\n/, $_, 6), split /\0/, $output;
288 Gitalist::Git::Repository - Model of a git repository
292 my $gitrepo = dir('/repo/base/Gitalist');
293 my $repository = Gitalist::Git::Repository->new($gitrepo);
294 $repository->name; # 'Gitalist'
295 $repository->path; # '/repo/base/Gitalist/.git'
296 $repository->description; # 'Unnamed repository.'
300 This class models a git repository, referred to in Gitalist
308 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
312 L<Path::Class:Dir> for the filesystem path to the git repository.
316 The contents of .git/description.
320 Owner of the files on the filesystem.
324 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
328 True if this is a bare git repository.
334 An array of the name and sha1 of all heads/tags in the repository.
338 Hashref of ArrayRefs for each reference.
343 =head2 head_hash ($head?)
345 Return the sha1 for HEAD, or any specified head.
347 =head2 get_object ($sha1)
349 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
351 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
353 Returns a list of revs for the given head ($sha1).
355 =head2 snapshot ($sha1, $format)
357 Generate an archived snapshot of the repository.
358 $sha1 should be a commit or tree.
359 Returns a filehandle to read from.
361 =head2 diff ($commit, $patch?, $parent?, $file?)
363 Generate a diff from a given L<Gitalist::Git::Object>.
365 =head2 reflog (@lorgargs)
367 Return a list of hashes representing each reflog entry.
369 FIXME Should this return objects?
374 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
379 See L<Gitalist> for authors.
383 See L<Gitalist> for the license.