5

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.

Developer
  • 77
  • 7
  • That does not look like the full content: you are missing at least the first line (`#!/bin/sh)`. if you had not included this line in your script, can you try again after adding this line, in a CMD session, with a simplified PATH? – VonC Sep 23 '19 at 07:37

2 Answers2

1

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(+)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Same thing is happening when I am trying with uppercase, like "Test commit". My goal was block if commit text start with lowercase and allow uppercase. But this check will drop everything whit lowercase and upercase. – Developer Sep 22 '19 at 18:34
  • @Developer The point of the answer is to illustrate it will *not* with a pre-commit hook, but only with commit-msg hook. – VonC Sep 22 '19 at 18:35
  • @Developer I have edited the answer to show it *will work* with an uppercase. – VonC Sep 22 '19 at 18:38
  • @Developer because you might using a pre-commit hook, instead of a commit-msg hook: what is your hook file name and path? – VonC Sep 22 '19 at 19:02
  • I am using commit-msg and location: .git\hooks\commit-msg – Developer Sep 22 '19 at 19:09
  • @Developer Did you try the exact content that I mention in the answer? For instance, you cannot use `$1` directly, you need to read (`cat`) the `$1` reference to access the message. In your question, I do not see how "`$text`" was initialized. – VonC Sep 22 '19 at 19:16
  • @Developer try with the code I suggest in the answer: it should work. – VonC Sep 22 '19 at 19:23
  • With your code: $ git commit -am "test message" Checking code style... 1='.git/COMMIT_EDITMSG' test message Check commit message. AND SECOUND:$ git commit -am "Test message" Checking code style... 1='.git/COMMIT_EDITMSG' Test message Check commit message. – Developer Sep 22 '19 at 19:24
  • @Developer Strange. Can you check with Git 2.23? – VonC Sep 22 '19 at 19:41
  • Same thing with 2.23 version as well. – Developer Sep 23 '19 at 05:08
  • @Developer Can you check that in a regular CMD shell session (no git bash) with a simplified PATH? (as seen in https://stackoverflow.com/a/58047336/6309) – VonC Sep 23 '19 at 06:30
  • @Developer Can you also edit your question with the full complete content of your commit-msg hook? – VonC Sep 23 '19 at 06:30
0

You shoudl try with:

^[a-z][A-Za-z0-9 -]*

You current regex checks for single letter lower case.

mslowiak
  • 1,688
  • 12
  • 25
  • I tried but this did not worked. I get same response when my message started with uppercase or lowercase. – Developer Sep 22 '19 at 18:09