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 Dir/;
9 use MooseX::Types::DateTime qw/ DateTime /;
12 use aliased 'DateTime' => 'DT';
13 use List::MoreUtils qw/any zip/;
14 use Encode qw/decode/;
16 use if $^O ne 'MSWin32' => 'I18N::Langinfo', qw/langinfo CODESET/;
18 use Gitalist::Git::Object::Blob;
19 use Gitalist::Git::Object::Tree;
20 use Gitalist::Git::Object::Commit;
21 use Gitalist::Git::Object::Tag;
22 use Gitalist::Git::Head;
23 use Gitalist::Git::Tag;
25 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
27 around BUILDARGS (ClassName $class: Dir $dir, Str $override_name = '') {
28 # Allows us to be called as Repository->new($dir)
29 # Last path component becomes $self->name
30 # Full path to git objects becomes $self->path
31 my $name = $dir->dir_list(-1);
32 if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git
33 $dir = $dir->subdir('.git');
34 $name = $dir->dir_list(-2, 1); # .../name/.git
35 } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
36 $name = $dir->dir_list(-2);
38 confess("Can't find a git repository at " . $dir)
39 unless -f $dir->file('HEAD');
40 return $class->$orig(name => $override_name || $name,
44 has name => ( isa => NonEmptySimpleStr,
45 is => 'ro', required => 1 );
47 has path => ( isa => Dir,
48 is => 'ro', required => 1,
49 traits => ['DoNotSerialize'] );
51 has description => ( isa => Str,
56 has owner => ( isa => NonEmptySimpleStr,
61 has last_change => ( isa => Maybe[DateTime],
66 has is_bare => ( isa => Bool,
70 -d $_[0]->path->parent->subdir($_[0]->name)
74 has heads => ( isa => ArrayRef['Gitalist::Git::Head'],
77 has tags => ( isa => ArrayRef['Gitalist::Git::Tag'],
80 has references => ( isa => HashRef[ArrayRef[Str]],
85 $self->$_() for qw/last_change owner description /; # Ensure to build early.
90 method head_hash (Str $head?) {
91 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
92 confess("No such head: " . $head) unless defined $output;
94 my($sha1) = $output =~ /^($SHA1RE)$/;
98 method get_object (NonEmptySimpleStr $sha1) {
99 unless (is_SHA1($sha1)) {
100 $sha1 = $self->head_hash($sha1);
102 my $type = $self->run_cmd('cat-file', '-t', $sha1);
104 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
112 method list_revs ( NonEmptySimpleStr :$sha1!,
116 NonEmptySimpleStr :$file? ) {
117 $sha1 = $self->head_hash($sha1)
118 if !$sha1 || $sha1 !~ $SHA1RE;
121 if ($search and exists $search->{text}) {
122 $search->{type} = 'grep'
123 if $search->{type} eq 'commit';
125 # This seems a little fragile ...
126 qq[--$search->{type}=$search->{text}],
127 '--regexp-ignore-case',
128 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
132 my $output = $self->run_cmd(
135 (defined $count ? "--max-count=$count" : ()),
136 (defined $skip ? "--skip=$skip" : ()),
140 ($file ? $file : ()),
142 return unless $output;
144 my @revs = $self->_parse_rev_list($output);
149 method snapshot (NonEmptySimpleStr :$sha1,
150 NonEmptySimpleStr :$format
152 # TODO - only valid formats are 'tar' and 'zip'
153 my $formats = { tgz => 'tar', zip => 'zip' };
154 unless ($formats->exists($format)) {
155 die("No such format: $format");
157 $format = $formats->{$format};
158 my $name = $self->name;
159 $name =~ s,([^/])/*\.git$,$1,;
160 my $filename = $name;
161 $filename .= "-$sha1.$format";
162 $name =~ s/\047/\047\\\047\047/g;
164 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
165 return ($filename, $self->run_cmd_fh(@cmd));
166 # TODO - support compressed archives
169 method reflog (@logargs) {
171 = $self->run_cmd(qw(log -g), @logargs)
172 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
174 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
175 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
176 # Reflog message: push
177 # Author: Foo Barsby <fbarsby@example.com>
178 # Date: Thu Sep 17 12:26:05 2009 +0100
180 # Merge branch 'abc123'
183 # XXX Stuff like this makes me want to switch to Git::PurePerl
184 my($sha1, $type, $author, $date)
186 ^ commit \s+ ($SHA1RE)$
188 Reflog[ ]message: \s+ (.+?)$ \s+
189 Author: \s+ ([^<]+) <.*?$ \s+
193 pos($_) = index($_, $date) + length $date;
195 # Yeah, I just did that.
196 my($msg) = /\G\s+(\S.*)/sg;
202 # XXX Add DateTime goodness.
212 Gitalist::Git::Util->new(
217 method _build_description {
218 my $description = "";
220 $description = $self->path->file('description')->slurp;
223 $description = "Unnamed repository, edit the .git/description file to set a description"
224 if $description eq "Unnamed repository; edit this file 'description' to name the repository.";
228 method _build_owner {
229 return 'system' if $^O =~ 'MSWin32';
231 my ($gecos, $name) = map { decode(langinfo(CODESET()), $_) } (getpwuid $self->path->stat->uid)[6,0];
233 return length($gecos) ? $gecos : $name;
236 method _build_last_change {
238 my $output = $self->run_cmd(
239 qw{ for-each-ref --format=%(committer)
240 --sort=-committerdate --count=1 refs/heads
242 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
243 my $dt = DT->from_epoch(epoch => $epoch);
244 $dt->set_time_zone($tz);
250 method _build_heads {
251 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
253 for my $line (@revlines) {
254 push @ret, Gitalist::Git::Head->new($line);
260 my @revlines = $self->run_cmd_list('for-each-ref',
261 '--sort=-creatordate',
262 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
266 map Gitalist::Git::Tag->new($_),
267 grep Gitalist::Git::Tag::is_valid_tag($_), @revlines
271 method _build_references {
272 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
273 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
274 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
278 push @{$refs{$1}}, $2
279 if m!^($SHA1RE)\srefs/(.*)$!;
286 method _parse_rev_list ($output) {
288 map $self->get_gpp_object($_),
290 map split(/\n/, $_, 6), split /\0/, $output;
299 Gitalist::Git::Repository - Model of a git repository
303 my $gitrepo = dir('/repo/base/Gitalist');
304 my $repository = Gitalist::Git::Repository->new($gitrepo);
305 $repository->name; # 'Gitalist'
306 $repository->path; # '/repo/base/Gitalist/.git'
307 $repository->description; # 'Unnamed repository.'
311 This class models a git repository, referred to in Gitalist
319 The name of the Repository. If unspecified, this will be derived from the path to the git repository.
323 L<Path::Class:Dir> for the filesystem path to the git repository.
327 The contents of .git/description.
331 Owner of the files on the filesystem.
335 The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
339 True if this is a bare git repository.
345 An array of the name and sha1 of all heads/tags in the repository.
349 Hashref of ArrayRefs for each reference.
354 =head2 head_hash ($head?)
356 Return the sha1 for HEAD, or any specified head.
358 =head2 get_object ($sha1)
360 Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
362 =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
364 Returns a list of revs for the given head ($sha1).
366 =head2 snapshot ($sha1, $format)
368 Generate an archived snapshot of the repository.
369 $sha1 should be a commit or tree.
370 Returns a filehandle to read from.
372 =head2 diff ($commit, $patch?, $parent?, $file?)
374 Generate a diff from a given L<Gitalist::Git::Object>.
376 =head2 reflog (@lorgargs)
378 Return a list of hashes representing each reflog entry.
380 FIXME Should this return objects?
385 L<Gitalist::Git::Util> L<Gitalist::Git::Object>
390 See L<Gitalist> for authors.
394 See L<Gitalist> for the license.