blib/lib/Git/Hooks/CheckWhitespace.pm

filter: Statementstmt 84.3% 43 / 51 Branchbran 60.0% 12 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 3 totaltotal 75.6% 62 / 82 CC 11 SCAR 18.2
CC
11
Cov
77%
check_ref
10.0
check_patchset
6.0
check_commit
2.0
CRAP 6.2
« blib/lib/Git/Hooks/GerritChangeId.pm ↑ Summary blib/lib/Git/Message.pm »
1 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Git::Hooks::CheckWhitespace;
4 # ABSTRACT: Git::Hooks plugin for checking whitespace errors
5 5 $Git::Hooks::CheckWhitespace::VERSION = '4.0.0';
6 5 ▶use v5.30.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Git::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
12  
13 # This routine can act both as an update or a pre-receive hook.
14 3 ▶sub check_ref {
Subroutine check_ref: called CC=9 SCAR=23.0
Pod undocumented
15 3 my ($git, $ref) = @_;
16  
17 3 my ($old_commit, $new_commit) = $git->get_affected_ref_range($ref);
18  
19 # If the reference is being deleted we have nothing to check
20 3 ▶ return 0 if $new_commit eq $git->undef_commit;
Branch: 1 branch
BranchTrueFalseTotal
if $new_commit eq $git->undef_commit 0 3 3
21  
22 # If the reference is being created we have to calculate a proper
23 # $old_commit to diff against.
24 3 ▶ if ($old_commit eq $git->undef_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_commit eq $git->undef_commit) 2 1 3
25 2 my $last_log;
26 2 my $log_iterator = $git->log($new_commit, qw/--not --all/);
27 2 ▶ while (my $log = $log_iterator->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $log_iterator->next) 2 2 4
28 2 $last_log = $log;
29 }
30 2 ▶ return 0 unless $last_log;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_log 0 2 2
31 2 my @parents = $last_log->parent;
32 2 ▶ if (@parents == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parents == 0) { } 2 0 2
elsif (@parents == 1) { } 0 0 0
33 # We reached the repository root. Hence, let's consider
34 # $old_commit to be the empty tree.
35 2 $old_commit = $git->empty_tree;
36 } elsif (@parents == 1) {
37 # We reached the first new commit and it's a normal commit. So,
38 # let's consider $old_commit to be its parent.
39 0 $old_commit = $parents[0];
40 } else {
41 # We reached the first new commit and it's a merge commit. So,
42 # let's consider $old_commit to be this commit, disregarding
43 # only the eventual conflict resolutions.
44 0 $old_commit = $last_log->commit;
45 }
46 }
47  
48 3 ▶ my $output = $git->run(
Branch: 1 branch
BranchTrueFalseTotal
$old_commit eq $git->undef_commit ? : 0 3 3
49 {fatal => [-129, -128], quiet => 1},
50 qw/diff-tree -r --check/,
51 $old_commit eq $git->undef_commit ? $git->empty_tree : $old_commit,
52 $new_commit);
53 3 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 2 3
54 1 $git->fault(<<'EOS', {ref => $ref, details => $output});
55 There are extra whitespaces in the changed files in the reference.
56 Please, remove them and amend your commit.
57 EOS
58 1 return 1;
59 };
60  
61 2 return 0;
62 }
63  
64 2 ▶sub check_commit {
Subroutine check_commit: called CC=2 SCAR=6.9
Pod undocumented
65 2 my ($git) = @_;
66  
67 2 my $output = $git->run(
68 {fatal => [-129, -128], quiet => 1},
69 qw/diff-index --check --cached/, $git->get_head_or_empty_tree());
70 2 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 1 1 2
71 1 return 1;
72 } else {
73 1 $git->fault(<<'EOS', {details => $output});
74 There are extra whitespaces in the changed files.
75 Please, remove them and amend your commit.
76 EOS
77 1 return 0;
78 };
79 }
80  
81 0 ▶sub check_patchset {
Subroutine check_patchset: not called CC=2 SCAR=17.9
Pod undocumented
82 0 my ($git, $branch, $commit) = @_;
83  
84 0 my $output = $git->run(
85 {fatal => [-129, -128], quiet => 1},
86 qw/diff-tree -r -m --check/, $commit->commit);
87 0 ▶ if ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
88 0 return 1;
89 } else {
90 0 $git->fault(<<'EOS', {commit => $commit->commit, details => $output});
91 There are extra whitespaces in the changed files.
92 Please, remove them and amend your commit.
93 EOS
94 0 return 0;
95 };
96 }
97  
98 # Install hooks
99 5 GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref);
100 5 GITHOOKS_CHECK_PRE_COMMIT(\&check_commit);
101 5 GITHOOKS_CHECK_PATCHSET(\&check_patchset);
102  
103 5 1;
104  
105 __END__
« blib/lib/Git/Hooks/GerritChangeId.pm ↑ Summary blib/lib/Git/Message.pm »