Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to grep from the input COMMIT_EDITMSG file in a hook fails #1455

Open
endika-campo opened this issue Jun 7, 2024 · 0 comments
Open

Comments

@endika-campo
Copy link

Context

When trying to grep from the input COMMIT_EDITMSG ($1) file in the prepare-commit-msg hook makes it fail without any relevant trace nor error message.

  • The version of terminal is zsh 5.9 on a Mac
  • Husky version 9.0.11
  • Try to grep from the $1 input variable in the aforementioned hook. It doesn't execute the hook as it fails on that line.

You have the following code I am currently using on my prepare-commit-msg. It executes correctly until it reaches line COMMIT_TYPE_PRESENT_IN_MESSAGE=$(echo $COMMIT_MESSAGE | grep -Eo "^(${COMMIT_TYPE_LITERALS}):").

I have correctly extract the contents of the $1 file with cat, but as soon as I try to grep anything from it, the hook fails.

#!/bin/sh

COMMIT_TYPE_LITERALS='feat|fix|docs|style|refactor|test|chore|perf|test|build|ci|chore|revert'

BRANCH_NAME=$(git symbolic-ref --short HEAD)
TICKET_ID=$(echo $BRANCH_NAME | grep -Eo 'CU-.{9}')
COMMIT_TYPE=$(echo $BRANCH_NAME | grep -Eo "^(${COMMIT_TYPE_LITERALS})")
COMMIT_MESSAGE=$(cat $1)

COMMIT_TYPE_PRESENT_IN_MESSAGE=$(echo $COMMIT_MESSAGE | grep -Eo "^(${COMMIT_TYPE_LITERALS}):")

if [ $COMMIT_TYPE_PRESENT_IN_MESSAGE ];
then
    CLEAN_COMMIT_MESSAGE=$(echo $COMMIT_MESSAGE | grep -E "^(${COMMIT_TYPE_LITERALS}):" | sed 's/^.*:https://')
    sed -i.bak -e "s/.*:/$COMMIT_TYPE_PRESENT_IN_MESSAGE $TICKET_ID | /" $1
else
    sed -i.bak -e "1s~^~$COMMIT_TYPE: $TICKET_ID | ~" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant