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