Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if commit first letter is uppercase with githooks

Tags:

githooks

I have validation statement (full githooks commit-msg): #!/bin/sh

read -r message<$1

if [[ $text =~ ^[a-z] ]]
then
    printf "$warning Check commit message.\n"
    exit 1
fi

When I make commit with text: "This is my test commit", githooks responds with:

[WARNING] Check commit message.

But when I make it with something like this: "this is my test commit", I get tge same result.

What an I doing wrong?

I am using Wind10, Git version 2.20.1.windows.1 and GitBash.

like image 283
Developer Avatar asked Nov 07 '22 13:11

Developer


1 Answers

I just tested such a similar hook, but using the commit-msg, not the pre-commit one.
(Git 2.23, Windows 10, CMD session)

myrepo/.git/hooks/commit-msg

#!/bin/sh

echo "1='$1'"
cat $1
if [[ $(cat $1) =~ ^[a-z] ]]
then
    printf "$warning Check commit message.\n"
    exit 1
fi

It works as advertised: if your commit message starts with a lowercase, it will block the commit creation:

D:\git\rr>git add .

D:\git\rr>git commit -m "aaa"
aaa
 Check commit message.

The commit-msg hook receives .git/COMMIT_EDITMSG as first parameter.

With an upercase first-letter, it will work:

D:\git\rr>git commit -m "Aaa"
Aaa
[master 222cffb] Aaa
 1 file changed, 1 insertion(+)
like image 173
VonC Avatar answered Nov 14 '22 21:11

VonC