1

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

4
  • I don't think this is related to git at all, the problem seems to be in matching the regex: if [[ ! 'ABC-123/something' =~ /([A-Z]+[-][\d]+\/\S+)/ ]]; then echo 'no match'; else echo 'match'; fi has the output no match. Specifically it seems to be backslash escaping, as if [[ ! 'ABC-123/something' =~ ^[A-Z]+-[0-9]+ ]]; then echo 'no match'; else echo 'match'; fi does output match.
    – jonrsharpe
    Commented Apr 9, 2019 at 14:53
  • in this way, it is still possible to commit UXD-12/ and this should be out. I tried with this ^[A-Z]+-[0-9][/]+ but this also is not working at all, I need to check also the slash and at least a character.
    – axel
    Commented Apr 9, 2019 at 15:07
  • 2
    If you need "also the slash and at least a character" then maybe e.g. ^[A-Z]+-[0-9]+/[a-zA-Z]? Or you'll need to work out how to handle backslashes (so you can use \d too). [/]+ would be a character class containing a single character, the forward slash, repeated one or more times, which is both an unnecessary class and not what you're describing.
    – jonrsharpe
    Commented Apr 9, 2019 at 15:08
  • I managed to rewrite the regular expression this way: [[ ABC-123/some =~ [A-Z]+-[0-9]+/[^\ ]+ ]] && echo yes || echo no (result is yes).
    – phd
    Commented Apr 9, 2019 at 15:54

1 Answer 1

1

So here is the correct answer

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

Thanks to @jonrsharpe

Not the answer you're looking for? Browse other questions tagged or ask your own question.