blib/lib/Git/Hooks/CheckWhitespace.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 88 CC 10 SCAR 35.8
CC
10
Cov
0%
check_ref
72.0
check_commit
6.0
check_patchset
6.0
CRAP 35.9
1 use warnings;
2  
3 package Git::Hooks::CheckWhitespace;
4 # ABSTRACT: Git::Hooks plugin for checking whitespace errors
5 $Git::Hooks::CheckWhitespace::VERSION = '3.5.0';
6 use v5.16.0;
7 use utf8;
8 use Log::Any '$log';
9 use Git::Hooks;
10  
11 my $CFG = __PACKAGE__ =~ s/.*::/githooks./r;
12  
13 # This routine can act both as an update or a pre-receive hook.
14 sub check_ref {
15 my ($git, $ref) = @_;
16  
17 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 return 0 if $new_commit eq $git->undef_commit;
21  
22 # If the reference is being created we have to calculate a proper
23 # $old_commit to diff against.
24 if ($old_commit eq $git->undef_commit) {
25 my $last_log;
26 my $log_iterator = $git->log($new_commit, qw/--not --all/);
27 while (my $log = $log_iterator->next()) {
28 $last_log = $log;
29 }
30 return 0 unless $last_log;
31 my @parents = $last_log->parent;
32 if (@parents == 0) {
33 # We reached the repository root. Hence, let's consider
34 # $old_commit to be the empty tree.
35 $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 $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 $old_commit = $last_log->commit;
45 }
46 }
47  
48 my $output = $git->run(
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 if ($? != 0) {
54 $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 return 1;
59 };
60  
61 return 0;
62 }
63  
64 sub check_commit {
65 my ($git) = @_;
66  
67 my $output = $git->run(
68 {fatal => [-129, -128], quiet => 1},
69 qw/diff-index --check --cached/, $git->get_head_or_empty_tree());
70 if ($? == 0) {
71 return 1;
72 } else {
73 $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 return 0;
78 };
79 }
80  
81 sub check_patchset {
82 my ($git, $branch, $commit) = @_;
83  
84 my $output = $git->run(
85 {fatal => [-129, -128], quiet => 1},
86 qw/diff-tree -r -m --check/, $commit->commit);
87 if ($? == 0) {
88 return 1;
89 } else {
90 $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 return 0;
95 };
96 }
97  
98 # Install hooks
99 GITHOOKS_CHECK_AFFECTED_REFS(\&check_ref);
100 GITHOOKS_CHECK_PRE_COMMIT(\&check_commit);
101 GITHOOKS_CHECK_PATCHSET(\&check_patchset);
102  
103 1;
104  
105 __END__