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/; |
84f31a44 |
6 | use MooseX::Types::Path::Class qw/Dir/; |
829d41ed |
7 | use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/; |
ceef0cf1 |
8 | use Gitalist::Git::Types qw/SHA1/; |
dcb1b927 |
9 | use MooseX::MultiMethods; |
2e79039a |
10 | use Moose::Autobox; |
77edf882 |
11 | use List::MoreUtils qw/any zip/; |
b5b638f7 |
12 | use DateTime; |
5effb2c9 |
13 | use Encode qw/decode/; |
14 | use I18N::Langinfo qw/langinfo CODESET/; |
f3083570 |
15 | use Gitalist::Git::Object::Blob; |
467fa7d9 |
16 | use Gitalist::Git::Object::Tree; |
17 | use Gitalist::Git::Object::Commit; |
18 | use Gitalist::Git::Object::Tag; |
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, |
01ced85b |
38 | is => 'ro', required => 1); |
56b6dbe6 |
39 | |
0617cbd0 |
40 | has description => ( isa => Str, |
56b6dbe6 |
41 | is => 'ro', |
42 | lazy_build => 1, |
43 | ); |
b5b638f7 |
44 | |
56b6dbe6 |
45 | has owner => ( isa => NonEmptySimpleStr, |
46 | is => 'ro', |
47 | lazy_build => 1, |
48 | ); |
b5b638f7 |
49 | |
0617cbd0 |
50 | has last_change => ( isa => Maybe['DateTime'], |
56b6dbe6 |
51 | is => 'ro', |
52 | lazy_build => 1, |
53 | ); |
54 | |
b5b638f7 |
55 | has is_bare => ( isa => Bool, |
56 | is => 'ro', |
57 | lazy => 1, |
58 | default => sub { |
59 | -d $_[0]->path->parent->subdir->($_[0]->name) |
60 | ? 1 : 0 |
61 | }, |
62 | ); |
cc88eca5 |
63 | has heads => ( isa => ArrayRef[HashRef], |
64 | is => 'ro', |
65 | lazy_build => 1); |
ea19a20c |
66 | has tags => ( isa => ArrayRef[HashRef], |
67 | is => 'ro', |
68 | lazy_build => 1); |
cc88eca5 |
69 | has references => ( isa => HashRef[ArrayRef[Str]], |
70 | is => 'ro', |
71 | lazy_build => 1 ); |
72 | |
01ced85b |
73 | method BUILD { |
38b9e5c8 |
74 | $self->$_() for qw/last_change owner description/; # Ensure to build early. |
01ced85b |
75 | } |
76 | |
bba40bd5 |
77 | ## Public methods |
dcb1b927 |
78 | |
79 | multi method get_object_or_head (SHA1 $sha1) { |
80 | $self->get_object($sha1); |
81 | } |
82 | multi method get_object_or_head (NonEmptySimpleStr $ref) { |
83 | my $sha1 = $self->head_hash($ref); |
84 | $self->get_object($sha1); |
85 | } |
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; |
05e8b4d0 |
138 | if ($search) { |
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 | |
cc88eca5 |
186 | method diff ( Gitalist::Git::Object :$commit!, |
77edf882 |
187 | Bool :$patch?, |
fc948aee |
188 | Maybe[NonEmptySimpleStr] :$parent?, |
7998de12 |
189 | NonEmptySimpleStr :$filename? |
1501cb4e |
190 | ) { |
191 | return $commit->diff( patch => $patch, |
192 | parent => $parent, |
7998de12 |
193 | filename => $filename); |
77edf882 |
194 | } |
195 | |
d8abdf1c |
196 | method reflog (@logargs) { |
197 | my @entries |
198 | = $self->run_cmd(qw(log -g), @logargs) |
199 | =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg; |
200 | |
05e8b4d0 |
201 | # commit 02526fc15beddf2c64798a947fecdd8d11bf993d |
202 | # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>) |
203 | # Reflog message: push |
204 | # Author: Foo Barsby <fbarsby@example.com> |
205 | # Date: Thu Sep 17 12:26:05 2009 +0100 |
206 | # |
207 | # Merge branch 'abc123' |
d8abdf1c |
208 | |
209 | return map { |
210 | # XXX Stuff like this makes me want to switch to Git::PurePerl |
211 | my($sha1, $type, $author, $date) |
212 | = m{ |
213 | ^ commit \s+ ($SHA1RE)$ |
214 | .*? |
215 | Reflog[ ]message: \s+ (.+?)$ \s+ |
216 | Author: \s+ ([^<]+) <.*?$ \s+ |
217 | Date: \s+ (.+?)$ |
218 | }xms; |
219 | |
220 | pos($_) = index($_, $date) + length $date; |
221 | |
222 | # Yeah, I just did that. |
223 | my($msg) = /\G\s+(\S.*)/sg; |
224 | { |
225 | hash => $sha1, |
226 | type => $type, |
227 | author => $author, |
228 | |
229 | # XXX Add DateTime goodness. |
230 | date => $date, |
231 | message => $msg, |
232 | } |
233 | ; |
234 | } @entries; |
235 | } |
236 | |
cc88eca5 |
237 | ## BUILDERS |
50d45f00 |
238 | method _build_util { |
32a371cc |
239 | Gitalist::Git::Util->new( |
82bc0f05 |
240 | repository => $self, |
32a371cc |
241 | ); |
242 | } |
243 | |
244 | method _build_description { |
76c97bf6 |
245 | my $description = ""; |
5d8568c6 |
246 | eval { |
76c97bf6 |
247 | $description = $self->path->file('description')->slurp; |
248 | chomp $description; |
5d8568c6 |
249 | }; |
76c97bf6 |
250 | return $description; |
32a371cc |
251 | } |
252 | |
253 | method _build_owner { |
5effb2c9 |
254 | my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0]; |
32a371cc |
255 | $gecos =~ s/,+$//; |
256 | return length($gecos) ? $gecos : $name; |
257 | } |
258 | |
259 | method _build_last_change { |
260 | my $last_change; |
261 | my $output = $self->run_cmd( |
262 | qw{ for-each-ref --format=%(committer) |
263 | --sort=-committerdate --count=1 refs/heads |
264 | }); |
265 | if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) { |
266 | my $dt = DateTime->from_epoch(epoch => $epoch); |
267 | $dt->set_time_zone($tz); |
268 | $last_change = $dt; |
269 | } |
270 | return $last_change; |
271 | } |
272 | |
cc88eca5 |
273 | method _build_heads { |
274 | my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads'); |
275 | my @ret; |
276 | for my $line (@revlines) { |
277 | my ($rev, $head, $commiter) = split /\0/, $line, 3; |
278 | $head =~ s!^refs/heads/!!; |
279 | |
280 | push @ret, { sha1 => $rev, name => $head }; |
281 | |
282 | #FIXME: That isn't the time I'm looking for.. |
283 | if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) { |
284 | my $dt = DateTime->from_epoch(epoch => $epoch); |
285 | $dt->set_time_zone($tz); |
286 | $ret[-1]->{last_change} = $dt; |
287 | } |
288 | } |
289 | |
290 | return \@ret; |
291 | } |
292 | |
ea19a20c |
293 | method _build_tags { |
294 | my @revlines = $self->run_cmd_list('for-each-ref', |
295 | '--sort=-creatordate', |
296 | '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)', |
c1a9cd73 |
297 | 'refs/tags' |
ea19a20c |
298 | ); |
299 | my @ret; |
300 | for my $line (@revlines) { |
301 | my($refinfo, $creatorinfo) = split /\0/, $line; |
c1a9cd73 |
302 | my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6); |
ea19a20c |
303 | my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/); |
304 | $name =~ s!^refs/tags/!!; |
305 | |
306 | push @ret, { sha1 => $rev, name => $name }; |
307 | |
308 | #FIXME: That isn't the time I'm looking for.. |
309 | if($epoch and $tz) { |
310 | my $dt = DateTime->from_epoch(epoch => $epoch); |
311 | $dt->set_time_zone($tz); |
312 | $ret[-1]->{last_change} = $dt; |
313 | } |
314 | } |
315 | |
316 | return \@ret; |
317 | } |
318 | |
cc88eca5 |
319 | method _build_references { |
c1a9cd73 |
320 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
321 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} |
322 | my @reflist = $self->run_cmd_list(qw(show-ref --dereference)) |
05e8b4d0 |
323 | or return; |
cc88eca5 |
324 | my %refs; |
05e8b4d0 |
325 | for (@reflist) { |
326 | push @{$refs{$1}}, $2 |
327 | if m!^($SHA1RE)\srefs/(.*)$!; |
328 | } |
cc88eca5 |
329 | |
05e8b4d0 |
330 | return \%refs; |
cc88eca5 |
331 | } |
332 | |
333 | ## Private methods |
cc88eca5 |
334 | method _parse_rev_list ($output) { |
335 | return |
336 | map $self->get_gpp_object($_), |
ceef0cf1 |
337 | grep is_SHA1($_), |
cc88eca5 |
338 | map split(/\n/, $_, 6), split /\0/, $output; |
339 | } |
340 | |
775e96e0 |
341 | } # end class |
342 | |
343 | __END__ |
344 | |
bba40bd5 |
345 | =head1 NAME |
346 | |
44a9ed75 |
347 | Gitalist::Git::Repository - Model of a git repository |
bba40bd5 |
348 | |
349 | =head1 SYNOPSIS |
350 | |
351 | my $gitrepo = dir('/repo/base/Gitalist'); |
82bc0f05 |
352 | my $repository = Gitalist::Git::Repository->new($gitrepo); |
353 | $repository->name; # 'Gitalist' |
354 | $repository->path; # '/repo/base/Gitalist/.git' |
355 | $repository->description; # 'Unnamed repository.' |
bba40bd5 |
356 | |
357 | =head1 DESCRIPTION |
358 | |
359 | This class models a git repository, referred to in Gitalist |
87581f05 |
360 | as a "Repository". |
bba40bd5 |
361 | |
8ba87261 |
362 | |
bba40bd5 |
363 | =head1 ATTRIBUTES |
364 | |
365 | =head2 name |
366 | |
87581f05 |
367 | The name of the Repository. If unspecified, this will be derived from the path to the git repository. |
bba40bd5 |
368 | |
bba40bd5 |
369 | =head2 path |
370 | |
8ba87261 |
371 | L<Path::Class:Dir> for the filesystem path to the git repository. |
bba40bd5 |
372 | |
bba40bd5 |
373 | =head2 description |
374 | |
8ba87261 |
375 | The contents of .git/description. |
bba40bd5 |
376 | |
bba40bd5 |
377 | =head2 owner |
378 | |
8ba87261 |
379 | Owner of the files on the filesystem. |
bba40bd5 |
380 | |
bba40bd5 |
381 | =head2 last_change |
382 | |
8ba87261 |
383 | The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used. |
bba40bd5 |
384 | |
bba40bd5 |
385 | =head2 is_bare |
386 | |
8ba87261 |
387 | True if this is a bare git repository. |
bba40bd5 |
388 | |
bba40bd5 |
389 | =head2 heads |
390 | |
bba40bd5 |
391 | =head2 tags |
392 | |
8ba87261 |
393 | An array of the name and sha1 of all heads/tags in the repository. |
bba40bd5 |
394 | |
395 | =head2 references |
396 | |
397 | Hashref of ArrayRefs for each reference. |
398 | |
8ba87261 |
399 | |
bba40bd5 |
400 | =head1 METHODS |
401 | |
402 | =head2 head_hash ($head?) |
403 | |
404 | Return the sha1 for HEAD, or any specified head. |
405 | |
bba40bd5 |
406 | =head2 list_tree ($sha1?) |
407 | |
408 | Return an array of contents for a given tree. |
409 | The tree is specified by sha1, and defaults to HEAD. |
410 | Each item is a L<Gitalist::Git::Object>. |
411 | |
bba40bd5 |
412 | =head2 get_object ($sha1) |
413 | |
414 | Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1. |
415 | |
8ba87261 |
416 | =head2 hash_by_path ($sha1, $path, $type?) |
bba40bd5 |
417 | |
418 | Returns the sha1 for a given path, optionally limited by type. |
419 | |
8ba87261 |
420 | =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?) |
bba40bd5 |
421 | |
422 | Returns a list of revs for the given head ($sha1). |
423 | |
8ba87261 |
424 | =head2 snapshot ($sha1, $format) |
bba40bd5 |
425 | |
426 | Generate an archived snapshot of the repository. |
427 | $sha1 should be a commit or tree. |
428 | Returns a filehandle to read from. |
429 | |
8ba87261 |
430 | =head2 diff ($commit, $patch?, $parent?, $file?) |
bba40bd5 |
431 | |
432 | Generate a diff from a given L<Gitalist::Git::Object>. |
433 | |
8ba87261 |
434 | =head2 reflog (@lorgargs) |
bba40bd5 |
435 | |
436 | Return a list of hashes representing each reflog entry. |
437 | |
438 | FIXME Should this return objects? |
439 | |
bba40bd5 |
440 | |
b5b638f7 |
441 | =head1 SEE ALSO |
442 | |
443 | L<Gitalist::Git::Util> L<Gitalist::Git::Object> |
444 | |
8ba87261 |
445 | |
775e96e0 |
446 | =head1 AUTHORS |
b5b638f7 |
447 | |
775e96e0 |
448 | See L<Gitalist> for authors. |
b5b638f7 |
449 | |
450 | =head1 LICENSE |
451 | |
775e96e0 |
452 | See L<Gitalist> for the license. |
b5b638f7 |
453 | |
454 | =cut |