Add Apache specific section to docs.
[catagits/Gitalist.git] / lib / Gitalist / Git / Repository.pm
CommitLineData
56b6dbe6 1use MooseX::Declare;
2
5c07fcf1 3class Gitalist::Git::Repository with (Gitalist::Git::HasUtils, Gitalist::Git::Serializable) {
4 use MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize;
5
56b6dbe6 6 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
5c07fcf1 7 use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/;
8 use Gitalist::Git::Types qw/SHA1 DateTime Dir/;
9
2e79039a 10 use Moose::Autobox;
5c07fcf1 11 use aliased 'DateTime' => 'DT';
77edf882 12 use List::MoreUtils qw/any zip/;
5c07fcf1 13 use Encode qw/decode/;
14 use I18N::Langinfo qw/langinfo CODESET/;
15
f3083570 16 use Gitalist::Git::Object::Blob;
467fa7d9 17 use Gitalist::Git::Object::Tree;
18 use Gitalist::Git::Object::Commit;
19 use Gitalist::Git::Object::Tag;
03bf0cab 20 use Gitalist::Git::Head;
cc57f1d2 21 use Gitalist::Git::Tag;
56b6dbe6 22
32a371cc 23 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
24
634770da 25 around BUILDARGS (ClassName $class: Dir $dir, Str $override_name = '') {
44a9ed75 26 # Allows us to be called as Repository->new($dir)
4b6bcf44 27 # Last path component becomes $self->name
28 # Full path to git objects becomes $self->path
a9f6bdbe 29 my $name = $dir->dir_list(-1);
84f30d65 30 if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git
31 $dir = $dir->subdir('.git');
a9f6bdbe 32 $name = $dir->dir_list(-2, 1); # .../name/.git
84f30d65 33 } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git
a9f6bdbe 34 $name = $dir->dir_list(-2);
84f30d65 35 }
32a371cc 36 confess("Can't find a git repository at " . $dir)
a9f6bdbe 37 unless -f $dir->file('HEAD');
38 return $class->$orig(name => $override_name || $name,
32a371cc 39 path => $dir);
40 }
41
56b6dbe6 42 has name => ( isa => NonEmptySimpleStr,
01ced85b 43 is => 'ro', required => 1 );
b5b638f7 44
84f31a44 45 has path => ( isa => Dir,
5c07fcf1 46 is => 'ro', required => 1,
47 traits => ['DoNotSerialize'] );
56b6dbe6 48
0617cbd0 49 has description => ( isa => Str,
56b6dbe6 50 is => 'ro',
51 lazy_build => 1,
52 );
b5b638f7 53
56b6dbe6 54 has owner => ( isa => NonEmptySimpleStr,
55 is => 'ro',
56 lazy_build => 1,
57 );
b5b638f7 58
5c07fcf1 59 has last_change => ( isa => Maybe[DateTime],
56b6dbe6 60 is => 'ro',
61 lazy_build => 1,
62 );
63
b5b638f7 64 has is_bare => ( isa => Bool,
65 is => 'ro',
66 lazy => 1,
67 default => sub {
2c130350 68 -d $_[0]->path->parent->subdir($_[0]->name)
b5b638f7 69 ? 1 : 0
70 },
71 );
03bf0cab 72 has heads => ( isa => ArrayRef['Gitalist::Git::Head'],
cc88eca5 73 is => 'ro',
3a6dd969 74 lazy_build => 1);
cc57f1d2 75 has tags => ( isa => ArrayRef['Gitalist::Git::Tag'],
ea19a20c 76 is => 'ro',
77 lazy_build => 1);
cc88eca5 78 has references => ( isa => HashRef[ArrayRef[Str]],
79 is => 'ro',
80 lazy_build => 1 );
81
01ced85b 82 method BUILD {
4dacb4b2 83 $self->$_() for qw/last_change owner description /; # Ensure to build early.
01ced85b 84 }
85
bba40bd5 86 ## Public methods
dcb1b927 87
32a371cc 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;
29debefd 91
32a371cc 92 my($sha1) = $output =~ /^($SHA1RE)$/;
93 return $sha1;
56b6dbe6 94 }
95
8bb7649b 96 method get_object (NonEmptySimpleStr $sha1) {
ceef0cf1 97 unless (is_SHA1($sha1)) {
8bb7649b 98 $sha1 = $self->head_hash($sha1);
99 }
e1307124 100 my $type = $self->run_cmd('cat-file', '-t', $sha1);
101 chomp($type);
467fa7d9 102 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
103 $class->new(
82bc0f05 104 repository => $self,
54368e9d 105 sha1 => $sha1,
f3083570 106 type => $type,
54368e9d 107 );
108 }
8bb7649b 109
4111e151 110 method list_revs ( NonEmptySimpleStr :$sha1!,
111 Int :$count?,
112 Int :$skip?,
113 HashRef :$search?,
05e8b4d0 114 NonEmptySimpleStr :$file? ) {
4111e151 115 $sha1 = $self->head_hash($sha1)
116 if !$sha1 || $sha1 !~ $SHA1RE;
117
c1a9cd73 118 my @search_opts;
53a9d6de 119 if ($search and exists $search->{text}) {
4111e151 120 $search->{type} = 'grep'
121 if $search->{type} eq 'commit';
122 @search_opts = (
123 # This seems a little fragile ...
124 qq[--$search->{type}=$search->{text}],
125 '--regexp-ignore-case',
126 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
127 );
128 }
129
130 my $output = $self->run_cmd(
131 'rev-list',
132 '--header',
133 (defined $count ? "--max-count=$count" : ()),
134 (defined $skip ? "--skip=$skip" : ()),
135 @search_opts,
136 $sha1,
137 '--',
138 ($file ? $file : ()),
139 );
140 return unless $output;
141
cc88eca5 142 my @revs = $self->_parse_rev_list($output);
4111e151 143
144 return @revs;
145 }
146
bba40bd5 147 method snapshot (NonEmptySimpleStr :$sha1,
30db8f5b 148 NonEmptySimpleStr :$format
149 ) {
bba40bd5 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");
154 }
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;
161
162 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
163 return ($filename, $self->run_cmd_fh(@cmd));
164 # TODO - support compressed archives
2e79039a 165 }
54368e9d 166
d8abdf1c 167 method reflog (@logargs) {
168 my @entries
169 = $self->run_cmd(qw(log -g), @logargs)
170 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
171
05e8b4d0 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
177 #
178 # Merge branch 'abc123'
d8abdf1c 179
180 return map {
181 # XXX Stuff like this makes me want to switch to Git::PurePerl
182 my($sha1, $type, $author, $date)
183 = m{
184 ^ commit \s+ ($SHA1RE)$
185 .*?
186 Reflog[ ]message: \s+ (.+?)$ \s+
187 Author: \s+ ([^<]+) <.*?$ \s+
188 Date: \s+ (.+?)$
189 }xms;
190
191 pos($_) = index($_, $date) + length $date;
192
193 # Yeah, I just did that.
194 my($msg) = /\G\s+(\S.*)/sg;
195 {
196 hash => $sha1,
197 type => $type,
198 author => $author,
199
200 # XXX Add DateTime goodness.
201 date => $date,
202 message => $msg,
203 }
204 ;
205 } @entries;
206 }
207
cc88eca5 208 ## BUILDERS
50d45f00 209 method _build_util {
32a371cc 210 Gitalist::Git::Util->new(
82bc0f05 211 repository => $self,
32a371cc 212 );
213 }
214
215 method _build_description {
76c97bf6 216 my $description = "";
5d8568c6 217 eval {
76c97bf6 218 $description = $self->path->file('description')->slurp;
219 chomp $description;
5d8568c6 220 };
84f30d65 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.";
76c97bf6 223 return $description;
32a371cc 224 }
225
226 method _build_owner {
e66671b8 227 my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0];
32a371cc 228 $gecos =~ s/,+$//;
229 return length($gecos) ? $gecos : $name;
230 }
231
232 method _build_last_change {
233 my $last_change;
234 my $output = $self->run_cmd(
235 qw{ for-each-ref --format=%(committer)
236 --sort=-committerdate --count=1 refs/heads
237 });
238 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
5c07fcf1 239 my $dt = DT->from_epoch(epoch => $epoch);
32a371cc 240 $dt->set_time_zone($tz);
241 $last_change = $dt;
242 }
243 return $last_change;
244 }
245
cc88eca5 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');
248 my @ret;
249 for my $line (@revlines) {
72fbbef7 250 push @ret, Gitalist::Git::Head->new($line);
cc88eca5 251 }
cc88eca5 252 return \@ret;
253 }
254
ea19a20c 255 method _build_tags {
256 my @revlines = $self->run_cmd_list('for-each-ref',
257 '--sort=-creatordate',
258 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
c1a9cd73 259 'refs/tags'
ea19a20c 260 );
84f30d65 261 return [
262 map Gitalist::Git::Tag->new($_),
263 grep Gitalist::Git::Tag::is_valid_tag($_), @revlines
264 ];
ea19a20c 265 }
266
cc88eca5 267 method _build_references {
c1a9cd73 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))
05e8b4d0 271 or return;
cc88eca5 272 my %refs;
05e8b4d0 273 for (@reflist) {
274 push @{$refs{$1}}, $2
275 if m!^($SHA1RE)\srefs/(.*)$!;
276 }
cc88eca5 277
05e8b4d0 278 return \%refs;
cc88eca5 279 }
280
281 ## Private methods
cc88eca5 282 method _parse_rev_list ($output) {
283 return
284 map $self->get_gpp_object($_),
ceef0cf1 285 grep is_SHA1($_),
cc88eca5 286 map split(/\n/, $_, 6), split /\0/, $output;
287 }
288
775e96e0 289} # end class
290
291__END__
292
bba40bd5 293=head1 NAME
294
44a9ed75 295Gitalist::Git::Repository - Model of a git repository
bba40bd5 296
297=head1 SYNOPSIS
298
299 my $gitrepo = dir('/repo/base/Gitalist');
82bc0f05 300 my $repository = Gitalist::Git::Repository->new($gitrepo);
301 $repository->name; # 'Gitalist'
302 $repository->path; # '/repo/base/Gitalist/.git'
303 $repository->description; # 'Unnamed repository.'
bba40bd5 304
305=head1 DESCRIPTION
306
307This class models a git repository, referred to in Gitalist
87581f05 308as a "Repository".
bba40bd5 309
8ba87261 310
bba40bd5 311=head1 ATTRIBUTES
312
313=head2 name
314
87581f05 315The name of the Repository. If unspecified, this will be derived from the path to the git repository.
bba40bd5 316
bba40bd5 317=head2 path
318
8ba87261 319L<Path::Class:Dir> for the filesystem path to the git repository.
bba40bd5 320
bba40bd5 321=head2 description
322
8ba87261 323The contents of .git/description.
bba40bd5 324
bba40bd5 325=head2 owner
326
8ba87261 327Owner of the files on the filesystem.
bba40bd5 328
bba40bd5 329=head2 last_change
330
8ba87261 331The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
bba40bd5 332
bba40bd5 333=head2 is_bare
334
8ba87261 335True if this is a bare git repository.
bba40bd5 336
bba40bd5 337=head2 heads
338
bba40bd5 339=head2 tags
340
8ba87261 341An array of the name and sha1 of all heads/tags in the repository.
bba40bd5 342
343=head2 references
344
345Hashref of ArrayRefs for each reference.
346
8ba87261 347
bba40bd5 348=head1 METHODS
349
350=head2 head_hash ($head?)
351
352Return the sha1 for HEAD, or any specified head.
353
bba40bd5 354=head2 get_object ($sha1)
355
356Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
357
8ba87261 358=head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
bba40bd5 359
360Returns a list of revs for the given head ($sha1).
361
8ba87261 362=head2 snapshot ($sha1, $format)
bba40bd5 363
364Generate an archived snapshot of the repository.
365$sha1 should be a commit or tree.
366Returns a filehandle to read from.
367
8ba87261 368=head2 diff ($commit, $patch?, $parent?, $file?)
bba40bd5 369
370Generate a diff from a given L<Gitalist::Git::Object>.
371
8ba87261 372=head2 reflog (@lorgargs)
bba40bd5 373
374Return a list of hashes representing each reflog entry.
375
376FIXME Should this return objects?
377
bba40bd5 378
b5b638f7 379=head1 SEE ALSO
380
381L<Gitalist::Git::Util> L<Gitalist::Git::Object>
382
8ba87261 383
775e96e0 384=head1 AUTHORS
b5b638f7 385
775e96e0 386See L<Gitalist> for authors.
b5b638f7 387
388=head1 LICENSE
389
775e96e0 390See L<Gitalist> for the license.
b5b638f7 391
392=cut