use Moose;
use namespace::autoclean;
-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 List::MoreUtils qw/any zip/;
use Scalar::Util qw/blessed/;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
-# abstraction fail - but currently needed for unit tests to work
-use Gitalist;
-
-{
- my $git;
- sub git {
- return $git
- if $git;
-
- if (my $config_git = Gitalist->config->{git}) {
- $git = $config_git if -x $config_git;
- }
- else {
- require File::Which;
- $git = File::Which::which('git');
- }
-
- if (!$git) {
- die <<EOR
+use Git::PurePerl;
+
+=head1 NAME
+
+Gitalist::Model::Git - the model for git interactions
+
+=head1 DESCRIPTION
+
+[enter your description here]
+
+=head1 METHODS
+
+=cut
+
+# Should these live in a separate module? Or perhaps extended Regexp::Common?
+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 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
+
+sub BUILD {
+ my ($self) = @_;
+ $self->git; # Cause lazy value build.
+ $self->repo_dir;
+}
+
+sub _build_git {
+ my $git = File::Which::which('git');
+
+ if (!$git) {
+ die <<EOR;
Could not find a git executable.
Please specify the which git executable to use in gitweb.yml
EOR
- }
+ }
- return $git;
- }
+ return $git;
}
+
+sub _build_repo_dir {
+ return Gitalist->config->{repo_dir};
+}
+
+=head2 get_object
+
+A wrapper for the equivalent L<Git::PurePerl> method.
+
+=cut
+
+sub get_object {
+ 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
+
+Determine whether a given directory (as a L<Path::Class::Dir> object) is a
+C<git> repo.
+
+=cut
sub is_git_repo {
- my ($self, $dir) = @_;
+ my ($self, $dir) = @_;
- return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
+ return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
}
-sub project_info {
- my ($self, $project) = @_;
+=head2 run_cmd
- return {
- name => $project,
- $self->get_project_properties(
- $self->git_dir_from_project_name($project),
- ),
- };
+Call out to the C<git> binary and return a string consisting of the output.
+
+=cut
+
+sub run_cmd {
+ my ($self, @args) = @_;
+
+ print STDERR 'RUNNING: ', $self->git, qq[ @args], $/;
+
+ open my $fh, '-|', $self->git, @args
+ or die "failed to run git command";
+ binmode $fh, ':encoding(UTF-8)';
+
+ my $output = do { local $/ = undef; <$fh> };
+ close $fh;
+
+ return $output;
}
+=head2 project_dir
+
+The directory under which the given project will reside i.e C<.git/..>
+
+=cut
+
+sub project_dir {
+ my($self, $project) = @_;
+
+ my $dir = blessed($project) && $project->isa('Path::Class::Dir')
+ ? $project->stringify
+ : $self->dir_from_project_name($project);
+
+ $dir .= '/.git'
+ if -f dir($dir)->file('.git/HEAD');
+
+ return $dir;
+}
+
+=head2 run_cmd_in
+
+Run a C<git> command in a given project and return the output as a string.
+
+=cut
+
+sub run_cmd_in {
+ my ($self, $project, @args) = @_;
+
+ return $self->run_cmd('--git-dir' => $self->project_dir($project), @args);
+}
+
+=head2 command
+
+Run a C<git> command for the project specified in the C<p> parameter and
+return the output as a list of strings corresponding to the lines of output.
+
+=cut
+
+sub command {
+ my($self, @args) = @_;
+
+ my $output = $self->run_cmd('--git-dir' => $self->project_dir($self->project), @args);
+
+ return $output ? split(/\n/, $output) : ();
+}
+
+=head2 project_info
+
+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
+
+=cut
+
+sub project_info {
+ my ($self, $project) = @_;
+
+ return {
+ name => $project,
+ $self->get_project_properties(
+ $self->dir_from_project_name($project),
+ ),
+ };
+}
+
+=head2 get_project_properties
+
+Called by C<project_info> to get a project's properties.
+
+=cut
+
sub get_project_properties {
- my ($self, $dir) = @_;
- my %props;
+ my ($self, $dir) = @_;
+ my %props;
- eval {
- $props{description} = $dir->file('description')->slurp;
- chomp $props{description};
+ eval {
+ $props{description} = $dir->file('description')->slurp;
+ chomp $props{description};
};
- if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
- delete $props{description};
- }
+ if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
+ delete $props{description};
+ }
- $props{owner} = (getpwuid $dir->stat->uid)[6];
+ ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//;
- my $output = $self->run_cmd_in($dir, qw{
- for-each-ref --format=%(committer)
- --sort=-committerdate --count=1 refs/heads
- });
+ my $output = $self->run_cmd_in($dir, qw{
+ for-each-ref --format=%(committer)
+ --sort=-committerdate --count=1 refs/heads
+ });
- if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
- my $dt = DateTime->from_epoch(epoch => $epoch);
- $dt->set_time_zone($tz);
- $props{last_change} = $dt;
- }
+ if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
+ my $dt = DateTime->from_epoch(epoch => $epoch);
+ $dt->set_time_zone($tz);
+ $props{last_change} = $dt;
+ }
- return %props;
+ return %props;
}
+=head2 list_projects
+
+For the C<repo_dir> specified in the config return an array of projects where
+each item will contain the contents of L</project_info>.
+
+=cut
+
sub list_projects {
- my ($self) = @_;
+ my ($self, $dir) = @_;
- my $base = dir(Gitalist->config->{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 $base = dir($dir || $self->repo_dir);
- return [sort { $a->{name} cmp $b->{name} } @ret];
-}
+ my @ret;
+ my $dh = $base->open;
+ while (my $file = $dh->read) {
+ next if $file =~ /^.{1,2}$/;
-sub run_cmd {
- my ($self, @args) = @_;
+ my $obj = $base->subdir($file);
+ next unless -d $obj;
+ next unless $self->is_git_repo($obj);
- open my $fh, '-|', __PACKAGE__->git, @args
- or die "failed to run git command";
- binmode $fh, ':encoding(UTF-8)';
+ # XXX Leaky abstraction alert!
+ my $is_bare = !-d $obj->subdir('.git');
- my $output = do { local $/ = undef; <$fh> };
- close $fh;
+ 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 $output;
+ return [sort { $a->{name} cmp $b->{name} } @ret];
}
-sub run_cmd_in {
- my ($self, $project, @args) = @_;
+=head2 dir_from_project_name
- my $path;
- if (blessed($project) && $project->isa('Path::Class::Dir')) {
- $path = $project->stringify;
- }
- else {
- $path = $self->git_dir_from_project_name($project);
- }
- return $self->run_cmd('--git-dir' => $path, @args);
-}
+Get the corresponding directory of a given project.
+
+=cut
-sub git_dir_from_project_name {
- my ($self, $project) = @_;
+sub dir_from_project_name {
+ my ($self, $project) = @_;
- return dir(Gitalist->config->{repo_dir})->subdir($project);
+ return dir($self->repo_dir)->subdir($project);
}
-sub get_head_hash {
- my ($self, $project) = @_;
+=head2 head_hash
- my $output = $self->run_cmd_in($project, qw/rev-parse --verify HEAD/ );
- return unless defined $output;
+Find the hash of a given head (defaults to HEAD) of given (or current) project.
- my ($head) = $output =~ /^([0-9a-fA-F]{40})$/;
- return $head;
+=cut
+
+sub head_hash {
+ my ($self, $head) = @_;
+
+ my($output) = $self->command(qw/rev-parse --verify/, $head || 'HEAD' );
+ return unless $output;
+
+ my($sha1) = $output =~ /^($SHA1RE)$/;
+ return $sha1;
}
-sub list_tree {
- my ($self, $project, $rev) = @_;
+=head2 list_tree
- $rev ||= $self->get_head_hash($project);
+For a given tree sha1 return an array describing the tree's contents. Where
+the keys for each item will be:
- my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
- return unless defined $output;
+ mode
+ type
+ object
+ file
- my @ret;
- for my $line (split /\0/, $output) {
- my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
+=cut
- push @ret, {
- mode => oct $mode,
- type => $type,
- object => $object,
- file => $file,
- };
- }
+sub list_tree {
+ my ($self, $sha1) = @_;
+
+ $sha1 = $self->head_hash($sha1)
+ if !$sha1 or $sha1 !~ $SHA1RE;
+
+ my($output) = $self->command(qw/ls-tree -z/, $sha1);
+ return
+ unless $output;
+
+ my @ret;
+ for my $line (split /\0/, $output) {
+ my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
+
+ push @ret, {
+ 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,
+ };
+ }
- return @ret;
+ return @ret;
}
+=head2 get_object_mode_string
+
+Provide a string equivalent of an octal mode e.g 0644 eq '-rw-r--r--'.
+
+=cut
+
sub get_object_mode_string {
- my ($self, $object) = @_;
+ my ($self, $object) = @_;
- return unless $object && $object->{mode};
- return mode_to_string($object->{mode});
+ return unless $object && $object->{mode};
+ return mode_to_string($object->{mode});
}
+=head2 get_object_type
+
+=cut
+
sub get_object_type {
- my ($self, $project, $object) = @_;
+ my ($self, $object) = @_;
- my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
- return unless $output;
+ my($output) = $self->command(qw/cat-file -t/, $object)
+ or return;
- chomp $output;
- return $output;
+ return $output;
}
+=head2 cat_file
+
+Return the contents of a given file.
+
+=cut
+
sub cat_file {
- my ($self, $project, $object) = @_;
+ my ($self, $object) = @_;
- my $type = $self->get_object_type($project, $object);
- die "object `$object' is not a file\n"
- if (!defined $type || $type ne 'blob');
+ my $type = $self->get_object_type($object);
+ die "object `$object' is not a file\n"
+ if (!defined $type || $type ne 'blob');
- my $output = $self->run_cmd_in($project, qw/cat-file -p/, $object);
- return unless $output;
+ my($output) = $self->command(qw/cat-file -p/, $object)
+ or return;
- return $output;
+ return $output;
}
-sub valid_rev {
- my ($self, $rev) = @_;
+=head2 hash_by_path
- return unless $rev;
- return ($rev =~ /^([0-9a-fA-F]{40})$/);
-}
+For a given sha1 and path find the corresponding hash. Useful for find blobs.
-sub diff {
- my ($self, $project, @revs) = @_;
+=cut
- 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;
+sub hash_by_path {
+ my($self, $base, $path, $type) = @_;
- my $output = $self->run_cmd_in($project, 'diff', @revs);
- return unless $output;
+ $path =~ s{/+$}();
- return $output;
+ my($line) = $self->command('ls-tree', $base, '--', $path)
+ or return;
+
+ #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
+ $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
+ return defined $type && $type ne $2
+ ? ()
+ : $3;
}
-{
- my $formatter = DateTime::Format::Mail->new;
+=head2 valid_rev
+
+Check whether a given rev is valid i.e looks like a sha1.
- sub parse_rev_list {
- my ($self, $output) = @_;
- my @ret;
+=cut
- my @revs = split /\0/, $output;
+sub valid_rev {
+ my ($self, $rev) = @_;
- for my $rev (split /\0/, $output) {
- for my $line (split /\n/, $rev, 6) {
- chomp $line;
- next unless $line;
+ return unless $rev;
+ return ($rev =~ /^($SHA1RE)$/);
+}
- if ($self->valid_rev($line)) {
- push @ret, {rev => $line};
- next;
- }
+=head2 raw_diff
- if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
- $ret[-1]->{$key} = $value;
- next;
- }
+Provides the raw output of a diff.
- 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);
- };
+=cut
- if ($@) {
- $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
- }
+# 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 --
- if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
- $ret[-1]->{ $key . "_name" } = $name;
- $ret[-1]->{ $key . "_email" } = $email;
- }
- }
+sub raw_diff {
+ my ($self, @args) = @_;
- $line =~ s/^\n?\s{4}//;
- $ret[-1]->{longmessage} = $line;
- $ret[-1]->{message} = (split /\n/, $line, 2)[0];
- }
- }
+ 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, %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}) : () ),
+ );
+
+ my @out = $self->raw_diff(
+ ( $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};
+
+ # 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)];
+}
- return @ret;
+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;
}
-sub list_revs {
- my ($self, $project, %args) = @_;
+# $ 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;
+}
- $args{rev} ||= $self->get_head_hash($project);
+=head2 parse_rev_list
- my $output = $self->run_cmd_in($project, 'rev-list',
- '--header',
- (defined $args{ count } ? "--max-count=$args{count}" : ()),
- (defined $args{ skip } ? "--skip=$args{skip}" : ()),
- $args{rev},
- '--',
- ($args{file} || ()),
- );
- return unless $output;
+Given the output of the C<rev-list> command return a list of hashes.
+
+=cut
- my @revs = $self->parse_rev_list($output);
+sub parse_rev_list {
+ my ($self, $output) = @_;
- return \@revs;
+ return
+ map $self->get_object($_),
+ grep $self->valid_rev($_),
+ map split(/\n/, $_, 6), split /\0/, $output;
}
+=head2 list_revs
+
+Calls the C<rev-list> command (a low-level from of C<log>) and returns an
+array of hashes.
+
+=cut
+
+sub list_revs {
+ my ($self, %args) = @_;
+
+ $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'
+ );
+ }
+
+ 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}" : ()),
+ @search_opts,
+ $args{sha1},
+ '--',
+ ($args{file} ? $args{file} : ()),
+ );
+ return unless $output;
+
+ my @revs = $self->parse_rev_list($output);
+
+ return @revs;
+}
+
+=head2 rev_info
+
+Get a single piece of revision information for a given sha1.
+
+=cut
+
sub rev_info {
- my ($self, $project, $rev) = @_;
+ my($self, $rev, $project) = @_;
+
+ return unless $self->valid_rev($rev);
+
+ return $self->list_revs(
+ rev => $rev, count => 1,
+ ( $project ? (project => $project) : () )
+ );
+}
+
+=head2 reflog
+
+Calls the C<reflog> command and returns a list of hashes.
+
+=cut
+
+sub reflog {
+ my ($self, @logargs) = @_;
+
+ my @entries
+ = $self->run_cmd_in($self->project, qw(log -g), @logargs)
+ =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
+
+=pod
+ commit 02526fc15beddf2c64798a947fecdd8d11bf993d
+ Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
+ Reflog message: push
+ Author: Foo Barsby <fbarsby@example.com>
+ Date: Thu Sep 17 12:26:05 2009 +0100
+
+ Merge branch 'abc123'
+=cut
+
+ return map {
+
+ # XXX Stuff like this makes me want to switch to Git::PurePerl
+ my($sha1, $type, $author, $date)
+ = m{
+ ^ commit \s+ ($SHA1RE)$
+ .*?
+ Reflog[ ]message: \s+ (.+?)$ \s+
+ Author: \s+ ([^<]+) <.*?$ \s+
+ Date: \s+ (.+?)$
+ }xms;
+
+ pos($_) = index($_, $date) + length $date;
- return unless $self->valid_rev($rev);
+ # Yeah, I just did that.
- return $self->list_revs($project, rev => $rev, count => 1);
+ my($msg) = /\G\s+(\S.*)/sg;
+
+ {
+ hash => $sha1,
+ type => $type,
+ author => $author,
+
+ # XXX Add DateTime goodness.
+ date => $date,
+ message => $msg,
+ };
+ } @entries;
}
-sub get_heads {
- my ($self, $project) = @_;
+=head2 heads
+
+Returns an array of hashes representing the heads (aka branches) for the
+given, or current, project.
+
+=cut
- my $output = $self->run_cmd_in($project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
- return unless $output;
+sub heads {
+ my ($self, $project) = @_;
- my @ret;
- for my $line (split /\n/, $output) {
- my ($rev, $head, $commiter) = split /\0/, $line, 3;
- $head =~ s!^refs/heads/!!;
+ my @output = $self->command(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
- push @ret, { rev => $rev, name => $head };
+ my @ret;
+ for my $line (@output) {
+ my ($rev, $head, $commiter) = split /\0/, $line, 3;
+ $head =~ s!^refs/heads/!!;
- #FIXME: That isn't the time I'm looking for..
- if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
- my $dt = DateTime->from_epoch(epoch => $epoch);
- $dt->set_time_zone($tz);
- $ret[-1]->{last_change} = $dt;
- }
+ push @ret, { sha1 => $rev, name => $head };
+
+ #FIXME: That isn't the time I'm looking for..
+ 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;
+}
+
+=head2 refs_for
+
+For a given sha1 check which branches currently point at it.
- return \@ret;
+=cut
+
+sub refs_for {
+ my($self, $sha1) = @_;
+
+ my $refs = $self->references->{$sha1};
+
+ return $refs ? @$refs : ();
}
-sub archive {
- my ($self, $project, $rev) = @_;
+=head2 references
+
+A wrapper for C<git show-ref --dereference>. Based on gitweb's
+C<git_get_references>.
+
+=cut
+
+sub references {
+ my($self) = @_;
+
+ 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;
+
+ my %refs;
+ for(@reflist) {
+ push @{$refs{$1}}, $2
+ if m!^($SHA1RE)\srefs/(.*)$!;
+ }
- #FIXME: huge memory consuption
- #TODO: compression
- return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
+ return $self->{references} = \%refs;
}
1;