1 package Gitalist::Model::Git;
4 use namespace::autoclean;
6 BEGIN { extends 'Catalyst::Model' }
12 use DateTime::Format::Mail;
13 use File::Stat::ModeString;
14 use List::MoreUtils qw/any/;
15 use Scalar::Util qw/blessed/;
23 if (my $config_git = Gitalist->config->{git}) {
24 $git = $config_git if -x $config_git;
28 $git = File::Which::which('git');
33 Could not find a git executable.
34 Please specify the which git executable to use in gitweb.yml
43 my ($self, $dir) = @_;
45 return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
49 my ($self, $project) = @_;
53 $self->get_project_properties(
54 $self->git_dir_from_project_name($project),
59 sub get_project_properties {
60 my ($self, $dir) = @_;
64 $props{description} = $dir->file('description')->slurp;
65 chomp $props{description};
68 if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
69 delete $props{description};
72 $props{owner} = (getpwuid $dir->stat->uid)[6];
74 my $output = $self->run_cmd_in($dir, qw{
75 for-each-ref --format=%(committer)
76 --sort=-committerdate --count=1 refs/heads
79 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
80 my $dt = DateTime->from_epoch(epoch => $epoch);
81 $dt->set_time_zone($tz);
82 $props{last_change} = $dt;
91 my $base = dir(Gitalist->config->{repo_dir});
95 while (my $file = $dh->read) {
96 next if $file =~ /^.{1,2}$/;
98 my $obj = $base->subdir($file);
100 next unless $self->is_git_repo($obj);
101 # XXX Leaky abstraction alert!
102 my $is_bare = !-d $obj->subdir('.git');
104 my $name = (File::Spec->splitdir($obj))[-1];
106 name => ($name . ( $is_bare ? '.git' : '/.git' )),
107 $self->get_project_properties(
108 $is_bare ? $obj : $obj->subdir('.git')
113 return [sort { $a->{name} cmp $b->{name} } @ret];
117 my ($self, @args) = @_;
119 open my $fh, '-|', __PACKAGE__->git, @args
120 or die "failed to run git command";
121 binmode $fh, ':encoding(UTF-8)';
123 print STDERR "RAN - git @_[1..$#_]\n";
125 my $output = do { local $/ = undef; <$fh> };
132 my ($self, $project, @args) = @_;
135 if (blessed($project) && $project->isa('Path::Class::Dir')) {
136 $path = $project->stringify;
139 $path = $self->git_dir_from_project_name($project);
141 return $self->run_cmd('--git-dir' => $path, @args);
144 sub git_dir_from_project_name {
145 my ($self, $project) = @_;
147 return dir(Gitalist->config->{repo_dir})->subdir($project);
151 my ($self, $project) = @_;
153 my $output = $self->run_cmd_in($project, qw/rev-parse --verify HEAD/ );
154 return unless defined $output;
156 my ($head) = $output =~ /^([0-9a-fA-F]{40})$/;
161 my ($self, $project, $rev) = @_;
163 $rev ||= $self->get_head_hash($project);
165 my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
166 return unless defined $output;
169 for my $line (split /\0/, $output) {
170 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
183 sub get_object_mode_string {
184 my ($self, $object) = @_;
186 return unless $object && $object->{mode};
187 return mode_to_string($object->{mode});
190 sub get_object_type {
191 my ($self, $project, $object) = @_;
193 my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
194 return unless $output;
200 sub get_hash_by_path {
201 my($self, $project, $base, $path, $type) = @_;
205 my $line = $self->run_cmd_in($project, 'ls-tree', $base, '--', $path)
208 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
209 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
210 return defined $type && $type ne $2
216 my ($self, $project, $object) = @_;
218 my $type = $self->get_object_type($project, $object);
219 die "object `$object' is not a file\n"
220 if (!defined $type || $type ne 'blob');
222 my $output = $self->run_cmd_in($project, qw/cat-file -p/, $object);
223 return unless $output;
229 my ($self, $rev) = @_;
232 return ($rev =~ /^([0-9a-fA-F]{40})$/);
236 my ($self, $project, @revs) = @_;
238 croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
241 || any { !$self->valid_rev($_) } @revs;
243 my $output = $self->run_cmd_in($project, 'diff', @revs);
244 return unless $output;
250 my $formatter = DateTime::Format::Mail->new;
253 my ($self, $output) = @_;
256 my @revs = split /\0/, $output;
258 for my $rev (split /\0/, $output) {
259 for my $line (split /\n/, $rev, 6) {
263 if ($self->valid_rev($line)) {
264 push @ret, {rev => $line};
268 if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
269 $ret[-1]->{$key} = $value;
273 if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
274 $ret[-1]->{$key} = $value;
276 $ret[-1]->{ $key . "_datetime" } = DateTime->from_epoch(epoch => $epoch);
277 $ret[-1]->{ $key . "_datetime" }->set_time_zone($tz);
278 $ret[-1]->{ $key . "_datetime" }->set_formatter($formatter);
282 $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
285 if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
286 $ret[-1]->{ $key . "_name" } = $name;
287 $ret[-1]->{ $key . "_email" } = $email;
291 $line =~ s/^\n?\s{4}//;
292 $ret[-1]->{longmessage} = $line;
293 $ret[-1]->{message} = (split /\n/, $line, 2)[0];
302 my ($self, $project, %args) = @_;
304 $args{rev} ||= $self->get_head_hash($project);
306 my $output = $self->run_cmd_in($project, 'rev-list',
308 (defined $args{ count } ? "--max-count=$args{count}" : ()),
309 (defined $args{ skip } ? "--skip=$args{skip}" : ()),
314 return unless $output;
316 my @revs = $self->parse_rev_list($output);
322 my ($self, $project, $rev) = @_;
324 return unless $self->valid_rev($rev);
326 return $self->list_revs($project, rev => $rev, count => 1);
330 my ($self, $project, @logargs) = @_;
333 = $self->run_cmd_in($project, qw(log -g), @logargs)
334 =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg;
338 commit 02526fc15beddf2c64798a947fecdd8d11bf993d
339 Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>)
341 Author: Iain Loasby <iloasby@rowlf.of-2.uk.venda.com>
342 Date: Thu Sep 17 12:26:05 2009 +0100
344 Merge branch 'rt125181
347 # XXX Stuff like this makes me want to switch to Git::PurePerl
348 my($sha1, $type, $author, $date)
350 ^ commit \s+ ([0-9a-f]+)$
352 Reflog[ ]message: \s+ (.+?)$ \s+
353 Author: \s+ ([^<]+) <.*?$ \s+
357 pos($_) = index($_, $date) + length $date;
358 # Yeah, I just did that.
360 my($msg) = /\G\s+(\S.*)/sg;
366 # XXX Add DateTime goodness.
374 my ($self, $project) = @_;
376 my $output = $self->run_cmd_in($project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
377 return unless $output;
380 for my $line (split /\n/, $output) {
381 my ($rev, $head, $commiter) = split /\0/, $line, 3;
382 $head =~ s!^refs/heads/!!;
384 push @ret, { rev => $rev, name => $head };
386 #FIXME: That isn't the time I'm looking for..
387 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
388 my $dt = DateTime->from_epoch(epoch => $epoch);
389 $dt->set_time_zone($tz);
390 $ret[-1]->{last_change} = $dt;
398 my ($self, $project, $rev) = @_;
400 #FIXME: huge memory consuption
402 return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
407 __PACKAGE__->meta->make_immutable;