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