Search code examples
bashgitshgithookspre-commit

How to fix the pre-commit-msg in my git environment?


Has someone have experienced a weird behaviour of .git/hooks/pre-commit-msg? The file has proper permission in being executed. And actually it is executed. The issue is that the commits, any commits with correct or uncorrect messages, make the execution goes into the error. Always.

The correct message should be something like these examples:

  • "ABC-123/ticket-task blah blha"
  • "ABC-234/ticket-task fixed some things"
  • "ABC-123/ticket-task added some"

where the first word after [test], or [fix], or [feat] is chosen between a set (like Added, Fixed, Upgraded..)

Working on OSX, with git, with zsh shell.

#!/bin/bash
#

commit_message=$(cat .git/COMMIT_EDITMSG)
commit_error="Error in the commit message. Prefix with 
JIRA ticket. For example UXD-1234/git-hook"


if [[ ! $commit_message =~ /([A-Z]+[-][\d]+\/\S+)/  ]]; then
  echo >&2 $commit_error
  exit 1
fi

Is there an error in this code?

Thanks in advance


Solution

  • So here is the correct answer

    ^[A-Z]+-[0-9]+/[a-zA-Z]
    

    Thanks to @jonrsharpe