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