1 package Gitalist::Model::Git;
4 use namespace::autoclean;
6 BEGIN { extends 'Catalyst::Model' }
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
19 # Should these live in a separate module? Or perhaps extended Regexp::Common?
20 our $SHA1RE = qr/[0-9a-fA-F]{40}/;
22 has project => ( isa => NonEmptySimpleStr, is => 'rw');
23 has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class
24 has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
28 $self->git; # Cause lazy value build.
35 #isa => 'Git::PurePerl'
41 return Git::PurePerl->new(
42 directory => $self->project_dir( $self->project )
48 my $git = File::Which::which('git');
52 Could not find a git executable.
53 Please specify the which git executable to use in gitweb.yml
61 return Gitalist->config->{repo_dir};
65 $_[0]->gpp->get_object($_[1]);
69 my ($self, $dir) = @_;
71 return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
75 my ($self, @args) = @_;
77 print STDERR 'RUNNING: ', $self->git, qq[ @args], $/;
79 open my $fh, '-|', $self->git, @args
80 or die "failed to run git command";
81 binmode $fh, ':encoding(UTF-8)';
83 my $output = do { local $/ = undef; <$fh> };
90 my($self, $project) = @_;
92 my $dir = blessed($project) && $project->isa('Path::Class::Dir')
94 : $self->git_dir_from_project_name($project);
102 my ($self, $project, @args) = @_;
104 return $self->run_cmd('--git-dir' => $self->project_dir($project)."/.git", @args);
108 my($self, @args) = @_;
110 my $output = $self->run_cmd('--git-dir' => $self->project_dir($self->project)."/.git", @args);
112 return $output ? split(/\n/, $output) : ();
116 my ($self, $project) = @_;
120 $self->get_project_properties(
121 $self->git_dir_from_project_name($project),
126 sub get_project_properties {
127 my ($self, $dir) = @_;
131 $props{description} = $dir->file('description')->slurp;
132 chomp $props{description};
135 if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
136 delete $props{description};
139 ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//;
141 my $output = $self->run_cmd_in($dir, qw{
142 for-each-ref --format=%(committer)
143 --sort=-committerdate --count=1 refs/heads
146 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
147 my $dt = DateTime->from_epoch(epoch => $epoch);
148 $dt->set_time_zone($tz);
149 $props{last_change} = $dt;
158 my $base = dir($self->repo_dir);
161 my $dh = $base->open;
162 while (my $file = $dh->read) {
163 next if $file =~ /^.{1,2}$/;
165 my $obj = $base->subdir($file);
167 next unless $self->is_git_repo($obj);
169 # XXX Leaky abstraction alert!
170 my $is_bare = !-d $obj->subdir('.git');
172 my $name = (File::Spec->splitdir($obj))[-1];
174 name => ($name . ( $is_bare ? '.git' : '/.git' )),
175 $self->get_project_properties(
176 $is_bare ? $obj : $obj->subdir('.git')
181 return [sort { $a->{name} cmp $b->{name} } @ret];
184 sub git_dir_from_project_name {
185 my ($self, $project) = @_;
187 return dir($self->repo_dir)->subdir($project);
191 my ($self, $project) = @_;
193 my $output = $self->run_cmd_in($project || $self->project, qw/rev-parse --verify HEAD/ );
194 return unless defined $output;
196 my ($head) = $output =~ /^($SHA1RE)$/;
201 my ($self, $project, $rev) = @_;
203 $rev ||= $self->head_hash($project);
205 my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
206 return unless defined $output;
209 for my $line (split /\0/, $output) {
210 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
223 sub get_object_mode_string {
224 my ($self, $object) = @_;
226 return unless $object && $object->{mode};
227 return mode_to_string($object->{mode});
230 sub get_object_type {
231 my ($self, $project, $object) = @_;
233 my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
234 return unless $output;
241 my($self, $base, $path, $type) = @_;
245 my($line) = $self->command('ls-tree', $base, '--', $path)
248 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
249 $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/;
250 return defined $type && $type ne $2
256 my ($self, $object) = @_;
258 my $type = $self->get_object_type($self->project, $object);
259 die "object `$object' is not a file\n"
260 if (!defined $type || $type ne 'blob');
262 my $output = $self->run_cmd_in($self->project, qw/cat-file -p/, $object);
263 return unless $output;
269 my ($self, $rev) = @_;
272 return ($rev =~ /^($SHA1RE)$/);
276 my ($self, $project, @revs) = @_;
278 croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
281 || any { !$self->valid_rev($_) } @revs;
283 my $output = $self->run_cmd_in($project, 'diff', @revs);
284 return unless $output;
290 my $formatter = DateTime::Format::Mail->new;
293 my ($self, $output) = @_;
296 my @revs = split /\0/, $output;
298 for my $rev (split /\0/, $output) {
299 for my $line (split /\n/, $rev, 6) {
303 if ($self->valid_rev($line)) {
304 push @ret, {rev => $line};
308 if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
309 $ret[-1]->{$key} = $value;
313 if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
314 $ret[-1]->{$key} = $value;
316 $ret[-1]->{ $key . "_datetime" } = DateTime->from_epoch(epoch => $epoch);
317 $ret[-1]->{ $key . "_datetime" }->set_time_zone($tz);
318 $ret[-1]->{ $key . "_datetime" }->set_formatter($formatter);
322 $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
325 if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
326 $ret[-1]->{ $key . "_name" } = $name;
327 $ret[-1]->{ $key . "_email" } = $email;
331 $line =~ s/^\n?\s{4}//;
332 $ret[-1]->{longmessage} = $line;
333 $ret[-1]->{message} = (split /\n/, $line, 2)[0];
342 my ($self, $project, %args) = @_;
344 $args{rev} ||= $self->head_hash($project);
346 my $output = $self->run_cmd_in($project, 'rev-list',
348 (defined $args{ count } ? "--max-count=$args{count}" : ()),
349 (defined $args{ skip } ? "--skip=$args{skip}" : ()),
354 return unless $output;
356 my @revs = $self->parse_rev_list($output);
362 my ($self, $project, $rev) = @_;
364 return unless $self->valid_rev($rev);
366 return $self->list_revs($project, rev => $rev, count => 1);
370 my ($self, @logargs) = @_;
373 = $self->run_cmd_in($self->project, qw(log -g), @logargs)
374 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
378 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
379 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
381 Author: Foo Barsby <fbarsby@example.com>
382 Date: Thu Sep 17 12:26:05 2009 +0100
384 Merge branch 'abc123'
389 # XXX Stuff like this makes me want to switch to Git::PurePerl
390 my($sha1, $type, $author, $date)
392 ^ commit \s+ ($SHA1RE)$
394 Reflog[ ]message: \s+ (.+?)$ \s+
395 Author: \s+ ([^<]+) <.*?$ \s+
399 pos($_) = index($_, $date) + length $date;
401 # Yeah, I just did that.
403 my($msg) = /\G\s+(\S.*)/sg;
410 # XXX Add DateTime goodness.
418 my ($self, $project) = @_;
420 my $output = $self->run_cmd_in($project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
421 return unless $output;
424 for my $line (split /\n/, $output) {
425 my ($rev, $head, $commiter) = split /\0/, $line, 3;
426 $head =~ s!^refs/heads/!!;
428 push @ret, { rev => $rev, name => $head };
430 #FIXME: That isn't the time I'm looking for..
431 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
432 my $dt = DateTime->from_epoch(epoch => $epoch);
433 $dt->set_time_zone($tz);
434 $ret[-1]->{last_change} = $dt;
443 Return a list of refs (e.g branches) for a given sha1.
448 my($self, $sha1) = @_;
450 my $refs = $self->references->{$sha1};
452 return $refs ? @$refs : ();
457 A wrapper for C<git show-ref --dereference>. Based on gitweb's
458 C<git_get_references>.
465 return $self->{references}
466 if $self->{references};
468 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
469 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
470 my @reflist = $self->command(qw(show-ref --dereference))
475 push @{$refs{$1}}, $2
476 if m!^($SHA1RE)\srefs/(.*)$!;
479 return $self->{references} = \%refs;
484 $ git diff-tree -r --no-commit-id -M b222ff0a7260cc1777c7e455dfcaf22551a512fc 7e54e579e196c6c545fee1030175f65a111039d4
485 :100644 100644 8976ebc7df65475b3def53a1653533c3f61070d0 852b6e170f1bad1fbd9930d3178dda8fdf1feae7 M TODO
486 :100644 100644 75f5e5f9ed10ae82a960fde77ecf138159c37610 7f54f8c3a4ad426f6889b13cfba5f5ad9969e3c6 M lib/Gitalist/Controller/Root.pm
487 :100644 100644 2c65caa46b56302502b9e6eef952b6f379c71fee e418acf5f7b5f771b0b2ef8be784e8dcd60a4271 M lib/Gitalist/View/Default.pm
488 :000000 100644 0000000000000000000000000000000000000000 642599f9ccfc4dbc7034987ad3233655010ff348 A lib/Gitalist/View/SyntaxHighlight.pm
489 :000000 100644 0000000000000000000000000000000000000000 3d2e533c41f01276b6f844bae98297273b38dffc A root/static/css/syntax-dark.css
490 :100644 100644 6a85d6c6315b55a99071974eb6ce643aeb2799d6 44c03ed6c328fa6de4b1d9b3f19a3de96b250370 M templates/blob.tt2
494 use List::MoreUtils qw(zip);
495 # XXX Hrm, getting called twice, not sure why.
497 my($self, $commit) = @_;
499 my @dtout = $self->command(
500 # XXX should really deal with multple parents ...
501 qw(diff-tree -r --no-commit-id -M), $commit->parent_sha1, $commit->sha1
504 my @keys = qw(modesrc modedst sha1src sha1dst status src dst);
506 # see. man git-diff-tree for more info
507 # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst]
508 my @vals = /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX])\t([^\t]+)(?:\t([^\n]+))?$/;
509 my %line = zip @keys, @vals;
510 # Some convenience keys
511 $line{file} = $line{src};
512 $line{sha1} = $line{sha1dst};
513 $line{is_new} = $line{sha1src} =~ /^0+$/;
521 my ($self, $project, $rev) = @_;
523 #FIXME: huge memory consuption
525 return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
530 __PACKAGE__->meta->make_immutable;