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