Improved Repo's POD.
[catagits/Gitalist.git] / lib / Gitalist / Git / Project.pm
CommitLineData
56b6dbe6 1use MooseX::Declare;
2
38b9e5c8 3class Gitalist::Git::Project with Gitalist::Git::HasUtils {
56b6dbe6 4 # FIXME, use Types::Path::Class and coerce
5 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
84f31a44 6 use MooseX::Types::Path::Class qw/Dir/;
829d41ed 7 use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/;
2e79039a 8 use Moose::Autobox;
77edf882 9 use List::MoreUtils qw/any zip/;
b5b638f7 10 use DateTime;
f3083570 11 use Gitalist::Git::Object::Blob;
467fa7d9 12 use Gitalist::Git::Object::Tree;
13 use Gitalist::Git::Object::Commit;
14 use Gitalist::Git::Object::Tag;
56b6dbe6 15
32a371cc 16 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
17
18 around BUILDARGS (ClassName $class: Dir $dir) {
4b6bcf44 19 # Allows us to be called as Project->new($dir)
20 # Last path component becomes $self->name
21 # Full path to git objects becomes $self->path
32a371cc 22 my $name = $dir->dir_list(-1);
23 $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD'));
24 confess("Can't find a git repository at " . $dir)
25 unless ( -f $dir->file('HEAD') );
26 return $class->$orig(name => $name,
27 path => $dir);
28 }
29
56b6dbe6 30 has name => ( isa => NonEmptySimpleStr,
01ced85b 31 is => 'ro', required => 1 );
b5b638f7 32
84f31a44 33 has path => ( isa => Dir,
01ced85b 34 is => 'ro', required => 1);
56b6dbe6 35
0617cbd0 36 has description => ( isa => Str,
56b6dbe6 37 is => 'ro',
38 lazy_build => 1,
39 );
b5b638f7 40
56b6dbe6 41 has owner => ( isa => NonEmptySimpleStr,
42 is => 'ro',
43 lazy_build => 1,
44 );
b5b638f7 45
0617cbd0 46 has last_change => ( isa => Maybe['DateTime'],
56b6dbe6 47 is => 'ro',
48 lazy_build => 1,
49 );
50
b5b638f7 51 has is_bare => ( isa => Bool,
52 is => 'ro',
53 lazy => 1,
54 default => sub {
55 -d $_[0]->path->parent->subdir->($_[0]->name)
56 ? 1 : 0
57 },
58 );
cc88eca5 59 has heads => ( isa => ArrayRef[HashRef],
60 is => 'ro',
61 lazy_build => 1);
ea19a20c 62 has tags => ( isa => ArrayRef[HashRef],
63 is => 'ro',
64 lazy_build => 1);
cc88eca5 65 has references => ( isa => HashRef[ArrayRef[Str]],
66 is => 'ro',
67 lazy_build => 1 );
68
01ced85b 69 method BUILD {
38b9e5c8 70 $self->$_() for qw/last_change owner description/; # Ensure to build early.
01ced85b 71 }
72
bba40bd5 73 ## Public methods
32a371cc 74 method head_hash (Str $head?) {
75 my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' );
76 confess("No such head: " . $head) unless defined $output;
29debefd 77
32a371cc 78 my($sha1) = $output =~ /^($SHA1RE)$/;
79 return $sha1;
56b6dbe6 80 }
81
a8a8f8f9 82 method list_tree (Str $sha1?) {
83 $sha1 ||= $self->head_hash;
b36b7e0b 84 my $object = $self->get_object($sha1);
85 return @{$object->tree};
a8a8f8f9 86 }
87
8bb7649b 88 method get_object (NonEmptySimpleStr $sha1) {
894bff59 89 unless ( $self->_is_valid_rev($sha1) ) {
8bb7649b 90 $sha1 = $self->head_hash($sha1);
91 }
e1307124 92 my $type = $self->run_cmd('cat-file', '-t', $sha1);
93 chomp($type);
467fa7d9 94 my $class = 'Gitalist::Git::Object::' . ucfirst($type);
95 $class->new(
54368e9d 96 project => $self,
97 sha1 => $sha1,
f3083570 98 type => $type,
54368e9d 99 );
100 }
8bb7649b 101
fc11c7f1 102 method hash_by_path ($base, $path = '', $type?) {
54368e9d 103 $path =~ s{/+$}();
32a371cc 104 # FIXME should this really just take the first result?
105 my @paths = $self->run_cmd('ls-tree', $base, '--', $path)
54368e9d 106 or return;
32a371cc 107 my $line = $paths[0];
54368e9d 108
109 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
110 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
111 return defined $type && $type ne $2
112 ? ()
113 : $3;
114 }
115
4111e151 116 method list_revs ( NonEmptySimpleStr :$sha1!,
117 Int :$count?,
118 Int :$skip?,
119 HashRef :$search?,
05e8b4d0 120 NonEmptySimpleStr :$file? ) {
4111e151 121 $sha1 = $self->head_hash($sha1)
122 if !$sha1 || $sha1 !~ $SHA1RE;
123
124 my @search_opts;
05e8b4d0 125 if ($search) {
4111e151 126 $search->{type} = 'grep'
127 if $search->{type} eq 'commit';
128 @search_opts = (
129 # This seems a little fragile ...
130 qq[--$search->{type}=$search->{text}],
131 '--regexp-ignore-case',
132 $search->{regexp} ? '--extended-regexp' : '--fixed-strings'
133 );
134 }
135
136 my $output = $self->run_cmd(
137 'rev-list',
138 '--header',
139 (defined $count ? "--max-count=$count" : ()),
140 (defined $skip ? "--skip=$skip" : ()),
141 @search_opts,
142 $sha1,
143 '--',
144 ($file ? $file : ()),
145 );
146 return unless $output;
147
cc88eca5 148 my @revs = $self->_parse_rev_list($output);
4111e151 149
150 return @revs;
151 }
152
bba40bd5 153 method snapshot (NonEmptySimpleStr :$sha1,
30db8f5b 154 NonEmptySimpleStr :$format
155 ) {
bba40bd5 156 # TODO - only valid formats are 'tar' and 'zip'
157 my $formats = { tgz => 'tar', zip => 'zip' };
158 unless ($formats->exists($format)) {
159 die("No such format: $format");
160 }
161 $format = $formats->{$format};
162 my $name = $self->name;
163 $name =~ s,([^/])/*\.git$,$1,;
164 my $filename = $name;
165 $filename .= "-$sha1.$format";
166 $name =~ s/\047/\047\\\047\047/g;
167
168 my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1);
169 return ($filename, $self->run_cmd_fh(@cmd));
170 # TODO - support compressed archives
2e79039a 171 }
54368e9d 172
cc88eca5 173 method diff ( Gitalist::Git::Object :$commit!,
77edf882 174 Bool :$patch?,
fc948aee 175 Maybe[NonEmptySimpleStr] :$parent?,
1501cb4e 176 NonEmptySimpleStr :$file?
177 ) {
178 return $commit->diff( patch => $patch,
179 parent => $parent,
180 file => $file);
77edf882 181 }
182
d8abdf1c 183 method reflog (@logargs) {
184 my @entries
185 = $self->run_cmd(qw(log -g), @logargs)
186 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
187
05e8b4d0 188 # commit 02526fc15beddf2c64798a947fecdd8d11bf993d
189 # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
190 # Reflog message: push
191 # Author: Foo Barsby <fbarsby@example.com>
192 # Date: Thu Sep 17 12:26:05 2009 +0100
193 #
194 # Merge branch 'abc123'
d8abdf1c 195
196 return map {
197 # XXX Stuff like this makes me want to switch to Git::PurePerl
198 my($sha1, $type, $author, $date)
199 = m{
200 ^ commit \s+ ($SHA1RE)$
201 .*?
202 Reflog[ ]message: \s+ (.+?)$ \s+
203 Author: \s+ ([^<]+) <.*?$ \s+
204 Date: \s+ (.+?)$
205 }xms;
206
207 pos($_) = index($_, $date) + length $date;
208
209 # Yeah, I just did that.
210 my($msg) = /\G\s+(\S.*)/sg;
211 {
212 hash => $sha1,
213 type => $type,
214 author => $author,
215
216 # XXX Add DateTime goodness.
217 date => $date,
218 message => $msg,
219 }
220 ;
221 } @entries;
222 }
223
cc88eca5 224 ## BUILDERS
50d45f00 225 method _build_util {
32a371cc 226 Gitalist::Git::Util->new(
227 project => $self,
228 );
229 }
230
231 method _build_description {
76c97bf6 232 my $description = "";
5d8568c6 233 eval {
76c97bf6 234 $description = $self->path->file('description')->slurp;
235 chomp $description;
5d8568c6 236 };
76c97bf6 237 return $description;
32a371cc 238 }
239
240 method _build_owner {
241 my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0];
242 $gecos =~ s/,+$//;
243 return length($gecos) ? $gecos : $name;
244 }
245
246 method _build_last_change {
247 my $last_change;
248 my $output = $self->run_cmd(
249 qw{ for-each-ref --format=%(committer)
250 --sort=-committerdate --count=1 refs/heads
251 });
252 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
253 my $dt = DateTime->from_epoch(epoch => $epoch);
254 $dt->set_time_zone($tz);
255 $last_change = $dt;
256 }
257 return $last_change;
258 }
259
cc88eca5 260 method _build_heads {
261 my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
262 my @ret;
263 for my $line (@revlines) {
264 my ($rev, $head, $commiter) = split /\0/, $line, 3;
265 $head =~ s!^refs/heads/!!;
266
267 push @ret, { sha1 => $rev, name => $head };
268
269 #FIXME: That isn't the time I'm looking for..
270 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
271 my $dt = DateTime->from_epoch(epoch => $epoch);
272 $dt->set_time_zone($tz);
273 $ret[-1]->{last_change} = $dt;
274 }
275 }
276
277 return \@ret;
278 }
279
ea19a20c 280 method _build_tags {
281 my @revlines = $self->run_cmd_list('for-each-ref',
282 '--sort=-creatordate',
283 '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)',
284 'refs/tags'
285 );
286 my @ret;
287 for my $line (@revlines) {
288 my($refinfo, $creatorinfo) = split /\0/, $line;
289 my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
290 my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
291 $name =~ s!^refs/tags/!!;
292
293 push @ret, { sha1 => $rev, name => $name };
294
295 #FIXME: That isn't the time I'm looking for..
296 if($epoch and $tz) {
297 my $dt = DateTime->from_epoch(epoch => $epoch);
298 $dt->set_time_zone($tz);
299 $ret[-1]->{last_change} = $dt;
300 }
301 }
302
303 return \@ret;
304 }
305
cc88eca5 306 method _build_references {
307 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
308 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
309 my @reflist = $self->run_cmd_list(qw(show-ref --dereference))
05e8b4d0 310 or return;
cc88eca5 311 my %refs;
05e8b4d0 312 for (@reflist) {
313 push @{$refs{$1}}, $2
314 if m!^($SHA1RE)\srefs/(.*)$!;
315 }
cc88eca5 316
05e8b4d0 317 return \%refs;
cc88eca5 318 }
319
320 ## Private methods
321 method _is_valid_rev (Str $rev) {
322 return ($rev =~ /^($SHA1RE)$/);
323 }
324
325 method _parse_rev_list ($output) {
326 return
327 map $self->get_gpp_object($_),
328 grep $self->_is_valid_rev($_),
329 map split(/\n/, $_, 6), split /\0/, $output;
330 }
331
775e96e0 332} # end class
333
334__END__
335
bba40bd5 336=head1 NAME
337
338Gitalist::Git::Project - Model of a git repository
339
340=head1 SYNOPSIS
341
342 my $gitrepo = dir('/repo/base/Gitalist');
343 my $project = Gitalist::Git::Project->new($gitrepo);
344 $project->name; # 'Gitalist'
345 $project->path; # '/repo/base/Gitalist/.git'
346 $project->description; # 'Unnamed repository.'
347
348=head1 DESCRIPTION
349
350This class models a git repository, referred to in Gitalist
351as a "Project".
352
8ba87261 353
bba40bd5 354=head1 ATTRIBUTES
355
356=head2 name
357
8ba87261 358The name of the Project. If unspecified, this will be derived from the path to the git repository.
bba40bd5 359
bba40bd5 360=head2 path
361
8ba87261 362L<Path::Class:Dir> for the filesystem path to the git repository.
bba40bd5 363
bba40bd5 364=head2 description
365
8ba87261 366The contents of .git/description.
bba40bd5 367
bba40bd5 368=head2 owner
369
8ba87261 370Owner of the files on the filesystem.
bba40bd5 371
bba40bd5 372=head2 last_change
373
8ba87261 374The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used.
bba40bd5 375
bba40bd5 376=head2 is_bare
377
8ba87261 378True if this is a bare git repository.
bba40bd5 379
bba40bd5 380=head2 heads
381
bba40bd5 382=head2 tags
383
8ba87261 384An array of the name and sha1 of all heads/tags in the repository.
bba40bd5 385
386=head2 references
387
388Hashref of ArrayRefs for each reference.
389
8ba87261 390
bba40bd5 391=head1 METHODS
392
393=head2 head_hash ($head?)
394
395Return the sha1 for HEAD, or any specified head.
396
bba40bd5 397=head2 list_tree ($sha1?)
398
399Return an array of contents for a given tree.
400The tree is specified by sha1, and defaults to HEAD.
401Each item is a L<Gitalist::Git::Object>.
402
bba40bd5 403=head2 get_object ($sha1)
404
405Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1.
406
8ba87261 407=head2 hash_by_path ($sha1, $path, $type?)
bba40bd5 408
409Returns the sha1 for a given path, optionally limited by type.
410
8ba87261 411=head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?)
bba40bd5 412
413Returns a list of revs for the given head ($sha1).
414
8ba87261 415=head2 snapshot ($sha1, $format)
bba40bd5 416
417Generate an archived snapshot of the repository.
418$sha1 should be a commit or tree.
419Returns a filehandle to read from.
420
8ba87261 421=head2 diff ($commit, $patch?, $parent?, $file?)
bba40bd5 422
423Generate a diff from a given L<Gitalist::Git::Object>.
424
8ba87261 425=head2 reflog (@lorgargs)
bba40bd5 426
427Return a list of hashes representing each reflog entry.
428
429FIXME Should this return objects?
430
bba40bd5 431
b5b638f7 432=head1 SEE ALSO
433
434L<Gitalist::Git::Util> L<Gitalist::Git::Object>
435
8ba87261 436
775e96e0 437=head1 AUTHORS
b5b638f7 438
775e96e0 439See L<Gitalist> for authors.
b5b638f7 440
441=head1 LICENSE
442
775e96e0 443See L<Gitalist> for the license.
b5b638f7 444
445=cut