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