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