use Moose;
use namespace::autoclean;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+use Moose::Autobox;
-BEGIN { extends 'Catalyst::Model' }
+extends 'Catalyst::Model';
+with 'Catalyst::Component::InstancePerContext';
-use DateTime;
-use Path::Class;
-use File::Which;
-use Carp qw/croak/;
-use File::Find::Rule;
-use DateTime::Format::Mail;
-use File::Stat::ModeString;
-use List::MoreUtils qw/any/;
-use Scalar::Util qw/blessed/;
-use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
+has repo_dir => ( is => 'ro', required => 1, isa => NonEmptySimpleStr );
=head1 NAME
=cut
+use Git::PurePerl;
+use Path::Class qw/dir/;
+sub build_per_context_instance {
+ my ( $self, $c ) = @_;
+
+ my $app = blessed($c) || $c;
+ my $model = Git::Repos->new(
+ project => ([$c->req->parameters->{p} || '/']->flatten)[0],
+ repo_dir => $self->repo_dir,
+ );
+
+ # This is fugly as fuck. Move Git::PurePerl construction into attribute builders..
+ my ($pd, $gd) = $model->project_dir( $model->project )->resolve =~ m{((.+?)(:?/\/\.git)?$)};
+ $gd .= '/.git' if ($gd !~ /\.git$/ and -d "$gd/.git");
+ $model->gpp( Git::PurePerl->new(gitdir => $gd, directory => $pd) );
+
+ return $model;
+}
+
+package Git::Repos; # Better name? Split out into own file once we have a sane name.
+use Moose;
+use namespace::autoclean;
+use DateTime;
+use Path::Class;
+use File::Which;
+use Carp qw/croak/;
+use File::Find::Rule;
+use DateTime::Format::Mail;
+use File::Stat::ModeString;
+use List::MoreUtils qw/any zip/;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
+
+use Git::PurePerl;
+
# Should these live in a separate module? Or perhaps extended Regexp::Common?
+# No, should be a MooseX::Types module!!
our $SHA1RE = qr/[0-9a-fA-F]{40}/;
-has project => ( isa => NonEmptySimpleStr, is => 'rw');
-has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class
+# These are static and only need to be setup on app start.
+has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', required => 1 ); # Fixme - path::class
has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
-
+# These are dynamic and can be different from one request to the next.
+has project => ( isa => NonEmptySimpleStr, is => 'rw');
+has gpp => ( isa => 'Git::PurePerl', is => 'rw', lazy_build => 1 );
+
+
+
=head2 BUILD
=cut
$self->repo_dir;
}
-use Git::PurePerl;
-
-has gpp => (
- #isa => 'Git::PurePerl'
- is => 'ro',
- required => 1,
- lazy => 1,
- default => sub {
- my($self) = @_;
- (my $pd = $self->project_dir( $self->project )) =~ s{/\.git$}();
- return Git::PurePerl->new(
- directory => $pd
- );
- },
-);
-
sub _build_git {
my $git = File::Which::which('git');
return $git;
}
-
-sub _build_repo_dir {
- return Gitalist->config->{repo_dir};
-}
=head2 get_object
=cut
sub get_object {
- $_[0]->gpp->get_object($_[1]);
+ my($self, $sha1) = @_;
+
+ # We either want an object or undef, *not* an empty list.
+ return $self->gpp->get_object($sha1) || undef;
}
=head2 is_git_repo
=cut
sub list_projects {
- my ($self, $dir) = @_;
-
- my $base = dir($dir || $self->repo_dir);
-
- my @ret;
- my $dh = $base->open;
- while (my $file = $dh->read) {
- next if $file =~ /^.{1,2}$/;
-
- my $obj = $base->subdir($file);
- next unless -d $obj;
- next unless $self->is_git_repo($obj);
+ my ($self, $dir) = @_;
- # XXX Leaky abstraction alert!
- my $is_bare = !-d $obj->subdir('.git');
+ my $base = dir($dir || $self->repo_dir);
- my $name = (File::Spec->splitdir($obj))[-1];
- push @ret, {
- name => ($name . ( $is_bare ? '' : '/.git' )),
- $self->get_project_properties(
- $is_bare ? $obj : $obj->subdir('.git')
- ),
- };
+ my @ret;
+ my $dh = $base->open or die("Cannot open dir $base");
+ while (my $file = $dh->read) {
+ next if $file =~ /^.{1,2}$/;
+
+ my $obj = $base->subdir($file);
+ next unless -d $obj;
+ next unless $self->is_git_repo($obj);
+ # XXX Leaky abstraction alert!
+ my $is_bare = !-d $obj->subdir('.git');
+
+ my $name = (File::Spec->splitdir($obj))[-1];
+ push @ret, {
+ name => ($name . ( $is_bare ? '' : '/.git' )),
+ $self->get_project_properties(
+ $is_bare ? $obj : $obj->subdir('.git')
+ ),
+ };
}
return [sort { $a->{name} cmp $b->{name} } @ret];
=head2 head_hash
-Find the C<HEAD> of given (or current) project.
+Find the hash of a given head (defaults to HEAD) of given (or current) project.
=cut
sub head_hash {
- my ($self, $project) = @_;
+ my ($self, $head, $project) = @_;
- my $output = $self->run_cmd_in($project || $self->project, qw/rev-parse --verify HEAD/ );
+ my $output = $self->run_cmd_in($project || $self->project, qw/rev-parse --verify/, $head || 'HEAD' );
return unless defined $output;
- my ($head) = $output =~ /^($SHA1RE)$/;
- return $head;
+ my($sha1) = $output =~ /^($SHA1RE)$/;
+ return $sha1;
}
=head2 list_tree
my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
push @ret, {
- mode => oct $mode,
- type => $type,
- object => $object,
- file => $file,
+ mode => oct $mode,
+ # XXX I wonder why directories always turn up as 040000 ...
+ modestr => $self->get_object_mode_string({mode=>oct $mode}),
+ type => $type,
+ object => $object,
+ file => $file,
};
}
sub cat_file {
my ($self, $object, $project) = @_;
- my $type = $self->get_object_type($object);
+ my $type = $self->get_object_type($object, $project);
die "object `$object' is not a file\n"
if (!defined $type || $type ne 'blob');
return ($rev =~ /^($SHA1RE)$/);
}
-=head2 diff
+=head2 raw_diff
+
+Provides the raw output of a diff.
+
+=cut
+
+# gitweb uses the following sort of command for diffing merges:
+# /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 --
+# and for regular diffs
+# /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 --
+
+sub raw_diff {
+ my ($self, @args) = @_;
+ return $self->command(
+ qw(diff-tree -r -M --no-commit-id --full-index),
+ @args
+ );
+}
+
+=pod
+diff --git a/TODO b/TODO
+index 6a05e77..2071fd0 100644
+--- a/TODO
++++ b/TODO
+@@ -2,4 +2,3 @@
+ * An action to find what branches have been merged, either as a list or through a search mechanism.
+ * An action to find which branches a given commit is on.
+ * Fix any not text/html bits e.g the patch action.
+-* Simplify the creation of links.
+diff --git a/lib/Gitalist/Controller/Root.pm b/lib/Gitalist/Controller/Root.pm
+index 706d024..7fac165 100644
+--- a/lib/Gitalist/Controller/Root.pm
++++ b/lib/Gitalist/Controller/Root.pm
+@@ -157,23 +157,6 @@ sub shortlog : Local {
+ );
+ }
+
+-=head2 tree
+-
+-The tree of a given commit.
+=cut
+
+=head2 diff
+Returns a list of diff chunks corresponding to the files contained in the diff
+and some associated metadata.
=cut
+# XXX Ideally this would return a wee object instead of ad hoc structures.
sub diff {
- my ($self, @revs, $project) = @_;
+ my($self, %args) = @_;
+
+ # So either a parent is specifed, or we use the commit's parent if there's
+ # only one, otherwise it was a merge commit.
+ my $parent = $args{parent}
+ ? $args{parent}
+ : @{$args{commit}->parents} <= 1
+ ? $args{commit}->parent_sha1
+ : '-c';
+ my @etc = (
+ ( $args{file} ? ('--', $args{file}) : () ),
+ );
- croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
- if scalar @revs < 1
- || scalar @revs > 2
- || any { !$self->valid_rev($_) } @revs;
+ my @out = $self->raw_diff(
+ ( $args{patch} ? '--patch-with-raw' : () ),
+ $parent, $args{commit}->sha1, @etc
+ );
- my $output = $self->run_cmd_in($project || $self->project, 'diff', @revs);
- return unless $output;
+ # XXX Yes, there is much wrongness having parse_diff_tree be destructive.
+ my @difftree = $self->parse_diff_tree(\@out);
- return $output;
+ return \@difftree
+ unless $args{patch};
+
+ # The blank line between the tree and the patch.
+ shift @out;
+
+ # XXX And no I'm not happy about having diff return tree + patch.
+ return \@difftree, [$self->parse_diff(@out)];
}
-{
- my $formatter = DateTime::Format::Mail->new;
+sub parse_diff {
+ my($self, @diff) = @_;
+
+ my @ret;
+ for (@diff) {
+ # This regex is a little pathological.
+ if(m{^diff --git (a/(.*?)) (b/\2)}) {
+ push @ret, {
+ head => $_,
+ a => $1,
+ b => $3,
+ file => $2,
+ diff => '',
+ };
+ next;
+ }
+
+ if(/^index (\w+)\.\.(\w+) (\d+)$/) {
+ @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3);
+ next
+ }
+
+ # XXX Somewhat hacky. Ahem.
+ $ret[@ret ? -1 : 0]{diff} .= "$_\n";
+ }
+
+ return @ret;
+}
+
+# $ git diff-tree -r --no-commit-id -M b222ff0a7260cc1777c7e455dfcaf22551a512fc 7e54e579e196c6c545fee1030175f65a111039d4
+# :100644 100644 6a85d6c6315b55a99071974eb6ce643aeb2799d6 44c03ed6c328fa6de4b1d9b3f19a3de96b250370 M templates/blob.tt2
+
+=head2 parse_diff_tree
+
+Given a L<Git::PurePerl> commit object return a list of hashes corresponding
+to the C<diff-tree> output.
+
+=cut
+
+sub parse_diff_tree {
+ my($self, $diff) = @_;
+
+ my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
+ my @ret;
+ while(@$diff and $diff->[0] =~ /^:\d+/) {
+ my $line = shift @$diff;
+ # see. man git-diff-tree for more info
+ # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
+ my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/;
+ my %line = zip @keys, @vals;
+ # Some convenience keys
+ $line{file} = $line{src};
+ $line{sha1} = $line{sha1dst};
+ $line{is_new} = $line{sha1src} =~ /^0+$/
+ if $line{sha1src};
+ @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
+ if $line{status} =~ /^R/;
+ push @ret, \%line;
+ }
+
+ return @ret;
+}
=head2 parse_rev_list
=cut
- sub parse_rev_list {
- my ($self, $output) = @_;
- my @ret;
-
- my @revs = split /\0/, $output;
-
- for my $rev (split /\0/, $output) {
- for my $line (split /\n/, $rev, 6) {
- chomp $line;
- next unless $line;
-
- if ($self->valid_rev($line)) {
- push @ret, {rev => $line};
- next;
- }
-
- if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
- $ret[-1]->{$key} = $value;
- next;
- }
-
- if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
- $ret[-1]->{$key} = $value;
- eval {
- $ret[-1]->{ $key . "_datetime" } = DateTime->from_epoch(epoch => $epoch);
- $ret[-1]->{ $key . "_datetime" }->set_time_zone($tz);
- $ret[-1]->{ $key . "_datetime" }->set_formatter($formatter);
- };
-
- if ($@) {
- $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
- }
-
- if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
- $ret[-1]->{ $key . "_name" } = $name;
- $ret[-1]->{ $key . "_email" } = $email;
- }
- }
-
- $line =~ s/^\n?\s{4}//;
- $ret[-1]->{longmessage} = $line;
- $ret[-1]->{message} = (split /\n/, $line, 2)[0];
- }
- }
+sub parse_rev_list {
+ my ($self, $output) = @_;
- return @ret;
- }
+ return
+ map $self->get_object($_),
+ grep $self->valid_rev($_),
+ map split(/\n/, $_, 6), split /\0/, $output;
}
=head2 list_revs
sub list_revs {
my ($self, %args) = @_;
- $args{rev} ||= $self->head_hash($args{project});
+ $args{sha1} = $self->head_hash($args{sha1})
+ if !$args{sha1} || $args{sha1} !~ $SHA1RE;
+
+ my @search_opts;
+ if($args{search}) {
+ my $sargs = $args{search};
+ $sargs->{type} = 'grep'
+ if $sargs->{type} eq 'commit';
+ @search_opts = (
+ # This seems a little fragile ...
+ qq[--$sargs->{type}=$sargs->{text}],
+ '--regexp-ignore-case',
+ $sargs->{regexp} ? '--extended-regexp' : '--fixed-strings'
+ );
+ }
+ $DB::single=1;
my $output = $self->run_cmd_in($args{project} || $self->project, 'rev-list',
'--header',
- (defined $args{ count } ? "--max-count=$args{count}" : ()),
- (defined $args{ skip } ? "--skip=$args{skip}" : ()),
- $args{rev},
+ (defined $args{ count } ? "--max-count=$args{count}" : ()),
+ (defined $args{ skip } ? "--skip=$args{skip}" : ()),
+ @search_opts,
+ $args{sha1},
'--',
($args{file} ? $args{file} : ()),
);
my @revs = $self->parse_rev_list($output);
- return \@revs;
+ return @revs;
}
=head2 rev_info
= $self->run_cmd_in($self->project, qw(log -g), @logargs)
=~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
-=begin
-
+=pod
commit 02526fc15beddf2c64798a947fecdd8d11bf993d
Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
Reflog message: push
} @entries;
}
-=head2 get_heads
+=head2 heads
Returns an array of hashes representing the heads (aka branches) for the
given, or current, project.
=cut
-sub get_heads {
+sub heads {
my ($self, $project) = @_;
- my $output = $self->run_cmd_in($project || $self->project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
- return unless $output;
+ my @output = $self->command(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
my @ret;
- for my $line (split /\n/, $output) {
+ for my $line (@output) {
my ($rev, $head, $commiter) = split /\0/, $line, 3;
$head =~ s!^refs/heads/!!;
- push @ret, { rev => $rev, name => $head };
+ push @ret, { sha1 => $rev, name => $head };
#FIXME: That isn't the time I'm looking for..
- if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
+ if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) {
my $dt = DateTime->from_epoch(epoch => $epoch);
$dt->set_time_zone($tz);
$ret[-1]->{last_change} = $dt;
}
}
- return \@ret;
+ return @ret;
}
=head2 refs_for
return $self->{references} = \%refs;
}
-=begin
-
-$ git diff-tree -r --no-commit-id -M b222ff0a7260cc1777c7e455dfcaf22551a512fc 7e54e579e196c6c545fee1030175f65a111039d4
-:100644 100644 8976ebc7df65475b3def53a1653533c3f61070d0 852b6e170f1bad1fbd9930d3178dda8fdf1feae7 M TODO
-:100644 100644 75f5e5f9ed10ae82a960fde77ecf138159c37610 7f54f8c3a4ad426f6889b13cfba5f5ad9969e3c6 M lib/Gitalist/Controller/Root.pm
-:100644 100644 2c65caa46b56302502b9e6eef952b6f379c71fee e418acf5f7b5f771b0b2ef8be784e8dcd60a4271 M lib/Gitalist/View/Default.pm
-:000000 100644 0000000000000000000000000000000000000000 642599f9ccfc4dbc7034987ad3233655010ff348 A lib/Gitalist/View/SyntaxHighlight.pm
-:000000 100644 0000000000000000000000000000000000000000 3d2e533c41f01276b6f844bae98297273b38dffc A root/static/css/syntax-dark.css
-:100644 100644 6a85d6c6315b55a99071974eb6ce643aeb2799d6 44c03ed6c328fa6de4b1d9b3f19a3de96b250370 M templates/blob.tt2
-
-=cut
-
-use List::MoreUtils qw(zip);
-# XXX Hrm, getting called twice, not sure why.
-=head2 diff_tree
-
-Given a L<Git::PurePerl> commit object return a list of hashes corresponding
-to the C<diff-tree> output.
-
-=cut
-
-sub diff_tree {
- my($self, $commit) = @_;
-
- my @dtout = $self->command(
- # XXX should really deal with multple parents ...
- qw(diff-tree -r --no-commit-id -M), $commit->parent_sha1, $commit->sha1
- );
-
- my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
- my @difftree = map {
- # see. man git-diff-tree for more info
- # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
- my @vals = /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX])\t([^\t]+)(?:\t([^\n]+))?$/;
- my %line = zip @keys, @vals;
- # Some convenience keys
- $line{file} = $line{src};
- $line{sha1} = $line{sha1dst};
- $line{is_new} = $line{sha1src} =~ /^0+$/;
- \%line;
- } @dtout;
-
- return @difftree;
-}
-
1;
__PACKAGE__->meta->make_immutable;