blib/lib/Git/MoreHooks/GitRepoAdmin.pm

filter: Statementstmt 65.8% 102 / 155 Branchbran 32.1% 9 / 28 Conditioncond 35.0% 7 / 20 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 94.4% 17 / 18 Podpod 100% 2 / 2 totaltotal 59.4% 139 / 234 CC 25 SCAR 29.2
CC
25
Cov
58%
check_affected_refs_server_side
49.0
check_affected_refs_client_side
29.5
_new_version
3.6
CRAP 18.6
« blib/lib/Git/MoreHooks/CheckPerl.pm ↑ Summary blib/lib/Git/MoreHooks/ScanCtags.pm »
1 package Git::MoreHooks::GitRepoAdmin;
2  
3 ## no critic (InputOutput::RequireCheckedSyscalls)
4 ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
5 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
6 ## no critic (ControlStructures::ProhibitPostfixControls)
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use feature qw( say );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: Integrate with .git-repo-admin
14  
15 2 our $VERSION = '0.021';
16  
17  
18 2 ▶use Git::Hooks 2.014000; # v2.14.0: oldest release on CPAN
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Temp qw/ tempfile tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Log::Any qw{$log};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 my $PKG = __PACKAGE__;
27 2 my ($CFG) = __PACKAGE__ =~ /::([^:]+)$/msx;
28 2 $CFG = 'githooks.' . $CFG;
29  
30 #############
31 # Grok hook configuration, check it and set defaults.
32  
33 4 ▶sub _setup_config {
Subroutine _setup_config: called CC=3 SCAR=11.0
34 4 my ($git) = @_;
35 4 $log->debugf( __PACKAGE__ . '::_setup_config(%s):', '$git' );
36  
37 4 my $config = $git->get_config();
38 4 $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config );
39  
40 4 ▶ $config->{ lc $CFG } //= {};
Condition100%2/2
$$config{lc $CFG} //= {}
l!l
22
MC/DC100%1/1
$$config{lc $CFG} //= {}
$$config{lc $CFG}
Truth table100%2/2
A
A= $$config{lc $CFG}
Aresult
11
00
41  
42 4 my $default = $config->{ lc $CFG };
43 4 ▶ $default->{'ref'} //= [ '^refs/heads/main$', '^refs/heads/master$' ];
Condition100%2/2
$default->{"ref"} //= ["^refs/heads/main\$", "^refs/heads/master\$"]
l!l
22
MC/DC100%1/1
$default->{"ref"} //= ["^refs/heads/main\$", "^refs/heads/master\$"]
$default->{"ref"}
Truth table100%2/2
A
A= $default->{"ref"}
Aresult
11
00
44  
45 4 return;
46 }
47  
48 12 ▶sub _current_version {
Subroutine _current_version: called CC=3 SCAR=11.0
49 12 my ( $git, $dir ) = @_;
50 12 $log->debug( ( caller 0 )[3], '( ', $git, ', ', $dir, ')' );
51  
52 # No VERSION file means .git-repo-admin has not been installed in this
53 # repository yet, which is an ordinary state and not an error: the config
54 # enabling this plugin is tracked, so it arrives in a clone before anybody
55 # runs client-hooks.sh --install. Slurping unconditionally made that state
56 # die, and because this plugin runs from post-merge it died on `git pull`.
57 12 my $ver_file = path( File::Spec->catdir( $dir, 'VERSION' ) );
58 12 ▶ if ( !$ver_file->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ver_file->is_file) 8 4 12
59 8 $log->debug( ( caller 0 )[3], ': no VERSION file at ', $ver_file->stringify );
60 8 return;
61 }
62  
63 4 my $ver_file_cnt = $ver_file->slurp_utf8;
64 4 $log->debug( ( caller 0 )[3], ': ver_file_cnt=', $ver_file_cnt );
65 4 my ($hooks_cfg_ver) = $ver_file_cnt =~ m/^([[:digit:]]+)$/msx;
66 4 ▶ return if !$hooks_cfg_ver;
Branch: 1 branch
BranchTrueFalseTotal
unless $hooks_cfg_ver 2 2 4
67 2 return $hooks_cfg_ver;
68 }
69  
70 2 ▶sub _new_version {
Subroutine _new_version: called CC=3 SCAR=12.9
71 2 my ( $git, $ref, $dir ) = @_;
72 2 $log->debug( ( caller 0 )[3], '( ', $git, ', ', $ref, ', ', $dir, ')' );
73  
74 2 my $filepath = File::Spec->catdir( $dir, 'VERSION' );
75 2 my $object = "$ref:$filepath";
76  
77 # A branch that does not carry .git-repo-admin/VERSION is not an error
78 # either -- the official branch may predate the hooks, or the path may have
79 # been renamed. `cat-file` exits non-zero for it and Git::Repository turns
80 # a non-zero exit into a die, so the call needs catching.
81 2 my $ver_file_cnt = eval {
82 2 $git->run(
83 'cat-file',
84 '-p', $object,
85 {
86 env => {
87  
88 # Eliminate the effects of system wide and global configuration.
89 GIT_CONFIG_NOSYSTEM => 1,
90 XDG_CONFIG_HOME => undef,
91 HOME => undef,
92 },
93 },
94 );
95 };
96 2 ▶ if ( !defined $ver_file_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ver_file_cnt) 2 0 2
97 2 $log->debug( ( caller 0 )[3], ': cannot read ', $object );
98 2 return;
99 }
100  
101 0 $log->debug( ( caller 0 )[3], ': ver_file_cnt=', $ver_file_cnt );
102 0 my ($hooks_cfg_ver) = $ver_file_cnt =~ m/^([[:digit:]]+)$/msx;
103 0 ▶ return if !$hooks_cfg_ver;
Branch: 1 branch
BranchTrueFalseTotal
unless $hooks_cfg_ver 0 0 0
104 0 return $hooks_cfg_ver;
105 }
106  
107 2 ▶sub _ref_matches {
Subroutine _ref_matches: called CC=2 SCAR=6.9
108 2 my ( $git, $our_refs, $branches ) = @_;
109 2 $log->debug( ( caller 0 )[3], '( ', ( join q{:}, @{$our_refs} ), ', ', ( join q{:}, @{$branches} ), ')' );
110  
111 2 my @matches;
112 2 foreach my $our_ref ( @{$our_refs} ) {
113 0 my @m = grep { m/$our_ref/msx } @{$branches};
114 4 push @matches, @m;
115 }
116  
117 2 return @matches;
118 }
119  
120 0 ▶sub _update_server_side {
Subroutine _update_server_side: not called CC=1 SCAR=6.9
121 0 my ( $git, $ref, $dir ) = @_;
122 0 $log->debug( ( caller 0 )[3], '( ', $git, ', ', $ref, ', ', $dir, ')' );
123  
124 0 my $tmp = File::Temp->new();
125  
126 # Hook is always run with cdw as repo root dir.
127 0 my $filepath = File::Spec->catdir( $dir, 'initialize-server.sh' );
128 0 my $object = "$ref:$filepath";
129 0 my $exe_file_cnt = $git->run(
130 'cat-file',
131 '-p', $object,
132 {
133 env => {
134  
135 # Eliminate the effects of system wide and global configuration.
136 GIT_CONFIG_NOSYSTEM => 1,
137 XDG_CONFIG_HOME => undef,
138 HOME => undef,
139 },
140 },
141 );
142 0 $log->debug( ( caller 0 )[3], ': exe_file_cnt=', $exe_file_cnt );
143 0 print {$tmp} $exe_file_cnt;
144 0 my $tmp_filename = $tmp->filename;
145 0 $log->debug( ( caller 0 )[3], ': tmp_filename=', $tmp_filename );
146 0 local $OUTPUT_AUTOFLUSH = 1;
147 0 system 'bash', $tmp_filename, $ref;
148  
149 0 return;
150 }
151  
152 ##########
153  
154 2 ▶sub check_affected_refs_client_side {
Subroutine check_affected_refs_client_side: called CC=10 SCAR=33.8
Pod documented
155 2 my ( $git, $is_squash_merge ) = @_;
156 2 ▶ $log->debug( ( caller 0 )[3], '( ', $git, '( ', ( $is_squash_merge // 'undef' ), ')' );
Condition50%1/2
$is_squash_merge // "undef"
l!l
20
MC/DC0%0/1
$is_squash_merge // "undef"
$is_squash_merge
Truth table50%1/2
A
A= $is_squash_merge
Aresult
11
00
157 2 ▶ $is_squash_merge = 0 if ( !defined $is_squash_merge );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_squash_merge 0 2 2
158  
159 2 _setup_config($git);
160 2 $log->debug( ( caller 0 )[3], ': cwd=', cwd );
161  
162 # Hook is always run with cdw as repo root dir.
163 2 my $curr_ver = _current_version( $git, '.git/.git-repo-admin' );
164 2 ▶ $log->debug( ( caller 0 )[3], ': curr_ver=', ( $curr_ver // 'undef' ) );
Condition50%1/2
$curr_ver // "undef"
l!l
02
MC/DC0%0/1
$curr_ver // "undef"
$curr_ver
Truth table50%1/2
A
A= $curr_ver
Aresult
11
00
165  
166 # We cannot use $git->get_affected_refs() because post-merge hook
167 # does not get information about which branches are affected.
168 # Don't know why!!!
169 # So instead we just work with the only branch in the config.
170 2 my @our_refs = $git->get_config( $CFG => 'ref' );
171 2 $log->debug( ( caller 0 )[3], ': our_refs=', ( join q{:}, @our_refs ) );
172 2 my $branches_raw = $git->run(
173 'for-each-ref',
174 '--format',
175 '%(refname)',
176 {
177 env => {
178  
179 # Eliminate the effects of system wide and global configuration.
180 GIT_CONFIG_NOSYSTEM => 1,
181 XDG_CONFIG_HOME => undef,
182 HOME => undef,
183 },
184 },
185 );
186 2 my @branches = split qr{\n}msx, $branches_raw;
187 2 $log->debug( ( caller 0 )[3], ': branches=', ( join q{:}, @branches ) );
188  
189 2 my @matches = _ref_matches( $git, \@our_refs, \@branches );
190 2 ▶ if ( @matches > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matches > 1) { } 0 2 2
elsif (@matches == 0) { } 2 0 2
191 0 $git->fault('Config variable \'ref\' matches with more than one branch.');
192 0 return scalar @matches;
193 }
194 elsif ( @matches == 0 ) {
195 2 $git->fault('Config variable \'ref\' does not match with any branch.');
196 2 return 1;
197 }
198  
199 # Okay, no errors in the config.
200 # We always proceed to read the VERSION from the hook config branch.
201 0 my $new_ver = _new_version( $git, $matches[0], '.git-repo-admin' );
202 0 ▶ $log->debug( ( caller 0 )[3], ': new_ver=', ( $new_ver // 'undef' ) );
Condition0%0/2
$new_ver // "undef"
l!l
00
MC/DC0%0/1
$new_ver // "undef"
$new_ver
Truth table0%0/2
A
A= $new_ver
Aresult
11
00
203 0 ▶ if ( !defined $curr_ver || !defined $new_ver ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $curr_ver and defined $new_ver) 0 0 0
Condition0%0/3
defined $curr_ver and defined $new_ver
!ll&&!rl&&r
000
MC/DC0%0/2
defined $curr_ver and defined $new_ver
defined $curr_ver defined $new_ver
Truth table0%0/3
A and B
A= defined $curr_verB= defined $new_ver
ABresult
0X0
100
111
204 0 $log->debug( ( caller 0 )[3], ': a version is unknown, so there is nothing to compare' );
205 0 return 0;
206 }
207 0 ▶ if ( $new_ver > $curr_ver ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_ver > $curr_ver) 0 0 0
208 0 $log->debug( ( caller 0 )[3], 'Newer version detected. Update config.' );
209 0 say '********************************************************************************';
210 0 say '* UPDATE CLIEND SIDE HOOKS *';
211 0 say '* Run .git-repo-admin/install-client-hooks.sh *';
212 0 say '* ATTN. Switch to right branch first! *';
213 0 say '********************************************************************************';
214 }
215  
216 0 return 0;
217 }
218  
219 2 ▶sub check_affected_refs_server_side {
Subroutine check_affected_refs_server_side: called CC=9 SCAR=38.9
Pod documented
220 2 my ($git) = @_;
221 2 $log->debug( ( caller 0 )[3], '( ', $git, ')' );
222  
223 2 _setup_config($git);
224 2 my $curr_ver = _current_version( $git, '.git-repo-admin' );
225 2 ▶ $log->debug( ( caller 0 )[3], ': curr_ver=', ( $curr_ver // 'undef' ) );
Condition50%1/2
$curr_ver // "undef"
l!l
02
MC/DC0%0/1
$curr_ver // "undef"
$curr_ver
Truth table50%1/2
A
A= $curr_ver
Aresult
11
00
226  
227 # We're only interested in branches
228 0 my @refs = grep { m{^refs/heads/}msx } $git->get_affected_refs();
229 2 ▶ return 1 unless @refs;
Branch: 1 branch
BranchTrueFalseTotal
unless @refs 2 0 2
230  
231 0 my @our_refs = $git->get_config( $CFG => 'ref' );
232 0 $log->debug( ( caller 0 )[3], ': our_refs=', ( join q{:}, @our_refs ) );
233  
234 # my ($r, $match) = _ref_matches( $git, \@our_refs, \@refs );
235 0 my @matches = _ref_matches( $git, \@our_refs, \@refs );
236 0 $log->debug( ( caller 0 )[3], ': matches=', ( join q{:}, @matches ) );
237 0 ▶ if ( @matches > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matches > 1) { } 0 0 0
elsif (@matches == 1) { } 0 0 0
238 0 $git->fault('Config variable \'ref\' matches with more than one branch.');
239 0 $log->debug( ( caller 0 )[3], '(): ' . scalar @matches );
240 0 return scalar @matches;
241 }
242 elsif ( @matches == 1 ) {
243 0 my $new_ver = _new_version( $git, $matches[0], '.git-repo-admin' );
244 0 ▶ $log->debug( ( caller 0 )[3], ': new_ver=', ( $new_ver // 'undef' ) );
Condition0%0/2
$new_ver // "undef"
l!l
00
MC/DC0%0/1
$new_ver // "undef"
$new_ver
Truth table0%0/2
A
A= $new_ver
Aresult
11
00
245 0 ▶ if ( !defined $curr_ver || !defined $new_ver ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $curr_ver and defined $new_ver) 0 0 0
Condition0%0/3
defined $curr_ver and defined $new_ver
!ll&&!rl&&r
000
MC/DC0%0/2
defined $curr_ver and defined $new_ver
defined $curr_ver defined $new_ver
Truth table0%0/3
A and B
A= defined $curr_verB= defined $new_ver
ABresult
0X0
100
111
246 0 $log->debug( ( caller 0 )[3], ': a version is unknown, so there is nothing to compare' );
247 0 return 0;
248 }
249 0 ▶ if ( $new_ver > $curr_ver ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_ver > $curr_ver) 0 0 0
250 0 $log->debug( ( caller 0 )[3], 'Newer version detected. Update config.' );
251 0 _update_server_side( $git, $matches[0], '.git-repo-admin' );
252 }
253 0 $log->debug( ( caller 0 )[3], '(): 0' );
254 0 return 0;
255 }
256  
257 # On server side we get the affected refs via the hook, and the main/master
258 # doesn't need to be one of them, if it wasn't updated.
259  
260 }
261  
262 # Install hooks
263 2 POST_MERGE \&check_affected_refs_client_side;
264 2 POST_RECEIVE \&check_affected_refs_server_side;
265  
266 2 1;
267  
268 __END__
« blib/lib/Git/MoreHooks/CheckPerl.pm ↑ Summary blib/lib/Git/MoreHooks/ScanCtags.pm »