1 package Gitalist::Model::Git;
4 use namespace::autoclean;
5 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
8 extends 'Catalyst::Model';
9 with 'Catalyst::Component::InstancePerContext';
11 has repo_dir => ( is => 'ro', required => 1, isa => NonEmptySimpleStr );
15 Gitalist::Model::Git - the model for git interactions
19 [enter your description here]
27 sub build_per_context_instance {
28 my ( $self, $c ) = @_;
30 my $app = blessed($c) || $c;
31 my $model = Git::Repos->new(
32 project => ([$c->req->parameters->{p} || '/']->flatten)[0],
33 repo_dir => $self->repo_dir,
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) );
43 package Git::Repos; # Better name? Split out into own file once we have a sane name.
45 use namespace::autoclean;
51 use DateTime::Format::Mail;
52 use File::Stat::ModeString;
53 use List::MoreUtils qw/any zip/;
54 use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
58 # Should these live in a separate module? Or perhaps extended Regexp::Common?
59 # No, should be a MooseX::Types module!!
60 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
62 # These are static and only need to be setup on app start.
63 has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', required => 1 ); # Fixme - path::class
64 has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
65 # These are dynamic and can be different from one request to the next.
66 has project => ( isa => NonEmptySimpleStr, is => 'rw');
67 has gpp => ( isa => 'Git::PurePerl', is => 'rw', lazy_build => 1 );
77 $self->git; # Cause lazy value build.
82 my $git = File::Which::which('git');
86 Could not find a git executable.
87 Please specify the which git executable to use in gitweb.yml
96 A wrapper for the equivalent L<Git::PurePerl> method.
101 my($self, $sha1) = @_;
103 # We either want an object or undef, *not* an empty list.
104 return $self->gpp->get_object($sha1) || undef;
109 Determine whether a given directory (as a L<Path::Class::Dir> object) is a
115 my ($self, $dir) = @_;
117 return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
122 Call out to the C<git> binary and return a string consisting of the output.
127 my ($self, @args) = @_;
129 print STDERR 'RUNNING: ', $self->git, qq[ @args], $/;
131 open my $fh, '-|', $self->git, @args
132 or die "failed to run git command";
133 binmode $fh, ':encoding(UTF-8)';
135 my $output = do { local $/ = undef; <$fh> };
143 The directory under which the given project will reside i.e C<.git/..>
148 my($self, $project) = @_;
150 my $dir = blessed($project) && $project->isa('Path::Class::Dir')
151 ? $project->stringify
152 : $self->dir_from_project_name($project);
155 if -f dir($dir)->file('.git/HEAD');
162 Run a C<git> command in a given project and return the output as a string.
167 my ($self, $project, @args) = @_;
169 return $self->run_cmd('--git-dir' => $self->project_dir($project), @args);
174 Run a C<git> command for the project specified in the C<p> parameter and
175 return the output as a list of strings corresponding to the lines of output.
180 my($self, @args) = @_;
182 my $output = $self->run_cmd('--git-dir' => $self->project_dir($self->project), @args);
184 return $output ? split(/\n/, $output) : ();
189 Returns a hash corresponding to a given project's properties. The keys will
193 description (empty if .git/description is empty/unnamed)
200 my ($self, $project) = @_;
204 $self->get_project_properties(
205 $self->dir_from_project_name($project),
210 =head2 get_project_properties
212 Called by C<project_info> to get a project's properties.
216 sub get_project_properties {
217 my ($self, $dir) = @_;
221 $props{description} = $dir->file('description')->slurp;
222 chomp $props{description};
225 if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
226 delete $props{description};
229 ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//;
231 my $output = $self->run_cmd_in($dir, qw{
232 for-each-ref --format=%(committer)
233 --sort=-committerdate --count=1 refs/heads
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;
247 For the C<repo_dir> specified in the config return an array of projects where
248 each item will contain the contents of L</project_info>.
253 my ($self, $dir) = @_;
255 my $base = dir($dir || $self->repo_dir);
258 my $dh = $base->open or die("Cannot open dir $base");
259 while (my $file = $dh->read) {
260 next if $file =~ /^.{1,2}$/;
262 my $obj = $base->subdir($file);
264 next unless $self->is_git_repo($obj);
265 # XXX Leaky abstraction alert!
266 my $is_bare = !-d $obj->subdir('.git');
268 my $name = (File::Spec->splitdir($obj))[-1];
270 name => ($name . ( $is_bare ? '' : '/.git' )),
271 $self->get_project_properties(
272 $is_bare ? $obj : $obj->subdir('.git')
277 return [sort { $a->{name} cmp $b->{name} } @ret];
280 =head2 dir_from_project_name
282 Get the corresponding directory of a given project.
286 sub dir_from_project_name {
287 my ($self, $project) = @_;
289 return dir($self->repo_dir)->subdir($project);
294 Find the hash of a given head (defaults to HEAD) of given (or current) project.
299 my ($self, $head, $project) = @_;
301 my $output = $self->run_cmd_in($project || $self->project, qw/rev-parse --verify/, $head || 'HEAD' );
302 return unless defined $output;
304 my($sha1) = $output =~ /^($SHA1RE)$/;
310 For a given tree sha1 return an array describing the tree's contents. Where
311 the keys for each item will be:
321 my ($self, $rev, $project) = @_;
323 $project ||= $self->project;
324 $rev ||= $self->head_hash($project);
326 my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
327 return unless defined $output;
330 for my $line (split /\0/, $output) {
331 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
335 # XXX I wonder why directories always turn up as 040000 ...
336 modestr => $self->get_object_mode_string({mode=>oct $mode}),
346 =head2 get_object_mode_string
348 Provide a string equivalent of an octal mode e.g 0644 eq '-rw-r--r--'.
352 sub get_object_mode_string {
353 my ($self, $object) = @_;
355 return unless $object && $object->{mode};
356 return mode_to_string($object->{mode});
359 =head2 get_object_type
363 sub get_object_type {
364 my ($self, $object, $project) = @_;
366 chomp(my $output = $self->run_cmd_in($project || $self->project, qw/cat-file -t/, $object));
367 return unless $output;
374 Return the contents of a given file.
379 my ($self, $object, $project) = @_;
381 my $type = $self->get_object_type($object, $project);
382 die "object `$object' is not a file\n"
383 if (!defined $type || $type ne 'blob');
385 my $output = $self->run_cmd_in($project || $self->project, qw/cat-file -p/, $object);
386 return unless $output;
393 For a given sha1 and path find the corresponding hash. Useful for find blobs.
398 my($self, $base, $path, $type) = @_;
402 my($line) = $self->command('ls-tree', $base, '--', $path)
405 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
406 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
407 return defined $type && $type ne $2
414 Check whether a given rev is valid i.e looks like a sha1.
419 my ($self, $rev) = @_;
422 return ($rev =~ /^($SHA1RE)$/);
427 Provides the raw output of a diff.
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 --
437 my ($self, @args) = @_;
439 return $self->command(
440 qw(diff-tree -r -M --no-commit-id --full-index),
446 diff --git a/TODO b/TODO
447 index 6a05e77..2071fd0 100644
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.
455 diff --git a/lib/Gitalist/Controller/Root.pm b/lib/Gitalist/Controller/Root.pm
456 index 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 {
465 -The tree of a given commit.
470 Returns a list of diff chunks corresponding to the files contained in the diff
471 and some associated metadata.
475 # XXX Ideally this would return a wee object instead of ad hoc structures.
477 my($self, %args) = @_;
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}
483 : @{$args{commit}->parents} <= 1
484 ? $args{commit}->parent_sha1
487 ( $args{file} ? ('--', $args{file}) : () ),
490 my @out = $self->raw_diff(
491 ( $args{patch} ? '--patch-with-raw' : () ),
492 $parent, $args{commit}->sha1, @etc
495 # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
496 my @difftree = $self->parse_diff_tree(\@out);
501 # The blank line between the tree and the patch.
504 # XXX And no I'm not happy about having diff return tree + patch.
505 return \@difftree, [$self->parse_diff(@out)];
509 my($self, @diff) = @_;
513 # This regex is a little pathological.
514 if(m{^diff --git (a/(.*?)) (b/\2)}) {
525 if(/^index (\w+)\.\.(\w+) (\d+)$/) {
526 @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
530 # XXX Somewhat hacky. Ahem.
531 $ret[@ret ? -1 : 0]{diff} .= "$_\n";
537 # $ git diff-tree -r --no-commit-id -M b222ff0a7260cc1777c7e455dfcaf22551a512fc 7e54e579e196c6c545fee1030175f65a111039d4
538 # :100644 100644 6a85d6c6315b55a99071974eb6ce643aeb2799d6 44c03ed6c328fa6de4b1d9b3f19a3de96b250370 M templates/blob.tt2
540 =head2 parse_diff_tree
542 Given a L<Git::PurePerl> commit object return a list of hashes corresponding
543 to the C<diff-tree> output.
547 sub parse_diff_tree {
548 my($self, $diff) = @_;
550 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
552 while(@$diff and $diff->[0] =~ /^:\d+/) {
553 my $line = shift @$diff;
554 # see. man git-diff-tree for more info
555 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
556 my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
557 my %line = zip @keys, @vals;
558 # Some convenience keys
559 $line{file} = $line{src};
560 $line{sha1} = $line{sha1dst};
561 $line{is_new} = $line{sha1src} =~ /^0+$/
563 @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
564 if $line{status} =~ /^R/;
571 =head2 parse_rev_list
573 Given the output of the C<rev-list> command return a list of hashes.
578 my ($self, $output) = @_;
581 my @revs = split /\0/, $output;
583 for my $rev (split /\0/, $output) {
584 for my $line (split /\n/, $rev, 6) {
588 if ($self->valid_rev($line)) {
589 push @ret, $self->get_object($line);
599 Calls the C<rev-list> command (a low-level from of C<log>) and returns an
605 my ($self, %args) = @_;
607 $args{sha1} ||= $self->head_hash($args{project});
609 my $output = $self->run_cmd_in($args{project} || $self->project, 'rev-list',
611 (defined $args{ count } ? "--max-count=$args{count}" : ()),
612 (defined $args{ skip } ? "--skip=$args{skip}" : ()),
615 ($args{file} ? $args{file} : ()),
617 return unless $output;
619 my @revs = $self->parse_rev_list($output);
626 Get a single piece of revision information for a given sha1.
631 my($self, $rev, $project) = @_;
633 return unless $self->valid_rev($rev);
635 return $self->list_revs(
636 rev => $rev, count => 1,
637 ( $project ? (project => $project) : () )
643 Calls the C<reflog> command and returns a list of hashes.
648 my ($self, @logargs) = @_;
651 = $self->run_cmd_in($self->project, qw(log -g), @logargs)
652 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
655 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
656 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
658 Author: Foo Barsby <fbarsby@example.com>
659 Date: Thu Sep 17 12:26:05 2009 +0100
661 Merge branch 'abc123'
666 # XXX Stuff like this makes me want to switch to Git::PurePerl
667 my($sha1, $type, $author, $date)
669 ^ commit \s+ ($SHA1RE)$
671 Reflog[ ]message: \s+ (.+?)$ \s+
672 Author: \s+ ([^<]+) <.*?$ \s+
676 pos($_) = index($_, $date) + length $date;
678 # Yeah, I just did that.
680 my($msg) = /\G\s+(\S.*)/sg;
687 # XXX Add DateTime goodness.
696 Returns an array of hashes representing the heads (aka branches) for the
697 given, or current, project.
702 my ($self, $project) = @_;
704 my @output = $self->command(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
707 for my $line (@output) {
708 my ($rev, $head, $commiter) = split /\0/, $line, 3;
709 $head =~ s!^refs/heads/!!;
711 push @ret, { sha1 => $rev, name => $head };
713 #FIXME: That isn't the time I'm looking for..
714 if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
715 my $dt = DateTime->from_epoch(epoch => $epoch);
716 $dt->set_time_zone($tz);
717 $ret[-1]->{last_change} = $dt;
726 For a given sha1 check which branches currently point at it.
731 my($self, $sha1) = @_;
733 my $refs = $self->references->{$sha1};
735 return $refs ? @$refs : ();
740 A wrapper for C<git show-ref --dereference>. Based on gitweb's
741 C<git_get_references>.
748 return $self->{references}
749 if $self->{references};
751 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
752 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
753 my @reflist = $self->command(qw(show-ref --dereference))
758 push @{$refs{$1}}, $2
759 if m!^($SHA1RE)\srefs/(.*)$!;
762 return $self->{references} = \%refs;
767 __PACKAGE__->meta->make_immutable;