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')
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 my $output = do { local $/ = undef; <$fh> };
130 my ($self, $project, @args) = @_;
133 if (blessed($project) && $project->isa('Path::Class::Dir')) {
134 $path = $project->stringify;
137 $path = $self->git_dir_from_project_name($project);
139 return $self->run_cmd('--git-dir' => $path, @args);
142 sub git_dir_from_project_name {
143 my ($self, $project) = @_;
145 return dir(Gitalist->config->{repo_dir})->subdir($project);
149 my ($self, $project) = @_;
151 my $output = $self->run_cmd_in($project, qw/rev-parse --verify HEAD/ );
152 return unless defined $output;
154 my ($head) = $output =~ /^([0-9a-fA-F]{40})$/;
159 my ($self, $project, $rev) = @_;
161 $rev ||= $self->get_head_hash($project);
163 my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
164 return unless defined $output;
167 for my $line (split /\0/, $output) {
168 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
181 sub get_object_mode_string {
182 my ($self, $object) = @_;
184 return unless $object && $object->{mode};
185 return mode_to_string($object->{mode});
188 sub get_object_type {
189 my ($self, $project, $object) = @_;
191 my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
192 return unless $output;
199 my ($self, $project, $object) = @_;
201 my $type = $self->get_object_type($project, $object);
202 die "object `$object' is not a file\n"
203 if (!defined $type || $type ne 'blob');
205 my $output = $self->run_cmd_in($project, qw/cat-file -p/, $object);
206 return unless $output;
212 my ($self, $rev) = @_;
215 return ($rev =~ /^([0-9a-fA-F]{40})$/);
219 my ($self, $project, @revs) = @_;
221 croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
224 || any { !$self->valid_rev($_) } @revs;
226 my $output = $self->run_cmd_in($project, 'diff', @revs);
227 return unless $output;
233 my $formatter = DateTime::Format::Mail->new;
236 my ($self, $output) = @_;
239 my @revs = split /\0/, $output;
241 for my $rev (split /\0/, $output) {
242 for my $line (split /\n/, $rev, 6) {
246 if ($self->valid_rev($line)) {
247 push @ret, {rev => $line};
251 if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
252 $ret[-1]->{$key} = $value;
256 if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
257 $ret[-1]->{$key} = $value;
259 $ret[-1]->{ $key . "_datetime" } = DateTime->from_epoch(epoch => $epoch);
260 $ret[-1]->{ $key . "_datetime" }->set_time_zone($tz);
261 $ret[-1]->{ $key . "_datetime" }->set_formatter($formatter);
265 $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
268 if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
269 $ret[-1]->{ $key . "_name" } = $name;
270 $ret[-1]->{ $key . "_email" } = $email;
274 $line =~ s/^\n?\s{4}//;
275 $ret[-1]->{longmessage} = $line;
276 $ret[-1]->{message} = (split /\n/, $line, 2)[0];
285 my ($self, $project, %args) = @_;
287 $args{rev} ||= $self->get_head_hash($project);
289 my $output = $self->run_cmd_in($project, 'rev-list',
291 (defined $args{ count } ? "--max-count=$args{count}" : ()),
292 (defined $args{ skip } ? "--skip=$args{skip}" : ()),
297 return unless $output;
299 my @revs = $self->parse_rev_list($output);
305 my ($self, $project, $rev) = @_;
307 return unless $self->valid_rev($rev);
309 return $self->list_revs($project, rev => $rev, count => 1);
313 my ($self, $project) = @_;
315 my $output = $self->run_cmd_in($project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
316 return unless $output;
319 for my $line (split /\n/, $output) {
320 my ($rev, $head, $commiter) = split /\0/, $line, 3;
321 $head =~ s!^refs/heads/!!;
323 push @ret, { rev => $rev, name => $head };
325 #FIXME: That isn't the time I'm looking for..
326 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
327 my $dt = DateTime->from_epoch(epoch => $epoch);
328 $dt->set_time_zone($tz);
329 $ret[-1]->{last_change} = $dt;
337 my ($self, $project, $rev) = @_;
339 #FIXME: huge memory consuption
341 return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
346 __PACKAGE__->meta->make_immutable;