File Coverage

blib/lib/OTRS/OPM/Maker/Utils/Git.pm
Criterion Covered Total %
statement 57 57 100.0
branch 13 16 81.2
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 77 82 93.9


line stmt bran cond sub pod time code
1             package OTRS::OPM::Maker::Utils::Git;
2              
3 5     5   273989 use strict;
  5         47  
  5         179  
4 5     5   32 use warnings;
  5         13  
  5         193  
5              
6 5     5   33 use List::Util qw(first);
  5         13  
  5         501  
7 5     5   40 use Carp;
  5         14  
  5         3781  
8              
9             our $GIT = 'git';
10              
11             sub commits {
12 3     3 0 329 my ($class, %params) = @_;
13              
14 3         33 chdir $params{dir};
15              
16 3         33813 my $log = qx{$GIT log -G"" --pretty=format:"%H %aI" *.sopm};
17              
18 3         107 my @lines = split /\n/, $log;
19              
20 3         16 my @found_commits;
21              
22 3         26 for my $line ( reverse @lines ) {
23 18         252 my ($commit_hash, $date) = split /\s+/, $line, 2;
24              
25 18         235818 my $commit_log = qx{ $GIT log -p $commit_hash };
26 18         875 my ($version) = $commit_log =~ m{\+ \s+ (.*?) }xms;
27              
28             my $is_found_version_greater = _check_version(
29             old_version => $params{version},
30 18         366 new_version => $version,
31             );
32              
33 18 100       930 if ( $is_found_version_greater ) {
34 15         352 push @found_commits, { hash => $commit_hash, version => $version, date => $date };
35             }
36             }
37              
38 3 50       48 return if !@found_commits;
39              
40 3         27 my $changes = '';
41 3         33 for my $index ( reverse 0 .. $#found_commits ) {
42 15         80 my $commit = $found_commits[$index];
43 15         80 my $version = $commit->{version};
44 15         71 my $hash = $commit->{hash};
45 15         118 my $date = $commit->{date};
46              
47 15 100       124 my $next_hash = $index >= $#found_commits ? '' : $found_commits[$index+1]->{hash};
48 15         91 my $range = $hash . '..' . $next_hash;
49              
50 15         225379 my $all_commits = qx{ $GIT log --pretty=format:"---%n%m%H%n%B" $range};
51              
52 15         723 my @commits = split /^---\n>/ms, $all_commits;
53              
54 15         298 $changes .= sprintf "%s %s\n\n", $version, $date;
55              
56             COMMIT_ENTITY:
57 15         200 for my $commit_entity ( @commits ) {
58 60         354 my ($commit_hash, @message) = split /\n/, $commit_entity;
59              
60 60 100       371 next COMMIT_ENTITY if !$commit_hash;
61 45 50       183 next COMMIT_ENTITY if !@message;
62              
63 45         125 my $i = 0;
64 45 100       258 $changes .= join "\n", map{ my $indent = $i++ ? 8 : 4; (" " x $indent) . $_ }@message;
  75         338  
  75         523  
65 45         225 $changes .= "\n\n";
66             }
67              
68 15         149 $changes .= "\n";
69             }
70              
71 3         174 return $changes
72             }
73              
74             sub find_toplevel {
75 1     1 0 198 my ($class, %params) = @_;
76              
77 1 50       7 return if !$params{dir};
78              
79 1         15 chdir $params{dir};
80              
81 1         8964 my $path = qx{$GIT rev-parse --show-toplevel};
82 1         40 return $path;
83             }
84              
85             sub _check_version {
86 22     22   2349 my (%params) = @_;
87              
88 22 100       215 return 1 if !$params{old_version};
89              
90 10         149 my $old = sprintf "%03d%03d%03d", split /\./, $params{old_version};
91 10         77 my $new = sprintf "%03d%03d%03d", split /\./, $params{new_version};
92              
93 10         74 return $new > $old;
94             }
95              
96             1;
97              
98             __END__