'git commit strange message in 'sed' and 'hook' prepare-commit-msg
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
Solution 1:[1]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | torek |
