3 class Gitalist::Git::Repository with (Gitalist::Git::HasUtils, Gitalist::Git::Serializable) {
4 use MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize;
6 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
7 use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/;
8 use Gitalist::Git::Types qw/SHA1 DateTime Dir/;
11 use aliased 'DateTime' => 'DT';
12 use List::MoreUtils qw/any zip/;
13 use Encode qw/decode/;
14 use I18N::Langinfo qw/langinfo CODESET/;
16 use Gitalist::Git::Object::Blob;
17 use Gitalist::Git::Object::Tree;
18 use Gitalist::Git::Object::Commit;
19 use Gitalist::Git::Object::Tag;
20 use Gitalist::Git::Head;
21 use Gitalist::Git::Tag;
23 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
25 around BUILDARGS (ClassName $class: Dir $dir, Str $override_name = '') {
26 # Allows us to be called as Repository->new($dir)
27 # Last path component becomes $self->name
28 # Full path to git objects becomes $self->path
29 my $name = $dir->dir_list(-1);
30 if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git
31 $dir = $dir->subdir('.git');
32 $name = $dir->dir_list(-2, 1); # .../name/.git
33 } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
34 $name = $dir->dir_list(-2);
36 confess("Can't find a git repository at " . $dir)
37 unless -f $dir->file('HEAD');
38 return $class->$orig(name => $override_name || $name,
42 has name => ( isa => NonEmptySimpleStr,
43 is => 'ro', required => 1 );
45 has path => ( isa => Dir,
46 is => 'ro', required => 1,
47 traits => ['DoNotSerialize'] );
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['Gitalist::Git::Head'],
75 has tags => ( isa => ArrayRef['Gitalist::Git::Tag'],
78 has references => ( isa => HashRef[ArrayRef[Str]],
83 $self->$_() for qw/last_change owner description /; # Ensure to build early.
88 method head_hash (Str $head?) {
89 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
90 confess("No such head: " . $head) unless defined $output;
92 my($sha1) = $output =~ /^($SHA1RE)$/;
96 method get_object (NonEmptySimpleStr $sha1) {
97 unless (is_SHA1($sha1)) {
98 $sha1 = $self->head_hash($sha1);
100 my $type = $self->run_cmd('cat-file', '-t', $sha1);
102 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
110 method list_revs ( NonEmptySimpleStr :$sha1!,
114 NonEmptySimpleStr :$file? ) {
115 $sha1 = $self->head_hash($sha1)
116 if !$sha1 || $sha1 !~ $SHA1RE;
119 if ($search and exists $search->{text}) {
120 $search->{type} = 'grep'
121 if $search->{type} eq 'commit';
123 # This seems a little fragile ...
124 qq[--$search->{type}=$search->{text}],
125 '--regexp-ignore-case',
126 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
130 my $output = $self->run_cmd(
133 (defined $count ? "--max-count=$count" : ()),
134 (defined $skip ? "--skip=$skip" : ()),
138 ($file ? $file : ()),
140 return unless $output;
142 my @revs = $self->_parse_rev_list($output);
147 method snapshot (NonEmptySimpleStr :$sha1,
148 NonEmptySimpleStr :$format
150 # TODO - only valid formats are 'tar' and 'zip'
151 my $formats = { tgz => 'tar', zip => 'zip' };
152 unless ($formats->exists($format)) {
153 die("No such format: $format");
155 $format = $formats->{$format};
156 my $name = $self->name;
157 $name =~ s,([^/])/*\.git$,$1,;
158 my $filename = $name;
159 $filename .= "-$sha1.$format";
160 $name =~ s/\047/\047\\\047\047/g;
162 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
163 return ($filename, $self->run_cmd_fh(@cmd));
164 # TODO - support compressed archives
167 method reflog (@logargs) {
169 = $self->run_cmd(qw(log -g), @logargs)
170 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
172 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
173 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
174 # Reflog message: push
175 # Author: Foo Barsby <fbarsby@example.com>
176 # Date: Thu Sep 17 12:26:05 2009 +0100
178 # Merge branch 'abc123'
181 # XXX Stuff like this makes me want to switch to Git::PurePerl
182 my($sha1, $type, $author, $date)
184 ^ commit \s+ ($SHA1RE)$
186 Reflog[ ]message: \s+ (.+?)$ \s+
187 Author: \s+ ([^<]+) <.*?$ \s+
191 pos($_) = index($_, $date) + length $date;
193 # Yeah, I just did that.
194 my($msg) = /\G\s+(\S.*)/sg;
200 # XXX Add DateTime goodness.
210 Gitalist::Git::Util->new(
215 method _build_description {
216 my $description = "";
218 $description = $self->path->file('description')->slurp;
221 $description = "Unnamed repository, edit the .git/description file to set a description"
222 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
226 method _build_owner {
227 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
229 return length($gecos) ? $gecos : $name;
232 method _build_last_change {
234 my $output = $self->run_cmd(
235 qw{ for-each-ref --format=%(committer)
236 --sort=-committerdate --count=1 refs/heads
238 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
239 my $dt = DT->from_epoch(epoch => $epoch);
240 $dt->set_time_zone($tz);
246 method _build_heads {
247 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
249 for my $line (@revlines) {
250 push @ret, Gitalist::Git::Head->new($line);
256 my @revlines = $self->run_cmd_list('for-each-ref',
257 '--sort=-creatordate',
258 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
262 map Gitalist::Git::Tag->new($_),
263 grep Gitalist::Git::Tag::is_valid_tag($_), @revlines
267 method _build_references {
268 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
269 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
270 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
274 push @{$refs{$1}}, $2
275 if m!^($SHA1RE)\srefs/(.*)$!;
282 method _parse_rev_list ($output) {
284 map $self->get_gpp_object($_),
286 map split(/\n/, $_, 6), split /\0/, $output;
295 Gitalist::Git::Repository - Model of a git repository
299 my $gitrepo = dir('/repo/base/Gitalist');
300 my $repository = Gitalist::Git::Repository->new($gitrepo);
301 $repository->name; # 'Gitalist'
302 $repository->path; # '/repo/base/Gitalist/.git'
303 $repository->description; # 'Unnamed repository.'
307 This class models a git repository, referred to in Gitalist
315 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
319 L<Path::Class:Dir> for the filesystem path to the git repository.
323 The contents of .git/description.
327 Owner of the files on the filesystem.
331 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
335 True if this is a bare git repository.
341 An array of the name and sha1 of all heads/tags in the repository.
345 Hashref of ArrayRefs for each reference.
350 =head2 head_hash ($head?)
352 Return the sha1 for HEAD, or any specified head.
354 =head2 get_object ($sha1)
356 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
358 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
360 Returns a list of revs for the given head ($sha1).
362 =head2 snapshot ($sha1, $format)
364 Generate an archived snapshot of the repository.
365 $sha1 should be a commit or tree.
366 Returns a filehandle to read from.
368 =head2 diff ($commit, $patch?, $parent?, $file?)
370 Generate a diff from a given L<Gitalist::Git::Object>.
372 =head2 reflog (@lorgargs)
374 Return a list of hashes representing each reflog entry.
376 FIXME Should this return objects?
381 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
386 See L<Gitalist> for authors.
390 See L<Gitalist> for the license.