Commit | Line | Data |
fbf3eb7e |
1 | package Gitalist::Model::Git; |
2 | |
3 | use Moose; |
4 | use namespace::autoclean; |
5 | |
37997f11 |
6 | extends 'Catalyst::Model'; |
7 | with 'Catalyst::Component::InstancePerContext'; |
86382b95 |
8 | |
fbf3eb7e |
9 | use DateTime; |
10 | use Path::Class; |
1feb3d6b |
11 | use File::Which; |
fbf3eb7e |
12 | use Carp qw/croak/; |
13 | use File::Find::Rule; |
14 | use DateTime::Format::Mail; |
15 | use File::Stat::ModeString; |
ad8884fc |
16 | use List::MoreUtils qw/any zip/; |
1feb3d6b |
17 | use Scalar::Util qw/blessed/; |
18 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce |
fbf3eb7e |
19 | |
a7cc1ede |
20 | use Git::PurePerl; |
21 | |
27e05d7b |
22 | =head1 NAME |
23 | |
24 | Gitalist::Model::Git - the model for git interactions |
25 | |
26 | =head1 DESCRIPTION |
27 | |
28 | [enter your description here] |
29 | |
30 | =head1 METHODS |
31 | |
32 | =cut |
33 | |
b3ad9e63 |
34 | # Should these live in a separate module? Or perhaps extended Regexp::Common? |
35 | our $SHA1RE = qr/[0-9a-fA-F]{40}/; |
36 | |
a7cc1ede |
37 | # These are static and only need to be setup on app start. |
8c032474 |
38 | has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class |
39 | has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); |
a7cc1ede |
40 | # These are dynamic and can be different from one request to the next. |
41 | has project => ( isa => NonEmptySimpleStr, is => 'rw'); |
42 | has gpp => ( isa => 'Git::PurePerl', is => 'rw', lazy_build => 1 ); |
43 | |
37997f11 |
44 | sub build_per_context_instance { |
a7cc1ede |
45 | my ( $self, $c ) = @_; |
5a2f0948 |
46 | |
47 | # If we don't have a project param it probably means we're at / |
48 | return $self |
49 | unless $c->req->param('p'); |
50 | |
a7cc1ede |
51 | $self->project( $c->req->param('p') ); |
52 | |
53 | (my $pd = $self->project_dir( $self->project )) =~ s{/\.git$}(); |
54 | $self->gpp( Git::PurePerl->new(directory => $pd) ); |
55 | |
56 | return $self; |
57 | } |
1feb3d6b |
58 | |
27e05d7b |
59 | =head2 BUILD |
60 | |
61 | =cut |
62 | |
8c032474 |
63 | sub BUILD { |
64 | my ($self) = @_; |
65 | $self->git; # Cause lazy value build. |
1feb3d6b |
66 | $self->repo_dir; |
8c032474 |
67 | } |
68 | |
69 | sub _build_git { |
1feb3d6b |
70 | my $git = File::Which::which('git'); |
d7c9a32f |
71 | |
1feb3d6b |
72 | if (!$git) { |
73 | die <<EOR; |
fbf3eb7e |
74 | Could not find a git executable. |
75 | Please specify the which git executable to use in gitweb.yml |
76 | EOR |
1feb3d6b |
77 | } |
fbf3eb7e |
78 | |
1feb3d6b |
79 | return $git; |
04d1d917 |
80 | } |
1feb3d6b |
81 | |
8c032474 |
82 | sub _build_repo_dir { |
1ef8dc7d |
83 | return Gitalist->config->{repo_dir}; |
84 | } |
85 | |
27e05d7b |
86 | =head2 get_object |
87 | |
88 | A wrapper for the equivalent L<Git::PurePerl> method. |
89 | |
90 | =cut |
91 | |
1ef8dc7d |
92 | sub get_object { |
a7cc1ede |
93 | my($self, $sha1) = @_; |
94 | |
9dc3b9a5 |
95 | # We either want an object or undef, *not* an empty list. |
a7cc1ede |
96 | return $self->gpp->get_object($sha1) || undef; |
8c032474 |
97 | } |
d7c9a32f |
98 | |
27e05d7b |
99 | =head2 is_git_repo |
100 | |
101 | Determine whether a given directory (as a L<Path::Class::Dir> object) is a |
102 | C<git> repo. |
103 | |
104 | =cut |
105 | |
fbf3eb7e |
106 | sub is_git_repo { |
1feb3d6b |
107 | my ($self, $dir) = @_; |
fbf3eb7e |
108 | |
1feb3d6b |
109 | return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD'); |
fbf3eb7e |
110 | } |
111 | |
27e05d7b |
112 | =head2 run_cmd |
113 | |
114 | Call out to the C<git> binary and return a string consisting of the output. |
115 | |
116 | =cut |
117 | |
1ef8dc7d |
118 | sub run_cmd { |
119 | my ($self, @args) = @_; |
120 | |
121 | print STDERR 'RUNNING: ', $self->git, qq[ @args], $/; |
122 | |
123 | open my $fh, '-|', $self->git, @args |
124 | or die "failed to run git command"; |
125 | binmode $fh, ':encoding(UTF-8)'; |
126 | |
127 | my $output = do { local $/ = undef; <$fh> }; |
128 | close $fh; |
129 | |
130 | return $output; |
131 | } |
132 | |
27e05d7b |
133 | =head2 project_dir |
134 | |
135 | The directory under which the given project will reside i.e C<.git/..> |
136 | |
137 | =cut |
138 | |
1ef8dc7d |
139 | sub project_dir { |
140 | my($self, $project) = @_; |
141 | |
142 | my $dir = blessed($project) && $project->isa('Path::Class::Dir') |
143 | ? $project->stringify |
27e05d7b |
144 | : $self->dir_from_project_name($project); |
1ef8dc7d |
145 | |
27e05d7b |
146 | $dir .= '/.git' |
147 | if -f dir($dir)->file('.git/HEAD'); |
1ef8dc7d |
148 | |
149 | return $dir; |
150 | } |
151 | |
27e05d7b |
152 | =head2 run_cmd_in |
153 | |
154 | Run a C<git> command in a given project and return the output as a string. |
155 | |
156 | =cut |
157 | |
1ef8dc7d |
158 | sub run_cmd_in { |
159 | my ($self, $project, @args) = @_; |
160 | |
27e05d7b |
161 | return $self->run_cmd('--git-dir' => $self->project_dir($project), @args); |
1ef8dc7d |
162 | } |
163 | |
27e05d7b |
164 | =head2 command |
165 | |
166 | Run a C<git> command for the project specified in the C<p> parameter and |
167 | return the output as a list of strings corresponding to the lines of output. |
168 | |
169 | =cut |
170 | |
b3ad9e63 |
171 | sub command { |
172 | my($self, @args) = @_; |
173 | |
27e05d7b |
174 | my $output = $self->run_cmd('--git-dir' => $self->project_dir($self->project), @args); |
b3ad9e63 |
175 | |
176 | return $output ? split(/\n/, $output) : (); |
177 | } |
178 | |
27e05d7b |
179 | =head2 project_info |
180 | |
181 | Returns a hash corresponding to a given project's properties. The keys will |
182 | be: |
183 | |
184 | name |
185 | description (empty if .git/description is empty/unnamed) |
186 | owner |
187 | last_change |
188 | |
189 | =cut |
190 | |
fbf3eb7e |
191 | sub project_info { |
1feb3d6b |
192 | my ($self, $project) = @_; |
fbf3eb7e |
193 | |
1feb3d6b |
194 | return { |
195 | name => $project, |
196 | $self->get_project_properties( |
27e05d7b |
197 | $self->dir_from_project_name($project), |
198 | ), |
199 | }; |
fbf3eb7e |
200 | } |
201 | |
27e05d7b |
202 | =head2 get_project_properties |
203 | |
204 | Called by C<project_info> to get a project's properties. |
205 | |
206 | =cut |
207 | |
fbf3eb7e |
208 | sub get_project_properties { |
1feb3d6b |
209 | my ($self, $dir) = @_; |
210 | my %props; |
fbf3eb7e |
211 | |
1feb3d6b |
212 | eval { |
213 | $props{description} = $dir->file('description')->slurp; |
214 | chomp $props{description}; |
fbf3eb7e |
215 | }; |
216 | |
1feb3d6b |
217 | if ($props{description} && $props{description} =~ /^Unnamed repository;/) { |
218 | delete $props{description}; |
219 | } |
fbf3eb7e |
220 | |
1feb3d6b |
221 | ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//; |
fbf3eb7e |
222 | |
1feb3d6b |
223 | my $output = $self->run_cmd_in($dir, qw{ |
224 | for-each-ref --format=%(committer) |
225 | --sort=-committerdate --count=1 refs/heads |
226 | }); |
fbf3eb7e |
227 | |
1feb3d6b |
228 | if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) { |
229 | my $dt = DateTime->from_epoch(epoch => $epoch); |
230 | $dt->set_time_zone($tz); |
231 | $props{last_change} = $dt; |
232 | } |
fbf3eb7e |
233 | |
1feb3d6b |
234 | return %props; |
fbf3eb7e |
235 | } |
236 | |
27e05d7b |
237 | =head2 list_projects |
238 | |
239 | For the C<repo_dir> specified in the config return an array of projects where |
240 | each item will contain the contents of L</project_info>. |
241 | |
242 | =cut |
243 | |
fbf3eb7e |
244 | sub list_projects { |
27e05d7b |
245 | my ($self, $dir) = @_; |
fbf3eb7e |
246 | |
27e05d7b |
247 | my $base = dir($dir || $self->repo_dir); |
fbf3eb7e |
248 | |
1feb3d6b |
249 | my @ret; |
250 | my $dh = $base->open; |
251 | while (my $file = $dh->read) { |
252 | next if $file =~ /^.{1,2}$/; |
253 | |
254 | my $obj = $base->subdir($file); |
255 | next unless -d $obj; |
256 | next unless $self->is_git_repo($obj); |
257 | |
258 | # XXX Leaky abstraction alert! |
259 | my $is_bare = !-d $obj->subdir('.git'); |
d7c9a32f |
260 | |
1feb3d6b |
261 | my $name = (File::Spec->splitdir($obj))[-1]; |
262 | push @ret, { |
27e05d7b |
263 | name => ($name . ( $is_bare ? '' : '/.git' )), |
1feb3d6b |
264 | $self->get_project_properties( |
265 | $is_bare ? $obj : $obj->subdir('.git') |
266 | ), |
267 | }; |
268 | } |
269 | |
270 | return [sort { $a->{name} cmp $b->{name} } @ret]; |
fbf3eb7e |
271 | } |
272 | |
27e05d7b |
273 | =head2 dir_from_project_name |
274 | |
275 | Get the corresponding directory of a given project. |
276 | |
277 | =cut |
278 | |
279 | sub dir_from_project_name { |
1feb3d6b |
280 | my ($self, $project) = @_; |
fbf3eb7e |
281 | |
1feb3d6b |
282 | return dir($self->repo_dir)->subdir($project); |
fbf3eb7e |
283 | } |
284 | |
27e05d7b |
285 | =head2 head_hash |
286 | |
0ee97fec |
287 | Find the hash of a given head (defaults to HEAD) of given (or current) project. |
27e05d7b |
288 | |
289 | =cut |
290 | |
c8870bd3 |
291 | sub head_hash { |
0ee97fec |
292 | my ($self, $head, $project) = @_; |
fbf3eb7e |
293 | |
0ee97fec |
294 | my $output = $self->run_cmd_in($project || $self->project, qw/rev-parse --verify/, $head || 'HEAD' ); |
1feb3d6b |
295 | return unless defined $output; |
fbf3eb7e |
296 | |
a7cc1ede |
297 | my($sha1) = $output =~ /^($SHA1RE)$/; |
298 | return $sha1; |
fbf3eb7e |
299 | } |
300 | |
27e05d7b |
301 | =head2 list_tree |
302 | |
303 | For a given tree sha1 return an array describing the tree's contents. Where |
304 | the keys for each item will be: |
305 | |
306 | mode |
307 | type |
308 | object |
309 | file |
310 | |
311 | =cut |
312 | |
fbf3eb7e |
313 | sub list_tree { |
27e05d7b |
314 | my ($self, $rev, $project) = @_; |
fbf3eb7e |
315 | |
27e05d7b |
316 | $project ||= $self->project; |
c8870bd3 |
317 | $rev ||= $self->head_hash($project); |
fbf3eb7e |
318 | |
1feb3d6b |
319 | my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev); |
320 | return unless defined $output; |
fbf3eb7e |
321 | |
1feb3d6b |
322 | my @ret; |
323 | for my $line (split /\0/, $output) { |
324 | my ($mode, $type, $object, $file) = split /\s+/, $line, 4; |
d7c9a32f |
325 | |
1feb3d6b |
326 | push @ret, { |
b3fa97cd |
327 | mode => oct $mode, |
0ee97fec |
328 | # XXX I wonder why directories always turn up as 040000 ... |
b3fa97cd |
329 | modestr => $self->get_object_mode_string({mode=>oct $mode}), |
330 | type => $type, |
331 | object => $object, |
332 | file => $file, |
27e05d7b |
333 | }; |
1feb3d6b |
334 | } |
335 | |
336 | return @ret; |
fbf3eb7e |
337 | } |
338 | |
27e05d7b |
339 | =head2 get_object_mode_string |
340 | |
341 | Provide a string equivalent of an octal mode e.g 0644 eq '-rw-r--r--'. |
342 | |
343 | =cut |
344 | |
fbf3eb7e |
345 | sub get_object_mode_string { |
1feb3d6b |
346 | my ($self, $object) = @_; |
fbf3eb7e |
347 | |
1feb3d6b |
348 | return unless $object && $object->{mode}; |
349 | return mode_to_string($object->{mode}); |
fbf3eb7e |
350 | } |
351 | |
27e05d7b |
352 | =head2 get_object_type |
353 | |
354 | =cut |
355 | |
fbf3eb7e |
356 | sub get_object_type { |
27e05d7b |
357 | my ($self, $object, $project) = @_; |
1feb3d6b |
358 | |
27e05d7b |
359 | chomp(my $output = $self->run_cmd_in($project || $self->project, qw/cat-file -t/, $object)); |
1feb3d6b |
360 | return unless $output; |
361 | |
1feb3d6b |
362 | return $output; |
363 | } |
364 | |
27e05d7b |
365 | =head2 cat_file |
366 | |
367 | Return the contents of a given file. |
368 | |
369 | =cut |
370 | |
371 | sub cat_file { |
372 | my ($self, $object, $project) = @_; |
373 | |
89a18cae |
374 | my $type = $self->get_object_type($object, $project); |
27e05d7b |
375 | die "object `$object' is not a file\n" |
376 | if (!defined $type || $type ne 'blob'); |
377 | |
378 | my $output = $self->run_cmd_in($project || $self->project, qw/cat-file -p/, $object); |
379 | return unless $output; |
380 | |
381 | return $output; |
382 | } |
383 | |
384 | =head2 hash_by_path |
385 | |
386 | For a given sha1 and path find the corresponding hash. Useful for find blobs. |
387 | |
388 | =cut |
389 | |
c8870bd3 |
390 | sub hash_by_path { |
1feb3d6b |
391 | my($self, $base, $path, $type) = @_; |
fbf3eb7e |
392 | |
1feb3d6b |
393 | $path =~ s{/+$}(); |
fbf3eb7e |
394 | |
c8870bd3 |
395 | my($line) = $self->command('ls-tree', $base, '--', $path) |
1feb3d6b |
396 | or return; |
397 | |
398 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' |
b3ad9e63 |
399 | $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/; |
1feb3d6b |
400 | return defined $type && $type ne $2 |
401 | ? () |
c8870bd3 |
402 | : $3; |
295c9703 |
403 | } |
404 | |
27e05d7b |
405 | =head2 valid_rev |
fbf3eb7e |
406 | |
27e05d7b |
407 | Check whether a given rev is valid i.e looks like a sha1. |
fbf3eb7e |
408 | |
27e05d7b |
409 | =cut |
fbf3eb7e |
410 | |
411 | sub valid_rev { |
1feb3d6b |
412 | my ($self, $rev) = @_; |
fbf3eb7e |
413 | |
1feb3d6b |
414 | return unless $rev; |
b3ad9e63 |
415 | return ($rev =~ /^($SHA1RE)$/); |
fbf3eb7e |
416 | } |
417 | |
9c0984d1 |
418 | =head2 raw_diff |
27e05d7b |
419 | |
9c0984d1 |
420 | Provides the raw output of a diff. |
27e05d7b |
421 | |
422 | =cut |
423 | |
ad8884fc |
424 | # gitweb uses the following sort of command for diffing merges: |
425 | # /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 -- |
426 | # and for regular diffs |
427 | # /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 -- |
428 | |
9c0984d1 |
429 | sub raw_diff { |
6cf4366a |
430 | my ($self, @args) = @_; |
fbf3eb7e |
431 | |
ad8884fc |
432 | return $self->command( |
433 | qw(diff-tree -r -M --no-commit-id --full-index), |
434 | @args |
435 | ); |
9c0984d1 |
436 | } |
fbf3eb7e |
437 | |
ad8884fc |
438 | =pod |
9c0984d1 |
439 | diff --git a/TODO b/TODO |
440 | index 6a05e77..2071fd0 100644 |
441 | --- a/TODO |
442 | +++ b/TODO |
443 | @@ -2,4 +2,3 @@ |
444 | * An action to find what branches have been merged, either as a list or through a search mechanism. |
445 | * An action to find which branches a given commit is on. |
446 | * Fix any not text/html bits e.g the patch action. |
447 | -* Simplify the creation of links. |
448 | diff --git a/lib/Gitalist/Controller/Root.pm b/lib/Gitalist/Controller/Root.pm |
449 | index 706d024..7fac165 100644 |
450 | --- a/lib/Gitalist/Controller/Root.pm |
451 | +++ b/lib/Gitalist/Controller/Root.pm |
452 | @@ -157,23 +157,6 @@ sub shortlog : Local { |
453 | ); |
454 | } |
455 | |
456 | -=head2 tree |
457 | - |
458 | -The tree of a given commit. |
459 | =cut |
460 | |
461 | =head2 diff |
462 | |
463 | Returns a list of diff chunks corresponding to the files contained in the diff |
464 | and some associated metadata. |
465 | |
466 | =cut |
467 | |
ad8884fc |
468 | # XXX Ideally this would return a wee object instead of ad hoc structures. |
9c0984d1 |
469 | sub diff { |
ad8884fc |
470 | my($self, %args) = @_; |
471 | |
472 | # So either a parent is specifed, or we use the commit's parent if there's |
473 | # only one, otherwise it was a merge commit. |
474 | my $parent = $args{parent} |
475 | ? $args{parent} |
476 | : @{$args{commit}->parents} <= 1 |
477 | ? $args{commit}->parent_sha1 |
478 | : '-c'; |
479 | my @etc = ( |
480 | ( $args{file} ? ('--', $args{file}) : () ), |
481 | ); |
482 | |
483 | my @out = $self->raw_diff( |
484 | ( $args{patch} ? '--patch-with-raw' : () ), |
485 | $parent, $args{commit}->sha1, @etc |
486 | ); |
9c0984d1 |
487 | |
ad8884fc |
488 | # XXX Yes, there is much wrongness having parse_diff_tree be destructive. |
489 | my @difftree = $self->parse_diff_tree(\@out); |
490 | |
491 | return \@difftree |
492 | unless $args{patch}; |
493 | |
494 | # The blank line between the tree and the patch. |
495 | shift @out; |
496 | |
497 | # XXX And no I'm not happy about having diff return tree + patch. |
498 | return \@difftree, [$self->parse_diff(@out)]; |
6cf4366a |
499 | } |
500 | |
501 | sub parse_diff { |
502 | my($self, @diff) = @_; |
9c0984d1 |
503 | |
504 | my @ret; |
9dc3b9a5 |
505 | for (@diff) { |
5a2f0948 |
506 | # This regex is a little pathological. |
507 | if(m{^diff --git (a/(.*?)) (b/\2)}) { |
9c0984d1 |
508 | push @ret, { |
5a2f0948 |
509 | head => $_, |
510 | a => $1, |
511 | b => $3, |
512 | file => $2, |
513 | diff => '', |
9c0984d1 |
514 | }; |
5a2f0948 |
515 | next; |
9c0984d1 |
516 | } |
5a2f0948 |
517 | |
518 | if(/^index (\w+)\.\.(\w+) (\d+)$/) { |
519 | @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3); |
520 | next |
521 | } |
522 | |
523 | # XXX Somewhat hacky. Ahem. |
524 | $ret[@ret ? -1 : 0]{diff} .= "$_\n"; |
9c0984d1 |
525 | } |
526 | |
527 | return @ret; |
fbf3eb7e |
528 | } |
529 | |
ad8884fc |
530 | # $ git diff-tree -r --no-commit-id -M b222ff0a7260cc1777c7e455dfcaf22551a512fc 7e54e579e196c6c545fee1030175f65a111039d4 |
531 | # :100644 100644 6a85d6c6315b55a99071974eb6ce643aeb2799d6 44c03ed6c328fa6de4b1d9b3f19a3de96b250370 M templates/blob.tt2 |
532 | |
533 | =head2 parse_diff_tree |
534 | |
535 | Given a L<Git::PurePerl> commit object return a list of hashes corresponding |
536 | to the C<diff-tree> output. |
537 | |
538 | =cut |
539 | |
540 | sub parse_diff_tree { |
541 | my($self, $diff) = @_; |
542 | |
543 | my @keys = qw(modesrc modedst sha1src sha1dst status src dst); |
544 | my @ret; |
b4b4d0fd |
545 | while(@$diff and $diff->[0] =~ /^:\d+/) { |
546 | my $line = shift @$diff; |
ad8884fc |
547 | # see. man git-diff-tree for more info |
548 | # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst] |
b4b4d0fd |
549 | my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/; |
ad8884fc |
550 | my %line = zip @keys, @vals; |
551 | # Some convenience keys |
552 | $line{file} = $line{src}; |
553 | $line{sha1} = $line{sha1dst}; |
b4b4d0fd |
554 | $line{is_new} = $line{sha1src} =~ /^0+$/ |
555 | if $line{sha1src}; |
556 | @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/ |
557 | if $line{status} =~ /^R/; |
ad8884fc |
558 | push @ret, \%line; |
559 | } |
560 | |
561 | return @ret; |
562 | } |
563 | |
27e05d7b |
564 | =head2 parse_rev_list |
565 | |
566 | Given the output of the C<rev-list> command return a list of hashes. |
567 | |
568 | =cut |
569 | |
47495599 |
570 | sub parse_rev_list { |
571 | my ($self, $output) = @_; |
572 | my @ret; |
573 | |
574 | my @revs = split /\0/, $output; |
1feb3d6b |
575 | |
47495599 |
576 | for my $rev (split /\0/, $output) { |
577 | for my $line (split /\n/, $rev, 6) { |
578 | chomp $line; |
579 | next unless $line; |
580 | |
581 | if ($self->valid_rev($line)) { |
582 | push @ret, $self->get_object($line); |
583 | } |
584 | } |
1feb3d6b |
585 | } |
47495599 |
586 | |
587 | return @ret; |
fbf3eb7e |
588 | } |
589 | |
27e05d7b |
590 | =head2 list_revs |
591 | |
592 | Calls the C<rev-list> command (a low-level from of C<log>) and returns an |
593 | array of hashes. |
594 | |
595 | =cut |
596 | |
fbf3eb7e |
597 | sub list_revs { |
27e05d7b |
598 | my ($self, %args) = @_; |
fbf3eb7e |
599 | |
0ee97fec |
600 | $args{sha1} ||= $self->head_hash($args{project}); |
fbf3eb7e |
601 | |
27e05d7b |
602 | my $output = $self->run_cmd_in($args{project} || $self->project, 'rev-list', |
1feb3d6b |
603 | '--header', |
604 | (defined $args{ count } ? "--max-count=$args{count}" : ()), |
27e05d7b |
605 | (defined $args{ skip } ? "--skip=$args{skip}" : ()), |
0ee97fec |
606 | $args{sha1}, |
1feb3d6b |
607 | '--', |
27e05d7b |
608 | ($args{file} ? $args{file} : ()), |
609 | ); |
1feb3d6b |
610 | return unless $output; |
fbf3eb7e |
611 | |
1feb3d6b |
612 | my @revs = $self->parse_rev_list($output); |
fbf3eb7e |
613 | |
790ce598 |
614 | return @revs; |
fbf3eb7e |
615 | } |
616 | |
27e05d7b |
617 | =head2 rev_info |
618 | |
619 | Get a single piece of revision information for a given sha1. |
620 | |
621 | =cut |
622 | |
fbf3eb7e |
623 | sub rev_info { |
27e05d7b |
624 | my($self, $rev, $project) = @_; |
fbf3eb7e |
625 | |
1feb3d6b |
626 | return unless $self->valid_rev($rev); |
c5065c66 |
627 | |
27e05d7b |
628 | return $self->list_revs( |
629 | rev => $rev, count => 1, |
630 | ( $project ? (project => $project) : () ) |
631 | ); |
1feb3d6b |
632 | } |
633 | |
27e05d7b |
634 | =head2 reflog |
635 | |
636 | Calls the C<reflog> command and returns a list of hashes. |
637 | |
638 | =cut |
639 | |
1feb3d6b |
640 | sub reflog { |
641 | my ($self, @logargs) = @_; |
642 | |
643 | my @entries |
644 | = $self->run_cmd_in($self->project, qw(log -g), @logargs) |
645 | =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg; |
646 | |
ad8884fc |
647 | =pod |
1feb3d6b |
648 | commit 02526fc15beddf2c64798a947fecdd8d11bf993d |
649 | Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>) |
650 | Reflog message: push |
1ef8dc7d |
651 | Author: Foo Barsby <fbarsby@example.com> |
1feb3d6b |
652 | Date: Thu Sep 17 12:26:05 2009 +0100 |
653 | |
1ef8dc7d |
654 | Merge branch 'abc123' |
1feb3d6b |
655 | =cut |
656 | |
657 | return map { |
658 | |
659 | # XXX Stuff like this makes me want to switch to Git::PurePerl |
660 | my($sha1, $type, $author, $date) |
661 | = m{ |
b3ad9e63 |
662 | ^ commit \s+ ($SHA1RE)$ |
1feb3d6b |
663 | .*? |
664 | Reflog[ ]message: \s+ (.+?)$ \s+ |
665 | Author: \s+ ([^<]+) <.*?$ \s+ |
666 | Date: \s+ (.+?)$ |
27e05d7b |
667 | }xms; |
1feb3d6b |
668 | |
669 | pos($_) = index($_, $date) + length $date; |
670 | |
671 | # Yeah, I just did that. |
672 | |
673 | my($msg) = /\G\s+(\S.*)/sg; |
674 | |
675 | { |
676 | hash => $sha1, |
677 | type => $type, |
678 | author => $author, |
679 | |
680 | # XXX Add DateTime goodness. |
681 | date => $date, |
682 | message => $msg, |
683 | }; |
1ef8dc7d |
684 | } @entries; |
c5065c66 |
685 | } |
686 | |
790ce598 |
687 | =head2 heads |
27e05d7b |
688 | |
689 | Returns an array of hashes representing the heads (aka branches) for the |
690 | given, or current, project. |
691 | |
692 | =cut |
693 | |
790ce598 |
694 | sub heads { |
1feb3d6b |
695 | my ($self, $project) = @_; |
fbf3eb7e |
696 | |
790ce598 |
697 | my @output = $self->command(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads'); |
fbf3eb7e |
698 | |
1feb3d6b |
699 | my @ret; |
790ce598 |
700 | for my $line (@output) { |
1feb3d6b |
701 | my ($rev, $head, $commiter) = split /\0/, $line, 3; |
702 | $head =~ s!^refs/heads/!!; |
fbf3eb7e |
703 | |
790ce598 |
704 | push @ret, { sha1 => $rev, name => $head }; |
fbf3eb7e |
705 | |
1feb3d6b |
706 | #FIXME: That isn't the time I'm looking for.. |
2247133f |
707 | if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) { |
1feb3d6b |
708 | my $dt = DateTime->from_epoch(epoch => $epoch); |
709 | $dt->set_time_zone($tz); |
710 | $ret[-1]->{last_change} = $dt; |
fbf3eb7e |
711 | } |
1feb3d6b |
712 | } |
fbf3eb7e |
713 | |
790ce598 |
714 | return @ret; |
fbf3eb7e |
715 | } |
716 | |
1ef8dc7d |
717 | =head2 refs_for |
718 | |
27e05d7b |
719 | For a given sha1 check which branches currently point at it. |
1ef8dc7d |
720 | |
721 | =cut |
722 | |
723 | sub refs_for { |
724 | my($self, $sha1) = @_; |
725 | |
726 | my $refs = $self->references->{$sha1}; |
727 | |
728 | return $refs ? @$refs : (); |
729 | } |
730 | |
27e05d7b |
731 | =head2 references |
1ef8dc7d |
732 | |
733 | A wrapper for C<git show-ref --dereference>. Based on gitweb's |
734 | C<git_get_references>. |
735 | |
736 | =cut |
737 | |
738 | sub references { |
739 | my($self) = @_; |
740 | |
741 | return $self->{references} |
742 | if $self->{references}; |
743 | |
744 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
745 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} |
b3ad9e63 |
746 | my @reflist = $self->command(qw(show-ref --dereference)) |
1ef8dc7d |
747 | or return; |
748 | |
749 | my %refs; |
b3ad9e63 |
750 | for(@reflist) { |
1ef8dc7d |
751 | push @{$refs{$1}}, $2 |
b3ad9e63 |
752 | if m!^($SHA1RE)\srefs/(.*)$!; |
1ef8dc7d |
753 | } |
754 | |
755 | return $self->{references} = \%refs; |
756 | } |
757 | |
fbf3eb7e |
758 | 1; |
759 | |
760 | __PACKAGE__->meta->make_immutable; |