-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit-msg
executable file
·63 lines (59 loc) · 2.17 KB
/
commit-msg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env perl
use strict;
use warnings;
use Term::ANSIColor;
# Commit message hook for Git repos.
# Andrew Benson (28-July-2021)
# Get the temporary file containing the commit message.
my $messageFileName = $ARGV[0];
# Enforce conventional commits (https://www.conventionalcommits.org) structure.
## Header line format:
my $type ;
my $scope ;
my $breaking;
{
my $headerRegExp = qr/^([a-zA-Z0-9]+)(\([a-zA-Z0-9]+\))?(!?):\s\S/;
open(my $messageFile,$messageFileName);
my $firstLine = <$messageFile>;
close($messageFile);
if ( $firstLine =~ m/$headerRegExp/ ) {
print color('bold green')."✔ ".color('reset')."Commit message does follow Conventional Commits format (https://www.conventionalcommits.org)\n";
} else {
print color('bold red' )."✕ ".color('reset')."Commit message does not follow Conventional Commits format (https://www.conventionalcommits.org)\n";
exit 1;
}
$type = $1;
$scope = $2;
$breaking = $3;
}
## Type is valid.
{my @validTypes = ( "fix", "feat", "build", "docs", "style", "test", "refactor", "perf", "clean" );
if ( grep {$_ eq $type} @validTypes ) {
print color('bold green')."✔ ".color('reset')."Type is valid (https://www.conventionalcommits.org)\n";
} else {
print color('bold red' )."✕ ".color('reset')."Type is invalid (https://www.conventionalcommits.org)\n";
exit 1;
}
}
## BREAKING CHANGE included.
{
my $breakingChangeFound = 0;
open(my $messageFile,$messageFileName);
while ( my $line = <$messageFile> ) {
if ( $line =~ m/^BREAKING\sCHANGE:\s/ ) {
$breakingChangeFound = 1;
last;
}
}
close($messageFile);
if ( $breaking eq "!" && ! $breakingChangeFound ) {
print color('bold red' )."✕ ".color('reset')."'BREAKING CHANGE' footer missing (https://www.conventionalcommits.org)\n";
exit 1;
} elsif ( $breakingChangeFound && $breaking eq "" ) {
print color('bold red' )."✕ ".color('reset')."'BREAKING CHANGE' footer present but '!' missing from header (https://www.conventionalcommits.org)\n";
exit 1;
} else {
print color('bold green')."✔ ".color('reset')."Consistent breaking change status (https://www.conventionalcommits.org)\n";
}
}
exit 0;