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