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/;
17 # abstraction fail - but currently needed for unit tests to work
26 if (my $config_git = Gitalist->config->{git}) {
27 $git = $config_git if -x $config_git;
31 $git = File::Which::which('git');
36 Could not find a git executable.
37 Please specify the which git executable to use in gitweb.yml
46 my ($self, $dir) = @_;
48 return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
52 my ($self, $project) = @_;
56 $self->get_project_properties(
57 $self->git_dir_from_project_name($project),
62 sub get_project_properties {
63 my ($self, $dir) = @_;
67 $props{description} = $dir->file('description')->slurp;
68 chomp $props{description};
71 if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
72 delete $props{description};
75 $props{owner} = (getpwuid $dir->stat->uid)[6];
77 my $output = $self->run_cmd_in($dir, qw{
78 for-each-ref --format=%(committer)
79 --sort=-committerdate --count=1 refs/heads
82 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
83 my $dt = DateTime->from_epoch(epoch => $epoch);
84 $dt->set_time_zone($tz);
85 $props{last_change} = $dt;
94 my $base = dir(Gitalist->config->{repo_dir});
98 while (my $file = $dh->read) {
99 next if $file =~ /^.{1,2}$/;
101 my $obj = $base->subdir($file);
103 next unless $self->is_git_repo($obj);
104 # XXX Leaky abstraction alert!
105 my $is_bare = !-d $obj->subdir('.git');
107 my $name = (File::Spec->splitdir($obj))[-1];
109 name => ($name . ( $is_bare ? '.git' : '/.git' )),
110 $self->get_project_properties(
111 $is_bare ? $obj : $obj->subdir('.git')
116 return [sort { $a->{name} cmp $b->{name} } @ret];
120 my ($self, @args) = @_;
122 open my $fh, '-|', __PACKAGE__->git, @args
123 or die "failed to run git command";
124 binmode $fh, ':encoding(UTF-8)';
126 my $output = do { local $/ = undef; <$fh> };
133 my ($self, $project, @args) = @_;
136 if (blessed($project) && $project->isa('Path::Class::Dir')) {
137 $path = $project->stringify;
140 $path = $self->git_dir_from_project_name($project);
142 return $self->run_cmd('--git-dir' => $path, @args);
145 sub git_dir_from_project_name {
146 my ($self, $project) = @_;
148 return dir(Gitalist->config->{repo_dir})->subdir($project);
152 my ($self, $project) = @_;
154 my $output = $self->run_cmd_in($project, qw/rev-parse --verify HEAD/ );
155 return unless defined $output;
157 my ($head) = $output =~ /^([0-9a-fA-F]{40})$/;
162 my ($self, $project, $rev) = @_;
164 $rev ||= $self->get_head_hash($project);
166 my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
167 return unless defined $output;
170 for my $line (split /\0/, $output) {
171 my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
184 sub get_object_mode_string {
185 my ($self, $object) = @_;
187 return unless $object && $object->{mode};
188 return mode_to_string($object->{mode});
191 sub get_object_type {
192 my ($self, $project, $object) = @_;
194 my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
195 return unless $output;
202 my ($self, $project, $object) = @_;
204 my $type = $self->get_object_type($project, $object);
205 die "object `$object' is not a file\n"
206 if (!defined $type || $type ne 'blob');
208 my $output = $self->run_cmd_in($project, qw/cat-file -p/, $object);
209 return unless $output;
215 my ($self, $rev) = @_;
218 return ($rev =~ /^([0-9a-fA-F]{40})$/);
222 my ($self, $project, @revs) = @_;
224 croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
227 || any { !$self->valid_rev($_) } @revs;
229 my $output = $self->run_cmd_in($project, 'diff', @revs);
230 return unless $output;
236 my $formatter = DateTime::Format::Mail->new;
239 my ($self, $output) = @_;
242 my @revs = split /\0/, $output;
244 for my $rev (split /\0/, $output) {
245 for my $line (split /\n/, $rev, 6) {
249 if ($self->valid_rev($line)) {
250 push @ret, {rev => $line};
254 if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
255 $ret[-1]->{$key} = $value;
259 if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
260 $ret[-1]->{$key} = $value;
262 $ret[-1]->{ $key . "_datetime" } = DateTime->from_epoch(epoch => $epoch);
263 $ret[-1]->{ $key . "_datetime" }->set_time_zone($tz);
264 $ret[-1]->{ $key . "_datetime" }->set_formatter($formatter);
268 $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
271 if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
272 $ret[-1]->{ $key . "_name" } = $name;
273 $ret[-1]->{ $key . "_email" } = $email;
277 $line =~ s/^\n?\s{4}//;
278 $ret[-1]->{longmessage} = $line;
279 $ret[-1]->{message} = (split /\n/, $line, 2)[0];
288 my ($self, $project, %args) = @_;
290 $args{rev} ||= $self->get_head_hash($project);
292 my $output = $self->run_cmd_in($project, 'rev-list',
294 (defined $args{ count } ? "--max-count=$args{count}" : ()),
295 (defined $args{ skip } ? "--skip=$args{skip}" : ()),
300 return unless $output;
302 my @revs = $self->parse_rev_list($output);
308 my ($self, $project, $rev) = @_;
310 return unless $self->valid_rev($rev);
312 return $self->list_revs($project, rev => $rev, count => 1);
316 my ($self, $project) = @_;
318 my $output = $self->run_cmd_in($project, qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads');
319 return unless $output;
322 for my $line (split /\n/, $output) {
323 my ($rev, $head, $commiter) = split /\0/, $line, 3;
324 $head =~ s!^refs/heads/!!;
326 push @ret, { rev => $rev, name => $head };
328 #FIXME: That isn't the time I'm looking for..
329 if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) {
330 my $dt = DateTime->from_epoch(epoch => $epoch);
331 $dt->set_time_zone($tz);
332 $ret[-1]->{last_change} = $dt;
340 my ($self, $project, $rev) = @_;
342 #FIXME: huge memory consuption
344 return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
349 __PACKAGE__->meta->make_immutable;