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/; |
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, |
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 | |
2c130350 |
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 { |
2c130350 |
59 | -d $_[0]->path->parent->subdir($_[0]->name) |
b5b638f7 |
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 | |
bc50b7ec |
79 | method get_object_or_head (NonEmptySimpleStr $ref) { |
80 | my $sha1 = is_SHA1($ref) ? $ref : $self->head_hash($ref); |
dcb1b927 |
81 | $self->get_object($sha1); |
82 | } |
bc50b7ec |
83 | |
32a371cc |
84 | method head_hash (Str $head?) { |
85 | my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' ); |
86 | confess("No such head: " . $head) unless defined $output; |
29debefd |
87 | |
32a371cc |
88 | my($sha1) = $output =~ /^($SHA1RE)$/; |
89 | return $sha1; |
56b6dbe6 |
90 | } |
91 | |
ceef0cf1 |
92 | method list_tree (SHA1 $sha1?) { |
a8a8f8f9 |
93 | $sha1 ||= $self->head_hash; |
b36b7e0b |
94 | my $object = $self->get_object($sha1); |
95 | return @{$object->tree}; |
a8a8f8f9 |
96 | } |
97 | |
8bb7649b |
98 | method get_object (NonEmptySimpleStr $sha1) { |
ceef0cf1 |
99 | unless (is_SHA1($sha1)) { |
8bb7649b |
100 | $sha1 = $self->head_hash($sha1); |
101 | } |
e1307124 |
102 | my $type = $self->run_cmd('cat-file', '-t', $sha1); |
103 | chomp($type); |
467fa7d9 |
104 | my $class = 'Gitalist::Git::Object::' . ucfirst($type); |
105 | $class->new( |
82bc0f05 |
106 | repository => $self, |
54368e9d |
107 | sha1 => $sha1, |
f3083570 |
108 | type => $type, |
54368e9d |
109 | ); |
110 | } |
8bb7649b |
111 | |
fc11c7f1 |
112 | method hash_by_path ($base, $path = '', $type?) { |
54368e9d |
113 | $path =~ s{/+$}(); |
32a371cc |
114 | # FIXME should this really just take the first result? |
115 | my @paths = $self->run_cmd('ls-tree', $base, '--', $path) |
54368e9d |
116 | or return; |
32a371cc |
117 | my $line = $paths[0]; |
54368e9d |
118 | |
119 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' |
120 | $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/; |
121 | return defined $type && $type ne $2 |
122 | ? () |
123 | : $3; |
124 | } |
125 | |
4111e151 |
126 | method list_revs ( NonEmptySimpleStr :$sha1!, |
127 | Int :$count?, |
128 | Int :$skip?, |
129 | HashRef :$search?, |
05e8b4d0 |
130 | NonEmptySimpleStr :$file? ) { |
4111e151 |
131 | $sha1 = $self->head_hash($sha1) |
132 | if !$sha1 || $sha1 !~ $SHA1RE; |
133 | |
c1a9cd73 |
134 | my @search_opts; |
53a9d6de |
135 | if ($search and exists $search->{text}) { |
4111e151 |
136 | $search->{type} = 'grep' |
137 | if $search->{type} eq 'commit'; |
138 | @search_opts = ( |
139 | # This seems a little fragile ... |
140 | qq[--$search->{type}=$search->{text}], |
141 | '--regexp-ignore-case', |
142 | $search->{regexp} ? '--extended-regexp' : '--fixed-strings' |
143 | ); |
144 | } |
145 | |
146 | my $output = $self->run_cmd( |
147 | 'rev-list', |
148 | '--header', |
149 | (defined $count ? "--max-count=$count" : ()), |
150 | (defined $skip ? "--skip=$skip" : ()), |
151 | @search_opts, |
152 | $sha1, |
153 | '--', |
154 | ($file ? $file : ()), |
155 | ); |
156 | return unless $output; |
157 | |
cc88eca5 |
158 | my @revs = $self->_parse_rev_list($output); |
4111e151 |
159 | |
160 | return @revs; |
161 | } |
162 | |
bba40bd5 |
163 | method snapshot (NonEmptySimpleStr :$sha1, |
30db8f5b |
164 | NonEmptySimpleStr :$format |
165 | ) { |
bba40bd5 |
166 | # TODO - only valid formats are 'tar' and 'zip' |
167 | my $formats = { tgz => 'tar', zip => 'zip' }; |
168 | unless ($formats->exists($format)) { |
169 | die("No such format: $format"); |
170 | } |
171 | $format = $formats->{$format}; |
172 | my $name = $self->name; |
173 | $name =~ s,([^/])/*\.git$,$1,; |
174 | my $filename = $name; |
175 | $filename .= "-$sha1.$format"; |
176 | $name =~ s/\047/\047\\\047\047/g; |
177 | |
178 | my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1); |
179 | return ($filename, $self->run_cmd_fh(@cmd)); |
180 | # TODO - support compressed archives |
2e79039a |
181 | } |
54368e9d |
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( |
82bc0f05 |
227 | repository => $self, |
32a371cc |
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 | }; |
3c12ecb7 |
237 | $description = "Unnamed repository, edit the .git/description file to set a description" |
238 | if $description eq "Unnamed repository; edit this file 'description' to name the repository."; |
76c97bf6 |
239 | return $description; |
32a371cc |
240 | } |
241 | |
242 | method _build_owner { |
e66671b8 |
243 | my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0]; |
32a371cc |
244 | $gecos =~ s/,+$//; |
245 | return length($gecos) ? $gecos : $name; |
246 | } |
247 | |
248 | method _build_last_change { |
249 | my $last_change; |
250 | my $output = $self->run_cmd( |
251 | qw{ for-each-ref --format=%(committer) |
252 | --sort=-committerdate --count=1 refs/heads |
253 | }); |
254 | if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) { |
2c130350 |
255 | my $dt = DT->from_epoch(epoch => $epoch); |
32a371cc |
256 | $dt->set_time_zone($tz); |
257 | $last_change = $dt; |
258 | } |
259 | return $last_change; |
260 | } |
261 | |
cc88eca5 |
262 | method _build_heads { |
263 | my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads'); |
264 | my @ret; |
265 | for my $line (@revlines) { |
266 | my ($rev, $head, $commiter) = split /\0/, $line, 3; |
267 | $head =~ s!^refs/heads/!!; |
268 | |
269 | push @ret, { sha1 => $rev, name => $head }; |
270 | |
271 | #FIXME: That isn't the time I'm looking for.. |
272 | if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) { |
2c130350 |
273 | my $dt = DT->from_epoch(epoch => $epoch); |
cc88eca5 |
274 | $dt->set_time_zone($tz); |
275 | $ret[-1]->{last_change} = $dt; |
276 | } |
277 | } |
278 | |
279 | return \@ret; |
280 | } |
281 | |
ea19a20c |
282 | method _build_tags { |
283 | my @revlines = $self->run_cmd_list('for-each-ref', |
284 | '--sort=-creatordate', |
285 | '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)', |
c1a9cd73 |
286 | 'refs/tags' |
ea19a20c |
287 | ); |
288 | my @ret; |
289 | for my $line (@revlines) { |
290 | my($refinfo, $creatorinfo) = split /\0/, $line; |
c1a9cd73 |
291 | my($rev, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6); |
ea19a20c |
292 | my($creator, $epoch, $tz) = ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/); |
293 | $name =~ s!^refs/tags/!!; |
294 | |
295 | push @ret, { sha1 => $rev, name => $name }; |
296 | |
297 | #FIXME: That isn't the time I'm looking for.. |
298 | if($epoch and $tz) { |
2c130350 |
299 | my $dt = DT->from_epoch(epoch => $epoch); |
ea19a20c |
300 | $dt->set_time_zone($tz); |
301 | $ret[-1]->{last_change} = $dt; |
302 | } |
303 | } |
304 | |
305 | return \@ret; |
306 | } |
307 | |
cc88eca5 |
308 | method _build_references { |
c1a9cd73 |
309 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
310 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} |
311 | my @reflist = $self->run_cmd_list(qw(show-ref --dereference)) |
05e8b4d0 |
312 | or return; |
cc88eca5 |
313 | my %refs; |
05e8b4d0 |
314 | for (@reflist) { |
315 | push @{$refs{$1}}, $2 |
316 | if m!^($SHA1RE)\srefs/(.*)$!; |
317 | } |
cc88eca5 |
318 | |
05e8b4d0 |
319 | return \%refs; |
cc88eca5 |
320 | } |
321 | |
322 | ## Private methods |
cc88eca5 |
323 | method _parse_rev_list ($output) { |
324 | return |
325 | map $self->get_gpp_object($_), |
ceef0cf1 |
326 | grep is_SHA1($_), |
cc88eca5 |
327 | map split(/\n/, $_, 6), split /\0/, $output; |
328 | } |
329 | |
775e96e0 |
330 | } # end class |
331 | |
332 | __END__ |
333 | |
bba40bd5 |
334 | =head1 NAME |
335 | |
44a9ed75 |
336 | Gitalist::Git::Repository - Model of a git repository |
bba40bd5 |
337 | |
338 | =head1 SYNOPSIS |
339 | |
340 | my $gitrepo = dir('/repo/base/Gitalist'); |
82bc0f05 |
341 | my $repository = Gitalist::Git::Repository->new($gitrepo); |
342 | $repository->name; # 'Gitalist' |
343 | $repository->path; # '/repo/base/Gitalist/.git' |
344 | $repository->description; # 'Unnamed repository.' |
bba40bd5 |
345 | |
346 | =head1 DESCRIPTION |
347 | |
348 | This class models a git repository, referred to in Gitalist |
87581f05 |
349 | as a "Repository". |
bba40bd5 |
350 | |
8ba87261 |
351 | |
bba40bd5 |
352 | =head1 ATTRIBUTES |
353 | |
354 | =head2 name |
355 | |
87581f05 |
356 | The name of the Repository. If unspecified, this will be derived from the path to the git repository. |
bba40bd5 |
357 | |
bba40bd5 |
358 | =head2 path |
359 | |
8ba87261 |
360 | L<Path::Class:Dir> for the filesystem path to the git repository. |
bba40bd5 |
361 | |
bba40bd5 |
362 | =head2 description |
363 | |
8ba87261 |
364 | The contents of .git/description. |
bba40bd5 |
365 | |
bba40bd5 |
366 | =head2 owner |
367 | |
8ba87261 |
368 | Owner of the files on the filesystem. |
bba40bd5 |
369 | |
bba40bd5 |
370 | =head2 last_change |
371 | |
8ba87261 |
372 | The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used. |
bba40bd5 |
373 | |
bba40bd5 |
374 | =head2 is_bare |
375 | |
8ba87261 |
376 | True if this is a bare git repository. |
bba40bd5 |
377 | |
bba40bd5 |
378 | =head2 heads |
379 | |
bba40bd5 |
380 | =head2 tags |
381 | |
8ba87261 |
382 | An array of the name and sha1 of all heads/tags in the repository. |
bba40bd5 |
383 | |
384 | =head2 references |
385 | |
386 | Hashref of ArrayRefs for each reference. |
387 | |
8ba87261 |
388 | |
bba40bd5 |
389 | =head1 METHODS |
390 | |
391 | =head2 head_hash ($head?) |
392 | |
393 | Return the sha1 for HEAD, or any specified head. |
394 | |
bba40bd5 |
395 | =head2 list_tree ($sha1?) |
396 | |
397 | Return an array of contents for a given tree. |
398 | The tree is specified by sha1, and defaults to HEAD. |
399 | Each item is a L<Gitalist::Git::Object>. |
400 | |
bba40bd5 |
401 | =head2 get_object ($sha1) |
402 | |
403 | Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1. |
404 | |
8ba87261 |
405 | =head2 hash_by_path ($sha1, $path, $type?) |
bba40bd5 |
406 | |
407 | Returns the sha1 for a given path, optionally limited by type. |
408 | |
8ba87261 |
409 | =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?) |
bba40bd5 |
410 | |
411 | Returns a list of revs for the given head ($sha1). |
412 | |
8ba87261 |
413 | =head2 snapshot ($sha1, $format) |
bba40bd5 |
414 | |
415 | Generate an archived snapshot of the repository. |
416 | $sha1 should be a commit or tree. |
417 | Returns a filehandle to read from. |
418 | |
8ba87261 |
419 | =head2 diff ($commit, $patch?, $parent?, $file?) |
bba40bd5 |
420 | |
421 | Generate a diff from a given L<Gitalist::Git::Object>. |
422 | |
8ba87261 |
423 | =head2 reflog (@lorgargs) |
bba40bd5 |
424 | |
425 | Return a list of hashes representing each reflog entry. |
426 | |
427 | FIXME Should this return objects? |
428 | |
bba40bd5 |
429 | |
b5b638f7 |
430 | =head1 SEE ALSO |
431 | |
432 | L<Gitalist::Git::Util> L<Gitalist::Git::Object> |
433 | |
8ba87261 |
434 | |
775e96e0 |
435 | =head1 AUTHORS |
b5b638f7 |
436 | |
775e96e0 |
437 | See L<Gitalist> for authors. |
b5b638f7 |
438 | |
439 | =head1 LICENSE |
440 | |
775e96e0 |
441 | See L<Gitalist> for the license. |
b5b638f7 |
442 | |
443 | =cut |