utils/commit-msg-hook

filter: Statementstmt 93.4% 100 / 107 Branchbran 57.4% 31 / 54 Conditioncond 37.5% 15 / 40 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 68.1% 165 / 242 CC 45 SCAR 28.0
CC
45
Cov
73%
check_subject
39.3
check_beginning
14.8
main
14.4
CRAP 16.5
« utils/prepare-commit-msg-hook ↑ Summary utils/perl-hook »
1 #!/usr/bin/env perl
2  
3 # Git commit message hook to check for some of our standards.
4  
5 7 ▶use v5.26.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings FATAL => qw( recursion );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use feature "unicode_strings";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Cwd qw( abs_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use lib dirname(abs_path(__FILE__)) . "/lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use GitHook qw( on_main ticket_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 my $Version = "1.00";
19  
20 7 $|++;
21 7 binmode STDOUT, ":encoding(UTF-8)";
22  
23 #<<<
24 7 my @Blocklist = (qw(
25 adapts adapting adapted
26 adds adding added
27 allows allowing allowed
28 amends amending amended
29 bumps bumping bumped
30 calculates calculating calculated
31 changes changing changed
32 cleans cleaning cleaned
33 commits committing committed
34 corrects correcting corrected
35 creates creating created
36 darkens darkening darkened
37 disables disabling disabled
38 displays displaying displayed
39 drys drying dryed
40 ends ending ended
41 enforces enforcing enforced
42 enqueues enqueuing enqueued
43 extracts extracting extracted
44 finishes finishing finished
45 fixes fixing fixed
46 formats formatting formatted
47 goes going went
48 guards guarding guarded
49 handles handling handled
50 hides hiding hid
51 increases increasing increased
52 ignores ignoring ignored
53 implements implementing implemented
54 improves improving improved
55 keeps keeping kept
56 kills killing killed
57 makes making made
58 merges merging merged
59 moves moving moved
60 permits permitting permitted
61 prevents preventing prevented
62 pushes pushing pushed
63 rebases rebasing rebased
64 refactors refactoring refactored
65 removes removing removed
66 renames renaming renamed
67 reorders reordering reordered
68 replaces replacing replaced
69 requires requiring required
70 restores restoring restored
71 returns returning returned
72 runs running ran
73 sends sending sent
74 sets setting
75 separates separating separated
76 shows showing showed
77 skips skipping skipped
78 sorts sorting sorted
79 speeds speeding sped
80 starts starting started
81 supports supporting supported
82 takes taking took
83 tests testing tested
84 truncates truncating truncated
85 turns turning turned
86 updates updating updated
87 uses using used
88 )); # adapted from https://github.com/m1foley/fit-commit
89 #>>>
90  
91 7 my $Blocklist = join "|", @Blocklist;
92 7 $Blocklist = qr/^(?:$Blocklist)\b/i;
93 7 my $Ticket_re = ticket_re;
94 7 my @Errors;
95  
96 7 ▶sub check_line_endings ($msg) {
Subroutine check_line_endings: called CC=3 SCAR=12.5
97 7 my $n = 1;
98 7 for my $m (@$msg) {
99 18 ▶ if ($m =~ /\015\012$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m =~ /\015\012$/u) 0 18 18
100 0 $m =~ s/\015\012$//;
101 0 push @Errors, ["Line $n has windows line endings (CR LF)", $m];
102 }
103 18 $n++;
104 }
105 }
106  
107 6 ▶sub check_subject ($l, $main, $merge, $skip) {
Subroutine check_subject: called CC=17 SCAR=36.7
108 6 ▶ push @Errors, ["Line 1 is longer than 50 characters", $l]
Branch: 1 branch
BranchTrueFalseTotal
if not $skip and length $l > 50 0 6 6
Condition33%1/3
not $skip and length $l > 50
!ll&&!rl&&r
060
MC/DC0%0/2
not $skip and length $l > 50
not $skip length $l > 50
Truth table33%1/3
not(A) and B
A= not $skipB= length $l > 50
ABresult
0X0
100
111
109 if !$skip && length $l > 50;
110 6 ▶ push @Errors, ["Line 1 should not reference ticket id", $l]
Branch: 1 branch
BranchTrueFalseTotal
if not $skip and $l =~ /[-#]\w?(?:\d{3,})/u 0 6 6
Condition33%1/3
not $skip and $l =~ /[-#]\w?(?:\d{3,})/u
!ll&&!rl&&r
060
MC/DC0%0/2
not $skip and $l =~ /[-#]\w?(?:\d{3,})/u
not $skip $l =~ /[-#]\w?(?:\d{3,})/u
Truth table33%1/3
not(A) and B
A= not $skipB= $l =~ /[-#]\w?(?:\d{3,})/u
ABresult
0X0
100
111
111 if !$skip && $l =~ /[-#]\w?(?:\d{3,})/;
112 6 ▶ push @Errors,
Branch: 1 branch
BranchTrueFalseTotal
if not $main and $merge and not $l =~ /'$Ticket_re'/u 0 6 6
Condition33%2/6
not $main and $merge and not $l =~ /'$Ticket_re'/u
!ll&&!rl&&r
600
not $main and $merge
!ll&&!rl&&r
060
MC/DC0%0/3
not $main and $merge and not $l =~ /'$Ticket_re'/u
not $main $merge not $l =~ /'$Ticket_re'/u
Truth table25%1/4
not(A) and B and not(C)
A= not $mainB= $mergeC= not $l =~ /'$Ticket_re'/u
ABCresult
0XX0
10X0
1100
1111
113 ["Merge should reference ticket rather than full branch name", $l]
114 if !$main && $merge && $l !~ /'$Ticket_re'/;
115 6 ▶ my $start = substr($l, 0, 1) // "";
Condition50%1/2
substr($l, 0, 1) // ""
l!l
60
MC/DC0%0/1
substr($l, 0, 1) // ""
substr($l, 0, 1)
Truth table50%1/2
A
A= substr($l, 0, 1)
Aresult
11
00
116 6 ▶ push @Errors, ["Line 1 does not start with an expected character", $l]
Branch: 1 branch
BranchTrueFalseTotal
unless $start =~ /\w/u 0 6 6
117 if $start !~ /\w/;
118 6 ▶ push @Errors, ["Line 1 does not start with a capital letter", $l]
Branch: 1 branch
BranchTrueFalseTotal
if not $skip and $start eq lc $start 1 5 6
Condition66%2/3
not $skip and $start eq lc $start
!ll&&!rl&&r
051
MC/DC50%1/2
not $skip and $start eq lc $start
not $skip $start eq lc $start
Truth table66%2/3
not(A) and B
A= not $skipB= $start eq lc $start
ABresult
0X0
100
111
119 if !$skip && $start eq lc $start;
120 6 ▶ my $verb = $l =~ $Blocklist ? "does" : $l =~ /^\w+ing\b/ ? "might" : "";
Branch: 2 branches
BranchTrueFalseTotal
$l =~ /$Blocklist/u ? : 0 6 6
$l =~ /^\w+ing\b/u ? : 0 6 6
121 6 ▶ push @Errors, ["Line 1 $verb not use imperative present tense", $l]
Branch: 1 branch
BranchTrueFalseTotal
if not $skip and $verb 0 6 6
Condition33%1/3
not $skip and $verb
!ll&&!rl&&r
060
MC/DC0%0/2
not $skip and $verb
not $skip $verb
Truth table33%1/3
not(A) and B
A= not $skipB= $verb
ABresult
0X0
100
111
122 if !$skip && $verb;
123 6 ▶ push @Errors, ["Line 1 needs changing from the commit template", $l]
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /Capitalised, short description/u 0 6 6
124 if $l =~ /Capitalised, short description/;
125 }
126  
127 7 ▶sub check_beginning ($msg) {
Subroutine check_beginning: called CC=10 SCAR=27.0
128 7 ▶ return unless @$msg;
Branch: 1 branch
BranchTrueFalseTotal
unless @$msg 1 6 7
129  
130 6 my $main = on_main;
131 6 my $l = shift @$msg;
132 6 my $merge = $l =~ /^Merge /;
133 6 my $autosquash = $l =~ /^(?:fixup|squash|amend)! /;
134 6 ▶ my $skip = $merge || $autosquash;
Condition33%1/3
$merge || $autosquash
l!l&&r!l&&!r
006
MC/DC0%0/2
$merge || $autosquash
$merge $autosquash
Truth table33%1/3
A || B
A= $mergeB= $autosquash
ABresult
1X1
011
000
135  
136 6 check_subject($l, $main, $merge, $skip);
137  
138 6 ▶ my $l2 = $msg->[0] // "";
Condition50%1/2
$msg->[0] // ""
l!l
60
MC/DC0%0/1
$msg->[0] // ""
$msg->[0]
Truth table50%1/2
A
A= $msg->[0]
Aresult
11
00
139 6 ▶ push @Errors, ["Line 2 is not empty", $l2] if !$skip && length $l2;
Branch: 1 branch
BranchTrueFalseTotal
if not $skip and length $l2 0 6 6
Condition33%1/3
not $skip and length $l2
!ll&&!rl&&r
060
MC/DC0%0/2
not $skip and length $l2
not $skip length $l2
Truth table33%1/3
not(A) and B
A= not $skipB= length $l2
ABresult
0X0
100
111
140  
141 6 ▶ my $l3 = $msg->[1] // "";
Condition50%1/2
$msg->[1] // ""
l!l
60
MC/DC0%0/1
$msg->[1] // ""
$msg->[1]
Truth table50%1/2
A
A= $msg->[1]
Aresult
11
00
142 6 ▶ push @Errors, ["Line 3 does not reference ticket id", $l3]
Branch: 1 branch
BranchTrueFalseTotal
if not $main || $skip || $l3 =~ /^Ticket $Ticket_re$/u 0 6 6
Condition33%2/6
$main || $skip || $l3 =~ /^Ticket $Ticket_re$/u
l!l&&r!l&&!r
060
$main or $skip
l!l&&r!l&&!r
006
MC/DC0%0/3
$main || $skip || $l3 =~ /^Ticket $Ticket_re$/u
$main $skip $l3 =~ /^Ticket $Ticket_re$/u
Truth table25%1/4
A or B || C
A= $mainB= $skipC= $l3 =~ /^Ticket $Ticket_re$/u
ABCresult
1XX1
01X1
0011
0000
143 if !$main && !$skip && $l3 !~ /^Ticket $Ticket_re$/;
144 }
145  
146 7 ▶sub check_end ($msg) {
Subroutine check_end: called CC=6 SCAR=18.6
147 7 my $n = 2;
148 7 ▶ while (@$msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$msg) 12 7 19
149 12 my $m = shift @$msg;
150 12 ▶ last if $m eq "# ------------------------ >8 ------------------------";
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "# ------------------------ >8 ------------------------" 0 12 12
151 12 ▶ last if $m =~ /^# interactive rebase in progress; onto/;
Branch: 1 branch
BranchTrueFalseTotal
if $m =~ /^# interactive rebase in progress; onto/u 0 12 12
152 12 ▶ next if $m =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $m =~ /^#/u 0 12 12
153 12 ▶ push @Errors, ["Line $n is longer than 72 characters", $m]
Branch: 1 branch
BranchTrueFalseTotal
if length $m > 72 0 12 12
154 if length $m > 72;
155 12 $n++;
156 }
157 }
158  
159 7 ▶sub process_msg ($msg) {
Subroutine process_msg: called CC=2 SCAR=6.9
160 7 ▶ push @Errors, ["Empty commit message", ""] unless @$msg;
Branch: 1 branch
BranchTrueFalseTotal
unless @$msg 1 6 7
161 7 check_line_endings($msg);
162 7 chomp @$msg;
163 7 check_beginning($msg);
164 7 check_end($msg);
165 }
166  
167 7 ▶sub read_msg ($file) {
Subroutine read_msg: called CC=5 SCAR=16.3
168 7 ▶ open my $fh, "<:raw", $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "Can't open ${file}: $!" 7 0 7
169 7 ▶ my $bytes = do { local $/; <$fh> }
Condition50%1/2
do { local $/; readline $fh } // ""
l!l
70
MC/DC0%0/1
do { local $/; readline $fh } // ""
do { local $/; readline $fh }
Truth table50%1/2
A
A= do { local $/; readline $fh }
Aresult
11
00
170 // "";
171 7 ▶ close $fh or die "Can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close ${file}: $!" 7 0 7
172 7 my $text = eval {
173 7 Encode::decode("UTF-8", $bytes, Encode::FB_CROAK | Encode::LEAVE_SRC)
174 };
175 7 ▶ unless (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 1 6 7
176 1 push @Errors, ["Commit message is not valid UTF-8", ""];
177 1 $text = Encode::decode("UTF-8", $bytes, Encode::FB_DEFAULT);
178 }
179 7 split /^/m, $text
180 }
181  
182 7 ▶sub main () {
Subroutine main: called CC=8 SCAR=26.7
183 7 my ($file) = @ARGV;
184 # say STDERR "commit-msg hook: [$file]";
185  
186 7 my @msg = read_msg($file);
187 7 process_msg(\@msg);
188  
189 7 ▶ return 0 unless @Errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @Errors 4 3 7
190  
191 3 my $max = 0;
192 3 ▶ for my $e (@Errors) { $max = length $e->[0] if length $e->[0] > $max }
Branch: 1 branch
BranchTrueFalseTotal
if length $e->[0] > $max 3 0 3
193 my $report = join "", "Errors found:\n",
194 3 map { sprintf "%-${max}s: «%s»\n", @$_ } @Errors;
195 3 print $report; # captured by pre-commit, shown on failure
196  
197 3 ▶ open my $tty, "+<", "/dev/tty" or return 1; # no terminal: decline
Branch: 1 branch
BranchTrueFalseTotal
open my $tty, "+<", "/dev/tty" or return 1 0 3 3
198 0 binmode $tty, ":encoding(UTF-8)";
199 0 print $tty $report, "Force commit? [y/N] ";
200 0 ▶ my $force = <$tty> // "";
Condition0%0/2
readline $tty // ""
l!l
00
MC/DC0%0/1
readline $tty // ""
readline $tty
Truth table0%0/2
A
A= readline $tty
Aresult
11
00
201 0 ▶ close $tty or die "Can't close tty";
Branch: 1 branch
BranchTrueFalseTotal
close $tty or die "Can't close tty" 0 0 0
202 0 ▶ $force =~ /^y/i ? 0 : 1
Branch: 1 branch
BranchTrueFalseTotal
$force =~ /^y/iu ? : 0 0 0
203 }
204  
205 7 exit main
« utils/prepare-commit-msg-hook ↑ Summary utils/perl-hook »