File Coverage

lib/Git/Lint/Config.pm
Criterion Covered Total %
statement 60 64 93.7
branch 14 18 77.7
condition 6 9 66.6
subroutine 10 10 100.0
pod 2 2 100.0
total 92 103 89.3


line stmt bran cond sub pod time code
1             package Git::Lint::Config;
2              
3 4     4   1344984 use strict;
  4         10  
  4         180  
4 4     4   73 use warnings;
  4         11  
  4         281  
5              
6 4     4   2270 use Module::Loader;
  4         81478  
  4         191  
7 4     4   2842 use List::MoreUtils ();
  4         74869  
  4         163  
8 4     4   2444 use Git::Lint::Command;
  4         22  
  4         4887  
9              
10             our $VERSION = '1.000';
11              
12             sub load {
13 5     5 1 26655 my $class = shift;
14 5         19 my $self = { profiles => undef };
15              
16 5         22 $self->{profiles}{commit}{default} = [];
17 5         16 $self->{profiles}{message}{default} = [];
18              
19 5         11 bless $self, $class;
20              
21 5         23 my $user_config = $self->user_config();
22              
23             # add user defined config settings
24 5 50       32 if ( exists $user_config->{config} ) {
25 0         0 $self->{config} = $user_config->{config};
26             }
27              
28             # if localdir is defined, add it into INC to load modules from
29 5 50 33     35 if ( exists $self->{config} && defined $self->{config}{localdir} ) {
30 0 0       0 if ( -d $self->{config}{localdir} ) {
31 0         0 push @INC, $self->{config}{localdir};
32             }
33             else {
34 0         0 warn "git-lint: [warn] configured localdir $self->{config}{localdir} isn't a readable directory\n";
35             }
36             }
37              
38             # all check modules are added to the default profile
39 5         33 my $loader = Module::Loader->new;
40 5         41 my $namespace = 'Git::Lint::Check::Commit';
41 5     11   40 my @commit_checks = List::MoreUtils::apply {s/$namespace\:\://g} $loader->find_modules( $namespace, { max_depth => 1 } );
  11         7325  
42              
43 5 100       38 if (@commit_checks) {
44 4         17 $self->{profiles}{commit}{default} = \@commit_checks;
45             }
46              
47 5         12 $namespace = 'Git::Lint::Check::Message';
48 5     10   31 my @message_checks = List::MoreUtils::apply {s/$namespace\:\://g} $loader->find_modules( $namespace, { max_depth => 1 } );
  10         4245  
49              
50 5 100       31 if (@message_checks) {
51 4         12 $self->{profiles}{message}{default} = \@message_checks;
52             }
53              
54             # user defined profiles override internally defined profiles
55 5         10 foreach my $check ( keys %{ $user_config->{profiles} } ) {
  5         18  
56 3         6 foreach my $profile ( keys %{ $user_config->{profiles}{$check} } ) {
  3         8  
57 4         11 $self->{profiles}{$check}{$profile} = $user_config->{profiles}{$check}{$profile};
58             }
59             }
60              
61 5         31 return $self;
62             }
63              
64             sub user_config {
65 3     3 1 33133 my $self = shift;
66              
67 3         14 my @git_config_cmd = (qw{ git config --get-regexp ^lint });
68              
69 3         13 my ( $stdout, $stderr, $exit ) = Git::Lint::Command::run( \@git_config_cmd );
70              
71             # if there is no user config, the git config command above will return 1
72             # but without stderr.
73 3 100 100     23 if ( $exit && !$stderr ) {
74 1         11 die "configuration setup is required. see the documentation for instructions.\n";
75             }
76              
77             # if there was an error, propagate that up.
78 2 100 66     11 if ( $exit && $stderr ) {
79 1         10 die "$stderr\n";
80             }
81              
82 1         3 my %parsed_config = ();
83              
84             # load check profiles
85 1         5 foreach my $line ( split( /\n/, $stdout ) ) {
86 3 100       18 next unless $line =~ /^lint\.profiles\.(\w+)\.(\w+)\s+(.+)$/;
87 2         11 my ( $check, $profile, $value ) = ( $1, $2, $3 );
88              
89 2     7   17 my @values = List::MoreUtils::apply {s/^\s+|\s+$//g} split( /,/, $value );
  7         67  
90 2         9 push @{ $parsed_config{profiles}{$check}{$profile} }, @values;
  2         13  
91             }
92              
93             # load other config settings
94 1         5 foreach my $line ( split( /\n/, $stdout ) ) {
95 3 100       10 if ( $line =~ /^lint\.config\.localdir\s+(.+)$/ ) {
96 1         3 my ($value) = ($1);
97 1         5 $parsed_config{config}{localdir} = $value;
98             }
99             }
100              
101 1         9 return \%parsed_config;
102             }
103              
104             1;
105              
106             __END__