epic-s6ts
[kivitendo-erp.git] / SL / Git.pm
1 package SL::Git;
2
3 use strict;
4 use warnings;
5
6 use parent qw(Rose::Object);
7
8 use Carp;
9 use List::Util qw(first);
10
11 sub is_git_installation {
12   my ($self) = @_;
13
14   return $self->git_exe && -d ".git" && -f ".git/config" ? 1 : 0;
15 }
16
17 sub git_exe {
18   my ($self) = @_;
19
20   return $self->{git_exe} if $self->{_git_exe_search};
21
22   $self->{_git_exe_search} = 1;
23   $self->{git_exe}         = first { -x } map { "${_}/git" } split m/:/, $ENV{PATH};
24
25   return $self->{git_exe};
26 }
27
28 sub get_log {
29   my ($self, %params) = @_;
30
31   croak "No git executable found" if !$self->git_exe;
32
33   my $since_until = join '..', $params{since}, $params{until};
34   my $in          = IO::File->new($self->git_exe . qq! log --format='tformat:\%H|\%an|\%ae|\%ai|\%s' ${since_until} |!);
35
36   if (!$in) {
37     no warnings 'once';
38     $::lxdebug->message(LXDebug::WARN(), "Error spawning git: $!");
39     return ();
40   }
41
42   my @log = grep { $_ } map { $self->_parse_log_line($_) } <$in>;
43   $in->close;
44
45   return @log;
46 }
47
48 sub _parse_log_line {
49   my ($self, $line) = @_;
50
51   chomp $line;
52
53   my @fields = split m/\|/, $line, 5;
54   return undef unless scalar(@fields) == 5;
55
56   my %commit     = (
57     hash         => $fields[0],
58     author_name  => $fields[1],
59     author_email => $fields[2],
60     subject      => $fields[4],
61   );
62
63   if ($fields[3] =~ m/^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)\s+?([\+\-]?\d+)?$/) {
64     $commit{author_date} = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6, time_zone => $7);
65   }
66
67   return \%commit;
68 }
69
70 1;