-1

When I do a commit I started to see this messages:

sed: -e expression #1, char 72: Unmatched ) or \)
sed: -e expression #1, char 72: Unmatched ) or \)
.git/hooks/prepare-commit-msg: 8: [: !=: argument expected

I don't change anything in the file '.git/hooks/prepare-commit-msg', so, I don't understand the problem. The content:

#!/bin/sh

BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') 
BRANCH_TYPE=$(echo $BRANCH_NAME | sed "s/\(\(feature\|hotfix\|bugfix\|devops\)\/[A-Z]\{2,3\}-[0-9]\)\+\)-.*/\2/")
BRANCH_ID=$(echo $BRANCH_NAME | sed "s/\(\(feature\|hotfix\|bugfix\|devops\)\/[A-Z]\{2,3\}-[0-9]\)\+\)-.*/\3/")
DESCRIPTION=$(git config branch."$BRANCH_NAME".description)

if [ $BRANCH_NAME != $BRANCH_ID ]
then
    echo $BRANCH_ID": "$(cat "$1") > "$1"
fi

if [ -n "$DESCRIPTION" ] 
then
   echo "" >> "$1"
   echo $DESCRIPTION >> "$1"
fi 

But, it still works

torek
  • 389,216
  • 48
  • 524
  • 664
  • The shell shuts of wordsplitting inside double quotes but still does most other preprocessing, say `man bash`. Add a `set -x` to your hook to see the commands it's issuing after all preprocessing. – jthill Apr 27 '22 at 17:56
  • copy/paste your shell script(s) into https://shellcheck.net and fix the problems that tool tells you about before posting here if you still have a problem afterwards. Also see [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) for why not to name all your variables all upper case. – Ed Morton Apr 27 '22 at 22:03

1 Answers1

0

This isn't really a Git issue. The problem is the sed expressions are just wrong:

s/\(\(feature\|hotfix\|bugfix\|devops\)\/[A-Z]\{2,3\}-[0-9]\)\+\)-.*/\2/"
  1 2                                1                     0   -1

The numbers on the second row show the nesting level of the parentheses. We see two opening parentheses (\( sequences since sed uses Basic REs), then one closing parenthesis, then a second closing parentheses that closes the final open parenthesis, and then a third closing parenthesis that closes ... well, there's nothing to close. So you get an error message.

The second sed expression uses a back-reference to the third parenthesized expression, but there is no third parenthesized expression, so removing the extra closing parenthesis produces a different error.

The error from line 8:

if [ $BRANCH_NAME != $BRANCH_ID ]

occurs when $BRANCH_NAME or $BRANCH_ID expands to the empty string or to a string with more than one "word" in it; to fix that, use quotes around the variable expansions. In any case you'll want to improve the regular expressions.

On the Git side, instead of using:

BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')

you should use:

BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

(which treats a detached HEAD as the name HEAD, which is probably the most appropriate for this case).

torek
  • 389,216
  • 48
  • 524
  • 664