File Coverage

blib/lib/Devel/Git/MultiBisect/Init.pm
Criterion Covered Total %
statement 17 54 31.4
branch 0 16 0.0
condition 0 2 0.0
subroutine 6 8 75.0
pod 1 1 100.0
total 24 81 29.6


line stmt bran cond sub pod time code
1             package Devel::Git::MultiBisect::Init;
2 7     7   70 use v5.14.0;
  7         21  
3 7     7   29 use warnings;
  7         10  
  7         171  
4 7     7   41 use Carp;
  7         11  
  7         615  
5 7     7   37 use Cwd;
  7         9  
  7         413  
6 7     7   38 use File::Spec;
  7         19  
  7         164  
7 7     7   4653 use File::Temp;
  7         138716  
  7         3226  
8              
9             our $VERSION = '0.20';
10             $VERSION = eval $VERSION;
11              
12             =head1 NAME
13              
14             Devel::Git::MultiBisect::Init - Initializer for Devel::Git::MultiBisect
15              
16             =head1 DESCRIPTION
17              
18             This package exports no subroutines. Subroutine C should be called
19             with a fully qualified name inside Cnew()>.
20              
21             =head1 SUBROUTINES
22              
23             =head2 C
24              
25             =over 4
26              
27             =item * Purpose
28              
29             Initializer, to be called from within constructors such as that of
30             F. Not meant to be publicly called.
31              
32             =item * Argument
33              
34             my $data = Devel::Git::MultiBisect::Init::init($params);
35              
36             Single hash reference, typically, the output of C.
37              
38             =item * Return Value
39              
40             Single hash reference.
41              
42             =back
43              
44             =cut
45              
46             sub init {
47 0     0 1   my $params = shift;
48 0           my %data;
49              
50 0           while (my ($k,$v) = each %{$params}) {
  0            
51 0           $data{$k} = $v;
52             }
53              
54 0           my @missing_dirs = ();
55 0           for my $dir ( qw| gitdir outputdir | ) {
56             push @missing_dirs, $data{$dir}
57 0 0         unless (-d $data{$dir});
58             }
59 0 0         if (@missing_dirs) {
60 0           croak "Cannot find directory(ies): @missing_dirs";
61             }
62              
63 0           $data{last_short} = substr($data{last}, 0, $data{short});
64 0           $data{commits} = _get_commits(\%data);
65 0   0       $data{targets} //= [];
66 0           $data{commit_counter} = 0;
67              
68 0           return \%data;
69             }
70              
71             sub _get_commits {
72 0     0     my $dataref = shift;
73 0           my $cwd = cwd();
74 0 0         chdir $dataref->{gitdir} or croak "Unable to chdir";
75 0           my @commits = ();
76 0           my ($older, $cmd);
77 0           my $err = File::Temp->new( UNLINK => 1, SUFFIX => '.err' );
78 0 0         if ($dataref->{last_before}) {
79 0           $older = '^' . $dataref->{last_before};
80 0           $cmd = "git rev-list --reverse $older $dataref->{last} 2>$err";
81             }
82             else {
83 0           $older = $dataref->{first} . '^';
84 0           $cmd = "git rev-list --reverse ${older}..$dataref->{last} 2>$err";
85             }
86 0           chomp(@commits = `$cmd`);
87 0 0         if (! -z $err) {
88 0 0         open my $FH, '<', $err or croak "Unable to open $err for reading";
89 0           my $error = <$FH>;
90 0           chomp($error);
91 0 0         close $FH or croak "Unable to close $err after reading";
92 0           croak $error;
93             }
94 0           my @extended_commits = map { {
95             sha => $_,
96 0           short => substr($_, 0, $dataref->{short}),
97             } } @commits;
98 0 0         chdir $cwd or croak "Unable to return to original directory";
99 0           return [ @extended_commits ];
100             }
101              
102             1;
103