Loginbildschirm: Unter Versionsnummer auch aktuelle Git-Revisionsnummer anzeigen
[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     $::lxdebug->message(LXDebug::WARN(), "Error spawning git: $!");
38     return ();
39   }
40
41   my @log = grep { $_ } map { $self->_parse_log_line($_) } <$in>;
42   $in->close;
43
44   return @log;
45 }
46
47 sub _parse_log_line {
48   my ($self, $line) = @_;
49
50   chomp $line;
51
52   my @fields = split m/\|/, $line, 5;
53   return undef unless scalar(@fields) == 5;
54
55   my %commit     = (
56     hash         => $fields[0],
57     author_name  => $fields[1],
58     author_email => $fields[2],
59     subject      => $fields[4],
60   );
61
62   if ($fields[3] =~ m/^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)\s+?([\+\-]?\d+)?$/) {
63     $commit{author_date} = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6, time_zone => $7);
64   }
65
66   return \%commit;
67 }
68
69 1;