Commit | Line | Data |
56b6dbe6 |
1 | use MooseX::Declare; |
2 | |
b5b638f7 |
3 | =head1 NAME |
4 | |
894bff59 |
5 | Gitalist::Git::Project - Model of a git repository |
b5b638f7 |
6 | |
7 | =head1 SYNOPSIS |
8 | |
561a3f14 |
9 | my $gitrepo = dir('/repo/base/Gitalist'); |
10 | my $project = Gitalist::Git::Project->new($gitrepo); |
11 | $project->name; # 'Gitalist' |
12 | $project->path; # '/repo/base/Gitalist/.git' |
13 | $project->description; # 'Unnamed repository.' |
b5b638f7 |
14 | |
15 | =head1 DESCRIPTION |
16 | |
17 | This class models a git repository, referred to in Gitalist |
18 | as a "Project". |
19 | |
20 | =cut |
21 | |
38b9e5c8 |
22 | class Gitalist::Git::Project with Gitalist::Git::HasUtils { |
56b6dbe6 |
23 | # FIXME, use Types::Path::Class and coerce |
24 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
84f31a44 |
25 | use MooseX::Types::Path::Class qw/Dir/; |
829d41ed |
26 | use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/; |
77edf882 |
27 | use List::MoreUtils qw/any zip/; |
b5b638f7 |
28 | use DateTime; |
a8a8f8f9 |
29 | use aliased 'Gitalist::Git::Object'; |
56b6dbe6 |
30 | |
32a371cc |
31 | our $SHA1RE = qr/[0-9a-fA-F]{40}/; |
32 | |
33 | around BUILDARGS (ClassName $class: Dir $dir) { |
34 | my $name = $dir->dir_list(-1); |
35 | $dir = $dir->subdir('.git') if (-f $dir->file('.git', 'HEAD')); |
36 | confess("Can't find a git repository at " . $dir) |
37 | unless ( -f $dir->file('HEAD') ); |
38 | return $class->$orig(name => $name, |
39 | path => $dir); |
40 | } |
41 | |
b5b638f7 |
42 | =head1 ATTRIBUTES |
43 | |
44 | =head2 name |
45 | |
46 | =cut |
29debefd |
47 | |
56b6dbe6 |
48 | has name => ( isa => NonEmptySimpleStr, |
01ced85b |
49 | is => 'ro', required => 1 ); |
b5b638f7 |
50 | |
51 | =head2 path |
52 | |
53 | L<Path::Class:Dir> for the location of the git repository. |
54 | |
55 | =cut |
56 | |
84f31a44 |
57 | has path => ( isa => Dir, |
01ced85b |
58 | is => 'ro', required => 1); |
56b6dbe6 |
59 | |
b5b638f7 |
60 | =head2 description |
61 | |
62 | String containing .git/description |
63 | |
64 | =cut |
65 | |
0617cbd0 |
66 | has description => ( isa => Str, |
56b6dbe6 |
67 | is => 'ro', |
68 | lazy_build => 1, |
69 | ); |
b5b638f7 |
70 | |
71 | =head2 owner |
72 | |
73 | Owner of the files on disk. |
74 | |
75 | =cut |
76 | |
56b6dbe6 |
77 | has owner => ( isa => NonEmptySimpleStr, |
78 | is => 'ro', |
79 | lazy_build => 1, |
80 | ); |
b5b638f7 |
81 | |
82 | =head2 last_change |
83 | |
84 | L<DateTime> for the time of the last update. |
85 | undef if the repository has never been used. |
86 | |
87 | =cut |
88 | |
0617cbd0 |
89 | has last_change => ( isa => Maybe['DateTime'], |
56b6dbe6 |
90 | is => 'ro', |
91 | lazy_build => 1, |
92 | ); |
93 | |
b5b638f7 |
94 | =head2 is_bare |
95 | |
96 | Bool indicating whether this Project is bare. |
97 | |
98 | =cut |
99 | |
100 | has is_bare => ( isa => Bool, |
101 | is => 'ro', |
102 | lazy => 1, |
103 | default => sub { |
104 | -d $_[0]->path->parent->subdir->($_[0]->name) |
105 | ? 1 : 0 |
106 | }, |
107 | ); |
84f31a44 |
108 | |
01ced85b |
109 | method BUILD { |
38b9e5c8 |
110 | $self->$_() for qw/last_change owner description/; # Ensure to build early. |
01ced85b |
111 | } |
112 | |
32a371cc |
113 | =head1 METHODS |
8dbe8024 |
114 | |
32a371cc |
115 | =head2 head_hash |
894bff59 |
116 | |
32a371cc |
117 | Return the sha1 for HEAD, or any specified head. |
894bff59 |
118 | |
32a371cc |
119 | =cut |
56b6dbe6 |
120 | |
32a371cc |
121 | method head_hash (Str $head?) { |
122 | my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' ); |
123 | confess("No such head: " . $head) unless defined $output; |
29debefd |
124 | |
32a371cc |
125 | my($sha1) = $output =~ /^($SHA1RE)$/; |
126 | return $sha1; |
56b6dbe6 |
127 | } |
128 | |
32a371cc |
129 | |
894bff59 |
130 | =head2 heads |
131 | |
c65cccc2 |
132 | ArrayRef of hashes containing the name and sha1 of all heads. |
894bff59 |
133 | |
134 | =cut |
135 | |
c65cccc2 |
136 | has heads => ( isa => ArrayRef[HashRef], is => 'ro', lazy_build => 1); |
137 | |
138 | method _build_heads { |
32a371cc |
139 | my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads'); |
8dbe8024 |
140 | my @ret; |
32a371cc |
141 | for my $line (@revlines) { |
8dbe8024 |
142 | my ($rev, $head, $commiter) = split /\0/, $line, 3; |
143 | $head =~ s!^refs/heads/!!; |
144 | |
145 | push @ret, { sha1 => $rev, name => $head }; |
146 | |
147 | #FIXME: That isn't the time I'm looking for.. |
148 | if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) { |
149 | my $dt = DateTime->from_epoch(epoch => $epoch); |
150 | $dt->set_time_zone($tz); |
151 | $ret[-1]->{last_change} = $dt; |
152 | } |
153 | } |
154 | |
c65cccc2 |
155 | return \@ret; |
8dbe8024 |
156 | } |
157 | |
894bff59 |
158 | =head2 references |
159 | |
c65cccc2 |
160 | Hashref of ArrayRefs for each reference. |
894bff59 |
161 | |
162 | =cut |
163 | |
829d41ed |
164 | has references => ( isa => HashRef[ArrayRef[Str]], is => 'ro', lazy_build => 1 ); |
4111e151 |
165 | |
b719ff6a |
166 | method _build_references { |
b719ff6a |
167 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
168 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} |
32a371cc |
169 | my @reflist = $self->run_cmd_list(qw(show-ref --dereference)) |
b719ff6a |
170 | or return; |
32a371cc |
171 | my %refs; |
b719ff6a |
172 | for(@reflist) { |
173 | push @{$refs{$1}}, $2 |
174 | if m!^($SHA1RE)\srefs/(.*)$!; |
175 | } |
176 | |
177 | return \%refs; |
178 | } |
4111e151 |
179 | |
a8a8f8f9 |
180 | =head2 list_tree |
181 | |
182 | Return an array of contents for a given tree. |
183 | The tree is specified by sha1, and defaults to HEAD. |
184 | The keys for each item will be: |
185 | |
186 | mode |
187 | type |
188 | object |
189 | file |
190 | |
191 | =cut |
192 | |
193 | method list_tree (Str $sha1?) { |
194 | $sha1 ||= $self->head_hash; |
195 | |
196 | my $output = $self->run_cmd(qw/ls-tree -z/, $sha1); |
197 | return unless defined $output; |
198 | |
199 | my @ret; |
200 | for my $line (split /\0/, $output) { |
201 | my ($mode, $type, $object, $file) = split /\s+/, $line, 4; |
202 | push @ret, Object->new( mode => oct $mode, |
203 | type => $type, |
204 | sha1 => $object, |
205 | file => $file, |
50394a3e |
206 | project => $self, |
a8a8f8f9 |
207 | ); |
208 | } |
209 | return @ret; |
210 | } |
211 | |
8bb7649b |
212 | method get_object (NonEmptySimpleStr $sha1) { |
894bff59 |
213 | unless ( $self->_is_valid_rev($sha1) ) { |
8bb7649b |
214 | $sha1 = $self->head_hash($sha1); |
215 | } |
77edf882 |
216 | return Object->new( |
54368e9d |
217 | project => $self, |
218 | sha1 => $sha1, |
219 | ); |
220 | } |
8bb7649b |
221 | |
894bff59 |
222 | method _is_valid_rev (Str $rev) { |
223 | return ($rev =~ /^($SHA1RE)$/); |
224 | } |
225 | |
fc11c7f1 |
226 | method hash_by_path ($base, $path = '', $type?) { |
54368e9d |
227 | $path =~ s{/+$}(); |
32a371cc |
228 | # FIXME should this really just take the first result? |
229 | my @paths = $self->run_cmd('ls-tree', $base, '--', $path) |
54368e9d |
230 | or return; |
32a371cc |
231 | my $line = $paths[0]; |
54368e9d |
232 | |
233 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' |
234 | $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/; |
235 | return defined $type && $type ne $2 |
236 | ? () |
237 | : $3; |
238 | } |
239 | |
4111e151 |
240 | method list_revs ( NonEmptySimpleStr :$sha1!, |
241 | Int :$count?, |
242 | Int :$skip?, |
243 | HashRef :$search?, |
244 | NonEmptySimpleStr :$file? |
245 | ) { |
246 | $sha1 = $self->head_hash($sha1) |
247 | if !$sha1 || $sha1 !~ $SHA1RE; |
248 | |
249 | my @search_opts; |
250 | if($search) { |
251 | $search->{type} = 'grep' |
252 | if $search->{type} eq 'commit'; |
253 | @search_opts = ( |
254 | # This seems a little fragile ... |
255 | qq[--$search->{type}=$search->{text}], |
256 | '--regexp-ignore-case', |
257 | $search->{regexp} ? '--extended-regexp' : '--fixed-strings' |
258 | ); |
259 | } |
260 | |
261 | my $output = $self->run_cmd( |
262 | 'rev-list', |
263 | '--header', |
264 | (defined $count ? "--max-count=$count" : ()), |
265 | (defined $skip ? "--skip=$skip" : ()), |
266 | @search_opts, |
267 | $sha1, |
268 | '--', |
269 | ($file ? $file : ()), |
270 | ); |
271 | return unless $output; |
272 | |
273 | my @revs = $self->parse_rev_list($output); |
274 | |
275 | return @revs; |
276 | } |
277 | |
278 | method parse_rev_list ($output) { |
279 | return |
280 | map $self->get_gpp_object($_), |
894bff59 |
281 | grep $self->_is_valid_rev($_), |
4111e151 |
282 | map split(/\n/, $_, 6), split /\0/, $output; |
283 | } |
54368e9d |
284 | |
77edf882 |
285 | # XXX Ideally this would return a wee object instead of ad hoc structures. |
286 | method diff ( Gitalist::Git::Object :$commit, |
287 | Bool :$patch?, |
fc948aee |
288 | Maybe[NonEmptySimpleStr] :$parent?, |
77edf882 |
289 | NonEmptySimpleStr :$file? ) { |
290 | # Use parent if specifed, else take the parent from the commit |
291 | # if there is only one, otherwise it was a merge commit. |
292 | $parent = $parent |
293 | ? $parent |
294 | : $commit->parents <= 1 |
295 | ? $commit->parent_sha1 |
296 | : '-c'; |
297 | my @etc = ( |
298 | ( $file ? ('--', $file) : () ), |
299 | ); |
300 | |
301 | my @out = $self->raw_diff( |
ba65bc52 |
302 | ( $patch ? '--patch-with-raw' : () ), |
66a82e01 |
303 | ( $parent ? $parent : () ), |
304 | $commit->sha1, @etc, |
77edf882 |
305 | ); |
306 | |
307 | # XXX Yes, there is much wrongness having parse_diff_tree be destructive. |
308 | my @difftree = $self->parse_diff_tree(\@out); |
309 | |
310 | return \@difftree |
311 | unless $patch; |
312 | |
313 | # The blank line between the tree and the patch. |
314 | shift @out; |
315 | |
316 | # XXX And no I'm not happy about having diff return tree + patch. |
317 | return \@difftree, [$self->parse_diff(@out)]; |
318 | } |
319 | |
320 | method parse_diff (@diff) { |
321 | my @ret; |
322 | for (@diff) { |
323 | # This regex is a little pathological. |
324 | if(m{^diff --git (a/(.*?)) (b/\2)}) { |
325 | push @ret, { |
326 | head => $_, |
327 | a => $1, |
328 | b => $3, |
329 | file => $2, |
330 | diff => '', |
331 | }; |
332 | next; |
333 | } |
334 | |
335 | if(/^index (\w+)\.\.(\w+) (\d+)$/) { |
336 | @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3); |
337 | next |
338 | } |
339 | |
340 | # XXX Somewhat hacky. Ahem. |
341 | $ret[@ret ? -1 : 0]{diff} .= "$_\n"; |
342 | } |
343 | |
344 | return @ret; |
345 | } |
346 | |
347 | # gitweb uses the following sort of command for diffing merges: |
348 | # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index --cc 316cf158df3f6207afbae7270bcc5ba0 -- |
349 | # and for regular diffs |
350 | # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index 2e3454ca0749641b42f063730b0090e1 316cf158df3f6207afbae7270bcc5ba0 -- |
351 | |
352 | method raw_diff (@args) { |
32a371cc |
353 | return $self->run_cmd_list( |
77edf882 |
354 | qw(diff-tree -r -M --no-commit-id --full-index), |
355 | @args |
356 | ); |
77edf882 |
357 | } |
358 | |
359 | method parse_diff_tree ($diff) { |
360 | my @keys = qw(modesrc modedst sha1src sha1dst status src dst); |
361 | my @ret; |
362 | while (@$diff and $diff->[0] =~ /^:\d+/) { |
363 | my $line = shift @$diff; |
364 | # see. man git-diff-tree for more info |
365 | # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst] |
366 | my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/; |
367 | my %line = zip @keys, @vals; |
368 | # Some convenience keys |
369 | $line{file} = $line{src}; |
370 | $line{sha1} = $line{sha1dst}; |
371 | $line{is_new} = $line{sha1src} =~ /^0+$/ |
372 | if $line{sha1src}; |
373 | @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/ |
374 | if $line{status} =~ /^R/; |
375 | push @ret, \%line; |
376 | } |
377 | |
378 | return @ret; |
379 | } |
54368e9d |
380 | |
d8abdf1c |
381 | method reflog (@logargs) { |
382 | my @entries |
383 | = $self->run_cmd(qw(log -g), @logargs) |
384 | =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg; |
385 | |
b719ff6a |
386 | # commit 02526fc15beddf2c64798a947fecdd8d11bf993d |
387 | # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>) |
388 | # Reflog message: push |
389 | # Author: Foo Barsby <fbarsby@example.com> |
390 | # Date: Thu Sep 17 12:26:05 2009 +0100 |
391 | # |
392 | # Merge branch 'abc123' |
d8abdf1c |
393 | |
394 | return map { |
395 | # XXX Stuff like this makes me want to switch to Git::PurePerl |
396 | my($sha1, $type, $author, $date) |
397 | = m{ |
398 | ^ commit \s+ ($SHA1RE)$ |
399 | .*? |
400 | Reflog[ ]message: \s+ (.+?)$ \s+ |
401 | Author: \s+ ([^<]+) <.*?$ \s+ |
402 | Date: \s+ (.+?)$ |
403 | }xms; |
404 | |
405 | pos($_) = index($_, $date) + length $date; |
406 | |
407 | # Yeah, I just did that. |
408 | my($msg) = /\G\s+(\S.*)/sg; |
409 | { |
410 | hash => $sha1, |
411 | type => $type, |
412 | author => $author, |
413 | |
414 | # XXX Add DateTime goodness. |
415 | date => $date, |
416 | message => $msg, |
417 | } |
418 | ; |
419 | } @entries; |
420 | } |
421 | |
56b6dbe6 |
422 | # Compatibility |
423 | |
caba5c95 |
424 | =head2 info |
56b6dbe6 |
425 | |
426 | Returns a hash containing properties of this project. The keys will |
427 | be: |
428 | |
429 | name |
430 | description (empty if .git/description is empty/unnamed) |
431 | owner |
432 | last_change |
433 | |
434 | =cut |
435 | |
caba5c95 |
436 | method info { |
56b6dbe6 |
437 | return { |
438 | name => $self->name, |
439 | description => $self->description, |
440 | owner => $self->owner, |
441 | last_change => $self->last_change, |
442 | }; |
443 | }; |
29debefd |
444 | |
32a371cc |
445 | method _build__util { |
446 | Gitalist::Git::Util->new( |
447 | project => $self, |
448 | ); |
449 | } |
450 | |
451 | method _build_description { |
452 | my $description = ""; |
453 | eval { |
454 | $description = $self->path->file('description')->slurp; |
455 | chomp $description; |
456 | }; |
457 | return $description; |
458 | } |
459 | |
460 | method _build_owner { |
461 | my ($gecos, $name) = (getpwuid $self->path->stat->uid)[6,0]; |
462 | $gecos =~ s/,+$//; |
463 | return length($gecos) ? $gecos : $name; |
464 | } |
465 | |
466 | method _build_last_change { |
467 | my $last_change; |
468 | my $output = $self->run_cmd( |
469 | qw{ for-each-ref --format=%(committer) |
470 | --sort=-committerdate --count=1 refs/heads |
471 | }); |
472 | if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) { |
473 | my $dt = DateTime->from_epoch(epoch => $epoch); |
474 | $dt->set_time_zone($tz); |
475 | $last_change = $dt; |
476 | } |
477 | return $last_change; |
478 | } |
479 | |
b5b638f7 |
480 | =head1 SEE ALSO |
481 | |
482 | L<Gitalist::Git::Util> L<Gitalist::Git::Object> |
483 | |
484 | =head1 AUTHORS AND COPYRIGHT |
485 | |
486 | Catalyst application: |
487 | (C) 2009 Venda Ltd and Dan Brook <dbrook@venda.com> |
488 | |
489 | Original gitweb.cgi from which this was derived: |
490 | (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> |
491 | (C) 2005, Christian Gierke |
492 | |
493 | =head1 LICENSE |
494 | |
495 | FIXME - Is this going to be GPLv2 as per gitweb? If so this is broken.. |
496 | |
497 | This library is free software. You can redistribute it and/or modify |
498 | it under the same terms as Perl itself. |
499 | |
500 | =cut |
501 | |
56b6dbe6 |
502 | } # end class |