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 if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git
29 $dir = $dir->subdir('.git');
30 $name = $dir->dir_list(-2, 1); # .../name/.git
31 } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
32 $name = $dir->dir_list(-2);
34 confess("Can't find a git repository at " . $dir)
35 unless ( -f $dir->file('HEAD') );
36 return $class->$orig(name => $name,
40 has name => ( isa => NonEmptySimpleStr,
41 is => 'ro', required => 1 );
43 has path => ( isa => Dir,
44 is => 'ro', required => 1);
46 has description => ( isa => Str,
51 has owner => ( isa => NonEmptySimpleStr,
56 has last_change => ( isa => Maybe['DateTime'],
61 has is_bare => ( isa => Bool,
65 -d $_[0]->path->parent->subdir($_[0]->name)
69 has heads => ( isa => ArrayRef['Gitalist::Git::Head'],
72 has tags => ( isa => ArrayRef['Gitalist::Git::Tag'],
75 has references => ( isa => HashRef[ArrayRef[Str]],
80 $self->$_() for qw/last_change owner description /; # Ensure to build early.
85 method head_hash (Str $head?) {
86 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
87 confess("No such head: " . $head) unless defined $output;
89 my($sha1) = $output =~ /^($SHA1RE)$/;
93 method get_object (NonEmptySimpleStr $sha1) {
94 unless (is_SHA1($sha1)) {
95 $sha1 = $self->head_hash($sha1);
97 my $type = $self->run_cmd('cat-file', '-t', $sha1);
99 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
107 method list_revs ( NonEmptySimpleStr :$sha1!,
111 NonEmptySimpleStr :$file? ) {
112 $sha1 = $self->head_hash($sha1)
113 if !$sha1 || $sha1 !~ $SHA1RE;
116 if ($search and exists $search->{text}) {
117 $search->{type} = 'grep'
118 if $search->{type} eq 'commit';
120 # This seems a little fragile ...
121 qq[--$search->{type}=$search->{text}],
122 '--regexp-ignore-case',
123 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
127 my $output = $self->run_cmd(
130 (defined $count ? "--max-count=$count" : ()),
131 (defined $skip ? "--skip=$skip" : ()),
135 ($file ? $file : ()),
137 return unless $output;
139 my @revs = $self->_parse_rev_list($output);
144 method snapshot (NonEmptySimpleStr :$sha1,
145 NonEmptySimpleStr :$format
147 # TODO - only valid formats are 'tar' and 'zip'
148 my $formats = { tgz => 'tar', zip => 'zip' };
149 unless ($formats->exists($format)) {
150 die("No such format: $format");
152 $format = $formats->{$format};
153 my $name = $self->name;
154 $name =~ s,([^/])/*\.git$,$1,;
155 my $filename = $name;
156 $filename .= "-$sha1.$format";
157 $name =~ s/\047/\047\\\047\047/g;
159 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
160 return ($filename, $self->run_cmd_fh(@cmd));
161 # TODO - support compressed archives
164 method reflog (@logargs) {
166 = $self->run_cmd(qw(log -g), @logargs)
167 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
169 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
170 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
171 # Reflog message: push
172 # Author: Foo Barsby <fbarsby@example.com>
173 # Date: Thu Sep 17 12:26:05 2009 +0100
175 # Merge branch 'abc123'
178 # XXX Stuff like this makes me want to switch to Git::PurePerl
179 my($sha1, $type, $author, $date)
181 ^ commit \s+ ($SHA1RE)$
183 Reflog[ ]message: \s+ (.+?)$ \s+
184 Author: \s+ ([^<]+) <.*?$ \s+
188 pos($_) = index($_, $date) + length $date;
190 # Yeah, I just did that.
191 my($msg) = /\G\s+(\S.*)/sg;
197 # XXX Add DateTime goodness.
207 Gitalist::Git::Util->new(
212 method _build_description {
213 my $description = "";
215 $description = $self->path->file('description')->slurp;
218 $description = "Unnamed repository, edit the .git/description file to set a description"
219 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
223 method _build_owner {
224 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
226 return length($gecos) ? $gecos : $name;
229 method _build_last_change {
231 my $output = $self->run_cmd(
232 qw{ for-each-ref --format=%(committer)
233 --sort=-committerdate --count=1 refs/heads
235 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
236 my $dt = DateTime->from_epoch(epoch => $epoch);
237 $dt->set_time_zone($tz);
243 method _build_heads {
244 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
246 for my $line (@revlines) {
247 push @ret, Gitalist::Git::Head->new($line);
253 my @revlines = $self->run_cmd_list('for-each-ref',
254 '--sort=-creatordate',
255 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
259 map Gitalist::Git::Tag->new($_),
260 grep Gitalist::Git::Tag::is_valid_tag($_), @revlines
264 method _build_references {
265 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
266 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
267 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
271 push @{$refs{$1}}, $2
272 if m!^($SHA1RE)\srefs/(.*)$!;
279 method _parse_rev_list ($output) {
281 map $self->get_gpp_object($_),
283 map split(/\n/, $_, 6), split /\0/, $output;
292 Gitalist::Git::Repository - Model of a git repository
296 my $gitrepo = dir('/repo/base/Gitalist');
297 my $repository = Gitalist::Git::Repository->new($gitrepo);
298 $repository->name; # 'Gitalist'
299 $repository->path; # '/repo/base/Gitalist/.git'
300 $repository->description; # 'Unnamed repository.'
304 This class models a git repository, referred to in Gitalist
312 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
316 L<Path::Class:Dir> for the filesystem path to the git repository.
320 The contents of .git/description.
324 Owner of the files on the filesystem.
328 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
332 True if this is a bare git repository.
338 An array of the name and sha1 of all heads/tags in the repository.
342 Hashref of ArrayRefs for each reference.
347 =head2 head_hash ($head?)
349 Return the sha1 for HEAD, or any specified head.
351 =head2 get_object ($sha1)
353 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
355 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
357 Returns a list of revs for the given head ($sha1).
359 =head2 snapshot ($sha1, $format)
361 Generate an archived snapshot of the repository.
362 $sha1 should be a commit or tree.
363 Returns a filehandle to read from.
365 =head2 diff ($commit, $patch?, $parent?, $file?)
367 Generate a diff from a given L<Gitalist::Git::Object>.
369 =head2 reflog (@lorgargs)
371 Return a list of hashes representing each reflog entry.
373 FIXME Should this return objects?
378 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
383 See L<Gitalist> for authors.
387 See L<Gitalist> for the license.