Pass repo dir correctly
[catagits/Gitalist.git] / lib / Gitalist / Model / Git.pm
CommitLineData
fbf3eb7e 1package Gitalist::Model::Git;
2
3use Moose;
4use namespace::autoclean;
7268d0ae 5use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
a3694730 6use Moose::Autobox;
fbf3eb7e 7
37997f11 8extends 'Catalyst::Model';
9with 'Catalyst::Component::InstancePerContext';
86382b95 10
7268d0ae 11has repo_dir => ( is => 'ro', required => 1, isa => NonEmptySimpleStr );
12
27e05d7b 13=head1 NAME
14
15Gitalist::Model::Git - the model for git interactions
16
17=head1 DESCRIPTION
18
19[enter your description here]
20
21=head1 METHODS
22
23=cut
24
a3694730 25use Git::PurePerl;
26
27sub build_per_context_instance {
28 my ( $self, $c ) = @_;
29
06e141d1 30 my $app = blessed($c) || $c;
a3694730 31 my $model = Git::Repos->new(
32 project => ([$c->req->parameters->{p} || '/']->flatten)[0],
7268d0ae 33 repo_dir => $self->repo_dir,
a3694730 34 );
35
36 # This is fugly as fuck. Move Git::PurePerl construction into attribute builders..
37 (my $pd = $self->project_dir( $self->project )) =~ s{/\.git$}();
38 $model->gpp( Git::PurePerl->new(directory => $pd) );
39
40 return $model;
41}
42
43package Git::Repos; # Better name? Split out into own file once we have a sane name.
44use Moose;
45use namespace::autoclean;
46use DateTime;
47use Path::Class;
48use File::Which;
49use Carp qw/croak/;
50use File::Find::Rule;
51use DateTime::Format::Mail;
52use File::Stat::ModeString;
53use List::MoreUtils qw/any zip/;
54use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
55
56use Git::PurePerl;
57
b3ad9e63 58# Should these live in a separate module? Or perhaps extended Regexp::Common?
a3694730 59# No, should be a MooseX::Types module!!
b3ad9e63 60our $SHA1RE = qr/[0-9a-fA-F]{40}/;
61
a7cc1ede 62# These are static and only need to be setup on app start.
0caa4dd2 63has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', required => 1 ); # Fixme - path::class
8c032474 64has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
a7cc1ede 65# These are dynamic and can be different from one request to the next.
66has project => ( isa => NonEmptySimpleStr, is => 'rw');
67has gpp => ( isa => 'Git::PurePerl', is => 'rw', lazy_build => 1 );
68
a7cc1ede 69
a7cc1ede 70
27e05d7b 71=head2 BUILD
72
73=cut
74
8c032474 75sub BUILD {
76 my ($self) = @_;
77 $self->git; # Cause lazy value build.
1feb3d6b 78 $self->repo_dir;
8c032474 79}
80
81sub _build_git {
1feb3d6b 82 my $git = File::Which::which('git');
d7c9a32f 83
1feb3d6b 84 if (!$git) {
85 die <<EOR;
fbf3eb7e 86Could not find a git executable.
87Please specify the which git executable to use in gitweb.yml
88EOR
1feb3d6b 89 }
fbf3eb7e 90
1feb3d6b 91 return $git;
04d1d917 92}
1ef8dc7d 93
27e05d7b 94=head2 get_object
95
96A wrapper for the equivalent L<Git::PurePerl> method.
97
98=cut
99
1ef8dc7d 100sub get_object {
a7cc1ede 101 my($self, $sha1) = @_;
102
9dc3b9a5 103 # We either want an object or undef, *not* an empty list.
a7cc1ede 104 return $self->gpp->get_object($sha1) || undef;
8c032474 105}
d7c9a32f 106
27e05d7b 107=head2 is_git_repo
108
109Determine whether a given directory (as a L<Path::Class::Dir> object) is a
110C<git> repo.
111
112=cut
113
fbf3eb7e 114sub is_git_repo {
1feb3d6b 115 my ($self, $dir) = @_;
fbf3eb7e 116
1feb3d6b 117 return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
fbf3eb7e 118}
119
27e05d7b 120=head2 run_cmd
121
122Call out to the C<git> binary and return a string consisting of the output.
123
124=cut
125
1ef8dc7d 126sub run_cmd {
127 my ($self, @args) = @_;
128
129 print STDERR 'RUNNING: ', $self->git, qq[ @args], $/;
130
131 open my $fh, '-|', $self->git, @args
132 or die "failed to run git command";
133 binmode $fh, ':encoding(UTF-8)';
134
135 my $output = do { local $/ = undef; <$fh> };
136 close $fh;
137
138 return $output;
139}
140
27e05d7b 141=head2 project_dir
142
143The directory under which the given project will reside i.e C<.git/..>
144
145=cut
146
1ef8dc7d 147sub project_dir {
148 my($self, $project) = @_;
149
150 my $dir = blessed($project) && $project->isa('Path::Class::Dir')
151 ? $project->stringify
27e05d7b 152 : $self->dir_from_project_name($project);
1ef8dc7d 153
27e05d7b 154 $dir .= '/.git'
155 if -f dir($dir)->file('.git/HEAD');
1ef8dc7d 156
157 return $dir;
158}
159
27e05d7b 160=head2 run_cmd_in
161
162Run a C<git> command in a given project and return the output as a string.
163
164=cut
165
1ef8dc7d 166sub run_cmd_in {
167 my ($self, $project, @args) = @_;
168
27e05d7b 169 return $self->run_cmd('--git-dir' => $self->project_dir($project), @args);
1ef8dc7d 170}
171
27e05d7b 172=head2 command
173
174Run a C<git> command for the project specified in the C<p> parameter and
175return the output as a list of strings corresponding to the lines of output.
176
177=cut
178
b3ad9e63 179sub command {
180 my($self, @args) = @_;
181
27e05d7b 182 my $output = $self->run_cmd('--git-dir' => $self->project_dir($self->project), @args);
b3ad9e63 183
184 return $output ? split(/\n/, $output) : ();
185}
186
27e05d7b 187=head2 project_info
188
189Returns a hash corresponding to a given project's properties. The keys will
190be:
191
192 name
193 description (empty if .git/description is empty/unnamed)
194 owner
195 last_change
196
197=cut
198
fbf3eb7e 199sub project_info {
1feb3d6b 200 my ($self, $project) = @_;
fbf3eb7e 201
1feb3d6b 202 return {
203 name => $project,
204 $self->get_project_properties(
27e05d7b 205 $self->dir_from_project_name($project),
206 ),
207 };
fbf3eb7e 208}
209
27e05d7b 210=head2 get_project_properties
211
212Called by C<project_info> to get a project's properties.
213
214=cut
215
fbf3eb7e 216sub get_project_properties {
1feb3d6b 217 my ($self, $dir) = @_;
218 my %props;
fbf3eb7e 219
1feb3d6b 220 eval {
221 $props{description} = $dir->file('description')->slurp;
222 chomp $props{description};
fbf3eb7e 223 };
224
1feb3d6b 225 if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
226 delete $props{description};
227 }
fbf3eb7e 228
1feb3d6b 229 ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//;
fbf3eb7e 230
1feb3d6b 231 my $output = $self->run_cmd_in($dir, qw{
232 for-each-ref --format=%(committer)
233 --sort=-committerdate --count=1 refs/heads
234 });
fbf3eb7e 235
1feb3d6b 236 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
237 my $dt = DateTime->from_epoch(epoch => $epoch);
238 $dt->set_time_zone($tz);
239 $props{last_change} = $dt;
240 }
fbf3eb7e 241
1feb3d6b 242 return %props;
fbf3eb7e 243}
244
27e05d7b 245=head2 list_projects
246
247For the C<repo_dir> specified in the config return an array of projects where
248each item will contain the contents of L</project_info>.
249
250=cut
251
fbf3eb7e 252sub list_projects {
29b8fbb9 253 my ($self, $dir) = @_;
fbf3eb7e 254
29b8fbb9 255 my $base = dir($dir || $self->repo_dir);
fbf3eb7e 256
257 my @ret;
6ccff25e 258 my $dh = $base->open or die("Cannot open dir $base");
fbf3eb7e 259 while (my $file = $dh->read) {
260 next if $file =~ /^.{1,2}$/;
261
262 my $obj = $base->subdir($file);
263 next unless -d $obj;
264 next unless $self->is_git_repo($obj);
d5cc37a4 265 # XXX Leaky abstraction alert!
e66db0fb 266 my $is_bare = !-d $obj->subdir('.git');
fbf3eb7e 267
e66db0fb 268 my $name = (File::Spec->splitdir($obj))[-1];
fbf3eb7e 269 push @ret, {
c1087225 270 name => ($name . ( $is_bare ? '' : '/.git' )),
e66db0fb 271 $self->get_project_properties(
272 $is_bare ? $obj : $obj->subdir('.git')
273 ),
fbf3eb7e 274 };
1feb3d6b 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;