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 has project => ( isa => NonEmptySimpleStr, is => 'rw');
20 has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class
21 has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
25 $self->git; # Cause lazy value build.
30 my $git = File::Which::which('git');
34 Could not find a git executable.
35 Please specify the which git executable to use in gitweb.yml
43 return Gitalist->config->{repo_dir};
47 my ($self, $dir) = @_;
49 return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
53 my ($self, $project) = @_;
57 $self->get_project_properties(
58 $self->git_dir_from_project_name($project),
63 sub get_project_properties {
64 my ($self, $dir) = @_;
68 $props{description} = $dir->file('description')->slurp;
69 chomp $props{description};
72 if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
73 delete $props{description};
76 ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//;
78 my $output = $self->run_cmd_in($dir, qw{
79 for-each-ref --format=%(committer)
80 --sort=-committerdate --count=1 refs/heads
83 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
84 my $dt = DateTime->from_epoch(epoch => $epoch);
85 $dt->set_time_zone($tz);
86 $props{last_change} = $dt;
95 my $base = dir($self->repo_dir);
99 while (my $file = $dh->read) {
100 next if $file =~ /^.{1,2}$/;
102 my $obj = $base->subdir($file);
104 next unless $self->is_git_repo($obj);
106 # XXX Leaky abstraction alert!
107 my $is_bare = !-d $obj->subdir('.git');
109 my $name = (File::Spec->splitdir($obj))[-1];
111 name => ($name . ( $is_bare ? '.git' : '/.git' )),
112 $self->get_project_properties(
113 $is_bare ? $obj : $obj->subdir('.git')
118 return [sort { $a->{name} cmp $b->{name} } @ret];
122 my ($self, @args) = @_;
124 open my $fh, '-|', $self->git, @args
125 or die "failed to run git command";
126 binmode $fh, ':encoding(UTF-8)';
128 my $output = do { local $/ = undef; <$fh> };
135 my($self, $project) = @_;
137 my $dir = blessed($project) && $project->isa('Path::Class::Dir')
138 ? $project->stringify
139 : $self->git_dir_from_project_name($project);
147 my ($self, $project, @args) = @_;
149 return $self->run_cmd('--git-dir' => $self->project_dir($project), @args);
152 sub git_dir_from_project_name {
153 my ($self, $project) = @_;
155 return dir($self->repo_dir)->subdir($project);
159 my ($self, $project) = @_;
161 my $output = $self->run_cmd_in($self->project, qw/rev-parse --verify HEAD/ );
162 return unless defined $output;
164 my ($head) = $output =~ /^([0-9a-fA-F]{40})$/;
169 my ($self, $project, $rev) = @_;
171 $rev ||= $self->get_head_hash($project);
173 my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
174 return unless defined $output;
177 for my $line (split /\0/, $output) {
178 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
191 sub get_object_mode_string {
192 my ($self, $object) = @_;
194 return unless $object && $object->{mode};
195 return mode_to_string($object->{mode});
198 sub get_object_type {
199 my ($self, $project, $object) = @_;
201 my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
202 return unless $output;
208 sub get_hash_by_path {
209 my($self, $base, $path, $type) = @_;
213 my $line = $self->run_cmd_in($self->project, 'ls-tree', $base, '--', $path)
216 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
217 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
218 return defined $type && $type ne $2
224 my ($self, $object) = @_;
226 my $type = $self->get_object_type($self->project, $object);
227 die "object `$object' is not a file\n"
228 if (!defined $type || $type ne 'blob');
230 my $output = $self->run_cmd_in($self->project, qw/cat-file -p/, $object);
231 return unless $output;
237 my ($self, $rev) = @_;
240 return ($rev =~ /^([0-9a-fA-F]{40})$/);
244 my ($self, $project, @revs) = @_;
246 croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
249 || any { !$self->valid_rev($_) } @revs;
251 my $output = $self->run_cmd_in($project, 'diff', @revs);
252 return unless $output;
258 my $formatter = DateTime::Format::Mail->new;
261 my ($self, $output) = @_;
264 my @revs = split /\0/, $output;
266 for my $rev (split /\0/, $output) {
267 for my $line (split /\n/, $rev, 6) {
271 if ($self->valid_rev($line)) {
272 push @ret, {rev => $line};
276 if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
277 $ret[-1]->{$key} = $value;
281 if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
282 $ret[-1]->{$key} = $value;
284 $ret[-1]->{ $key . "_datetime" } = DateTime->from_epoch(epoch => $epoch);
285 $ret[-1]->{ $key . "_datetime" }->set_time_zone($tz);
286 $ret[-1]->{ $key . "_datetime" }->set_formatter($formatter);
290 $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
293 if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
294 $ret[-1]->{ $key . "_name" } = $name;
295 $ret[-1]->{ $key . "_email" } = $email;
299 $line =~ s/^\n?\s{4}//;
300 $ret[-1]->{longmessage} = $line;
301 $ret[-1]->{message} = (split /\n/, $line, 2)[0];
310 my ($self, $project, %args) = @_;
312 $args{rev} ||= $self->get_head_hash($project);
314 my $output = $self->run_cmd_in($project, 'rev-list',
316 (defined $args{ count } ? "--max-count=$args{count}" : ()),
317 (defined $args{ skip } ? "--skip=$args{skip}" : ()),
322 return unless $output;
324 my @revs = $self->parse_rev_list($output);
330 my ($self, $project, $rev) = @_;
332 return unless $self->valid_rev($rev);
334 return $self->list_revs($project, rev => $rev, count => 1);
338 my ($self, @logargs) = @_;
341 = $self->run_cmd_in($self->project, qw(log -g), @logargs)
342 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
346 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
347 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
349 Author: Iain Loasby <iloasby@rowlf.of-2.uk.venda.com>
350 Date: Thu Sep 17 12:26:05 2009 +0100
352 Merge branch 'rt125181
357 # XXX Stuff like this makes me want to switch to Git::PurePerl
358 my($sha1, $type, $author, $date)
360 ^ commit \s+ ([0-9a-f]+)$
362 Reflog[ ]message: \s+ (.+?)$ \s+
363 Author: \s+ ([^<]+) <.*?$ \s+
367 pos($_) = index($_, $date) + length $date;
369 # Yeah, I just did that.
371 my($msg) = /\G\s+(\S.*)/sg;
378 # XXX Add DateTime goodness.
386 my ($self, $project) = @_;
388 my $output = $self->run_cmd_in($project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
389 return unless $output;
392 for my $line (split /\n/, $output) {
393 my ($rev, $head, $commiter) = split /\0/, $line, 3;
394 $head =~ s!^refs/heads/!!;
396 push @ret, { rev => $rev, name => $head };
398 #FIXME: That isn't the time I'm looking for..
399 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
400 my $dt = DateTime->from_epoch(epoch => $epoch);
401 $dt->set_time_zone($tz);
402 $ret[-1]->{last_change} = $dt;
410 my ($self, $project, $rev) = @_;
412 #FIXME: huge memory consuption
414 return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
419 __PACKAGE__->meta->make_immutable;