7d9813771cdf962fa2fc91e9849d7e8cf5f67402
[catagits/Gitalist.git] / lib / Gitalist / Model / Git.pm
1 package Gitalist::Model::Git;
2
3 use Moose;
4 use namespace::autoclean;
5
6 BEGIN { extends 'Catalyst::Model' }
7
8 use DateTime;
9 use Path::Class;
10 use Carp qw/croak/;
11 use File::Find::Rule;
12 use DateTime::Format::Mail;
13 use File::Stat::ModeString;
14 use List::MoreUtils qw/any/;
15 use Scalar::Util qw/blessed/;
16
17 {
18         my $git;
19         sub git {
20                 return $git
21                         if $git;
22
23                 if (my $config_git = Gitalist->config->{git}) {
24                         $git = $config_git if -x $config_git;
25                 }
26                 else {
27                         require File::Which;
28                         $git = File::Which::which('git');
29                 }
30
31                 if (!$git) {
32                         die <<EOR
33 Could not find a git executable.
34 Please specify the which git executable to use in gitweb.yml
35 EOR
36                 }
37
38                 return $git;
39         }
40 }
41
42 sub is_git_repo {
43     my ($self, $dir) = @_;
44
45     return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD');
46 }
47
48 sub project_info {
49     my ($self, $project) = @_;
50
51     return {
52         name => $project,
53         $self->get_project_properties(
54                 $self->git_dir_from_project_name($project),
55         ),
56     };
57 }
58
59 sub get_project_properties {
60     my ($self, $dir) = @_;
61     my %props;
62
63     eval {
64         $props{description} = $dir->file('description')->slurp;
65         chomp $props{description};
66     };
67
68     if ($props{description} && $props{description} =~ /^Unnamed repository;/) {
69         delete $props{description};
70     }
71
72     $props{owner} = (getpwuid $dir->stat->uid)[6];
73
74     my $output = $self->run_cmd_in($dir, qw{
75             for-each-ref --format=%(committer)
76             --sort=-committerdate --count=1 refs/heads
77     });
78
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;
83     }
84
85     return %props;
86 }
87
88 sub list_projects {
89     my ($self) = @_;
90
91     my $base = dir(Gitalist->config->{repo_dir});
92
93     my @ret;
94     my $dh = $base->open;
95     while (my $file = $dh->read) {
96         next if $file =~ /^.{1,2}$/;
97
98         my $obj = $base->subdir($file);
99         next unless -d $obj;
100         next unless $self->is_git_repo($obj);
101                 # XXX Leaky abstraction alert!
102                 my $is_bare = !-d $obj->subdir('.git');
103
104                 my $name = (File::Spec->splitdir($obj))[-1];
105         push @ret, {
106             name => ($name . ( $is_bare ? '.git' : '/.git' )),
107             $self->get_project_properties(
108                                 $is_bare ? $obj : $obj->subdir('.git')
109                         ),
110         };
111     }
112
113     return \@ret;
114 }
115
116 sub run_cmd {
117     my ($self, @args) = @_;
118
119     open my $fh, '-|', __PACKAGE__->git, @args
120         or die "failed to run git command";
121     binmode $fh, ':encoding(UTF-8)';
122
123     my $output = do { local $/ = undef; <$fh> };
124     close $fh;
125
126     return $output;
127 }
128
129 sub run_cmd_in {
130     my ($self, $project, @args) = @_;
131
132     my $path;
133     if (blessed($project) && $project->isa('Path::Class::Dir')) {
134         $path = $project->stringify;
135     }
136     else {
137         $path = $self->git_dir_from_project_name($project);
138     }
139     return $self->run_cmd('--git-dir' => $path, @args);
140 }
141
142 sub git_dir_from_project_name {
143     my ($self, $project) = @_;
144
145     return dir(Gitalist->config->{repo_dir})->subdir($project);
146 }
147
148 sub get_head_hash {
149     my ($self, $project) = @_;
150
151     my $output = $self->run_cmd_in($project, qw/rev-parse --verify HEAD/ );
152     return unless defined $output;
153
154     my ($head) = $output =~ /^([0-9a-fA-F]{40})$/;
155     return $head;
156 }
157
158 sub list_tree {
159     my ($self, $project, $rev) = @_;
160
161     $rev ||= $self->get_head_hash($project);
162
163     my $output = $self->run_cmd_in($project, qw/ls-tree -z/, $rev);
164     return unless defined $output;
165
166     my @ret;
167     for my $line (split /\0/, $output) {
168         my ($mode, $type, $object, $file) = split /\s+/, $line, 4;
169
170         push @ret, {
171             mode   => oct $mode,
172             type   => $type,
173             object => $object,
174             file   => $file,
175         };
176     }
177
178     return @ret;
179 }
180
181 sub get_object_mode_string {
182     my ($self, $object) = @_;
183
184     return unless $object && $object->{mode};
185     return mode_to_string($object->{mode});
186 }
187
188 sub get_object_type {
189     my ($self, $project, $object) = @_;
190
191     my $output = $self->run_cmd_in($project, qw/cat-file -t/, $object);
192     return unless $output;
193
194     chomp $output;
195     return $output;
196 }
197
198 sub cat_file {
199     my ($self, $project, $object) = @_;
200
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');
204
205     my $output = $self->run_cmd_in($project, qw/cat-file -p/, $object);
206     return unless $output;
207
208     return $output;
209 }
210
211 sub valid_rev {
212     my ($self, $rev) = @_;
213
214     return unless $rev;
215     return ($rev =~ /^([0-9a-fA-F]{40})$/);
216 }
217
218 sub diff {
219     my ($self, $project, @revs) = @_;
220
221     croak("Gitalist::Model::Git::diff needs a project and either one or two revisions")
222         if scalar @revs < 1
223         || scalar @revs > 2
224         || any { !$self->valid_rev($_) } @revs;
225
226     my $output = $self->run_cmd_in($project, 'diff', @revs);
227     return unless $output;
228
229     return $output;
230 }
231
232 {
233     my $formatter = DateTime::Format::Mail->new;
234
235     sub parse_rev_list {
236         my ($self, $output) = @_;
237         my @ret;
238
239         my @revs = split /\0/, $output;
240
241         for my $rev (split /\0/, $output) {
242             for my $line (split /\n/, $rev, 6) {
243                 chomp $line;
244                 next unless $line;
245
246                 if ($self->valid_rev($line)) {
247                     push @ret, {rev => $line};
248                     next;
249                 }
250
251                 if (my ($key, $value) = $line =~ /^(tree|parent)\s+(.*)$/) {
252                     $ret[-1]->{$key} = $value;
253                     next;
254                 }
255
256                 if (my ($key, $value, $epoch, $tz) = $line =~ /^(author|committer)\s+(.*)\s+(\d+)\s+([+-]\d+)$/) {
257                     $ret[-1]->{$key} = $value;
258                     eval {
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);
262                     };
263
264                     if ($@) {
265                         $ret[-1]->{ $key . "_datetime" } = "$epoch $tz";
266                     }
267
268                     if (my ($name, $email) = $value =~ /^([^<]+)\s+<([^>]+)>$/) {
269                         $ret[-1]->{ $key . "_name"  } = $name;
270                         $ret[-1]->{ $key . "_email" } = $email;
271                     }
272                 }
273
274                 $line =~ s/^\n?\s{4}//;
275                 $ret[-1]->{longmessage} = $line;
276                 $ret[-1]->{message} = (split /\n/, $line, 2)[0];
277             }
278         }
279
280         return @ret;
281     }
282 }
283
284 sub list_revs {
285     my ($self, $project, %args) = @_;
286
287     $args{rev} ||= $self->get_head_hash($project);
288
289     my $output = $self->run_cmd_in($project, 'rev-list',
290             '--header',
291             (defined $args{ count } ? "--max-count=$args{count}" : ()),
292             (defined $args{ skip  } ? "--skip=$args{skip}"     : ()),
293             $args{rev},
294             '--',
295             ($args{file} || ()),
296     );
297     return unless $output;
298
299     my @revs = $self->parse_rev_list($output);
300
301     return \@revs;
302 }
303
304 sub rev_info {
305     my ($self, $project, $rev) = @_;
306
307     return unless $self->valid_rev($rev);
308
309     return $self->list_revs($project, rev => $rev, count => 1);
310 }
311
312 sub get_heads {
313     my ($self, $project) = @_;
314
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;
317
318     my @ret;
319     for my $line (split /\n/, $output) {
320         my ($rev, $head, $commiter) = split /\0/, $line, 3;
321         $head =~ s!^refs/heads/!!;
322
323         push @ret, { rev => $rev, name => $head };
324
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;
330         }
331     }
332
333     return \@ret;
334 }
335
336 sub archive {
337     my ($self, $project, $rev) = @_;
338
339     #FIXME: huge memory consuption
340     #TODO: compression
341     return $self->run_cmd_in($project, qw/archive --format=tar/, "--prefix=${project}/", $rev);
342 }
343
344 1;
345
346 __PACKAGE__->meta->make_immutable;