use Moose;
use namespace::autoclean;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
+use Moose::Autobox;
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 zip/;
-use Scalar::Util qw/blessed/;
-use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
-
-use Git::PurePerl;
+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__->meta->make_immutable;
+
+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}/;
# These are static and only need to be setup on app start.
-has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class
+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 );
-sub build_per_context_instance {
- my ( $self, $c ) = @_;
-
- # If we don't have a project param it probably means we're at /
- return $self
- unless $c->req->param('p');
- $self->project( $c->req->param('p') );
- (my $pd = $self->project_dir( $self->project )) =~ s{/\.git$}();
- $self->gpp( Git::PurePerl->new(directory => $pd) );
-
- return $self;
-}
-
=head2 BUILD
=cut
return $git;
}
-
-sub _build_repo_dir {
- return Gitalist->config->{repo_dir};
-}
=head2 get_object
: $self->dir_from_project_name($project);
$dir .= '/.git'
- if -f dir($dir)->file('.git/HEAD');
+ if -f dir($dir)->file('.git/HEAD');
return $dir;
}
Returns a hash corresponding to a given project's properties. The keys will
be:
- name
- description (empty if .git/description is empty/unnamed)
- owner
- last_change
+ name
+ description (empty if .git/description is empty/unnamed)
+ owner
+ last_change
=cut
=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);
-
- # 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')
- ),
- };
+ my ($self, $dir) = @_;
+
+ my $base = dir($dir || $self->repo_dir);
+
+ 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];
=cut
sub head_hash {
- my ($self, $head) = @_;
+ my ($self, $head, $project) = @_;
- my($output) = $self->command(qw/rev-parse --verify/, $head || 'HEAD' );
- return unless $output;
+ my $output = $self->run_cmd_in($project || $self->project, qw/rev-parse --verify/, $head || 'HEAD' );
+ return unless defined $output;
my($sha1) = $output =~ /^($SHA1RE)$/;
return $sha1;
For a given tree sha1 return an array describing the tree's contents. Where
the keys for each item will be:
- mode
- type
- object
- file
+ mode
+ type
+ object
+ file
=cut
sub list_tree {
- my ($self, $sha1) = @_;
+ my ($self, $rev, $project) = @_;
- $sha1 = $self->head_hash($sha1)
- if !$sha1 or $sha1 !~ $SHA1RE;
+ $project ||= $self->project;
+ $rev ||= $self->head_hash($project);
- my($output) = $self->command(qw/ls-tree -z/, $sha1);
- return
- unless $output;
+ my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
+ return unless defined $output;
my @ret;
for my $line (split /\0/, $output) {
push @ret, {
mode => oct $mode,
- # XXX I wonder why directories always turn up as 040000 ...
+ # XXX I wonder why directories always turn up as 040000 ...
modestr => $self->get_object_mode_string({mode=>oct $mode}),
type => $type,
object => $object,
=cut
sub get_object_type {
- my ($self, $object) = @_;
+ my ($self, $object, $project) = @_;
- my($output) = $self->command(qw/cat-file -t/, $object)
- or return;
+ chomp(my $output = $self->run_cmd_in($project || $self->project, qw/cat-file -t/, $object));
+ return unless $output;
return $output;
}
=cut
sub cat_file {
- my ($self, $object) = @_;
+ my ($self, $object, $project) = @_;
- my $type = $self->get_object_type($object);
+ my $type = $self->get_object_type($object, $project || $self->project);
die "object `$object' is not a file\n"
if (!defined $type || $type ne 'blob');
- my($output) = $self->command(qw/cat-file -p/, $object)
- or return;
+ my $output = $self->run_cmd_in($project || $self->project, qw/cat-file -p/, $object);
+ return unless $output;
return $output;
}
my($line) = $self->command('ls-tree', $base, '--', $path)
or return;
- #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
+ #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
$line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
return defined $type && $type ne $2
? ()
my ($self, @args) = @_;
return $self->command(
- qw(diff-tree -r -M --no-commit-id --full-index),
- @args
+ qw(diff-tree -r -M --no-commit-id --full-index),
+ @args
);
}
# only one, otherwise it was a merge commit.
my $parent = $args{parent}
? $args{parent}
- : @{$args{commit}->parents} <= 1
+ : $args{commit}->parents <= 1
? $args{commit}->parent_sha1
: '-c';
my @etc = (
);
my @out = $self->raw_diff(
- ( $args{patch} ? '--patch-with-raw' : () ),
- $parent, $args{commit}->sha1, @etc
+ ( $args{patch} ? '--patch-with-raw' : () ),
+ $parent, $args{commit}->sha1, @etc
);
# XXX Yes, there is much wrongness having parse_diff_tree be destructive.
my @difftree = $self->parse_diff_tree(\@out);
return \@difftree
- unless $args{patch};
+ unless $args{patch};
# The blank line between the tree and the patch.
shift @out;
my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
my @ret;
while(@$diff and $diff->[0] =~ /^:\d+/) {
- my $line = shift @$diff;
+ 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]+))?$/;
$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{sha1src};
+ @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/
if $line{status} =~ /^R/;
push @ret, \%line;
}
return unless $self->valid_rev($rev);
return $self->list_revs(
- rev => $rev, count => 1,
- ( $project ? (project => $project) : () )
+ rev => $rev, count => 1,
+ ( $project ? (project => $project) : () )
);
}
=cut
sub refs_for {
- my($self, $sha1) = @_;
+ my($self, $sha1) = @_;
- my $refs = $self->references->{$sha1};
+ my $refs = $self->references->{$sha1};
- return $refs ? @$refs : ();
+ return $refs ? @$refs : ();
}
=head2 references
=cut
sub references {
- my($self) = @_;
+ my($self) = @_;
- return $self->{references}
- if $self->{references};
+ return $self->{references}
+ if $self->{references};
- # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
- # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
- my @reflist = $self->command(qw(show-ref --dereference))
- or return;
+ # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
+ # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
+ my @reflist = $self->command(qw(show-ref --dereference))
+ or return;
- my %refs;
- for(@reflist) {
- push @{$refs{$1}}, $2
- if m!^($SHA1RE)\srefs/(.*)$!;
- }
+ my %refs;
+ for(@reflist) {
+ push @{$refs{$1}}, $2
+ if m!^($SHA1RE)\srefs/(.*)$!;
+ }
- return $self->{references} = \%refs;
+ return $self->{references} = \%refs;
}
1;