bashhookgit-commithuskygit-husky

Regex not working with husky commit-msg hook


.husky/commit-msg

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"


PATTERN_COMMITS='(^IMO-[0-9]+\s#time\s([0-9]+([.][0-9]+)?[wdhm])([ ][0-9]+([.][0-9]+)?[wdhm])*\s\|\s[[:alnum:]_].+|Merge|Revert|Apply)'

msg=$(cat $1 | sed '/^#.*/d')
if ! [[ "$msg" =~ $PATTERN_COMMITS ]]; then
  echo -e "\x1b[31mError commit message:\x1b[0m \x1b[33m"$msg"\x1b[0m does not follow conventional commit standard, e.g \x1b[33m 'IMO-1234 #time 1w1d1h1m | This is my commit' \x1b[33m"
  exit 1;
fi

Git commit command I'm using

git commit -m "IMO-1209 #time 1m | testing husky 11"

The Hooks should pass the regex and commit should work. BUT I got error as below

Error commit message: IMO-1209 #time 1m | testing husky 11 does not follow conventional commit standard, e.g 'IMO-1234 #time 1w1d1h1m | This is my commit'


Solution

  • Regexes in bash don't support \s. Use [[:space:]] instead.