typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
31.69k stars 996 forks source link

prepare-commit-msg hook stopped working with v9 #1432

Open zaunermax opened 2 weeks ago

zaunermax commented 2 weeks ago

Context

When upgrading husky to version 9 my prepare-commit-msg hook stopped working (works fine with v8).

The upgrade was done following the docs (also tried swapping husky with husky install in the prepare script). I've also tried using the IDE as well as the terminal (which I restarted as well)

The script fails with the following msg: husky - prepare-commit-msg script failed (code 1)

here is the script: (I've also tried omitting the first line which had no effect)

#!/usr/bin/env sh

# The following script inspects the branch name and checks if it contains a Jira ticket number (i.e. ABC-123).
# If yes, the commit message will be automatically prepended with ABC-123.

BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase).
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook,
# while still allowing other githooks to run.
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then

  PREFIX_PATTERN='[A-Z]{2,5}-[0-9]{1,5}'

  [[ $BRANCH_NAME =~ $PREFIX_PATTERN ]]

  PREFIX=${BASH_REMATCH[0]}

  # Check if commit message starts with PREFIX
  PREFIX_IN_COMMIT=$(grep -c "^$PREFIX" $1)
  FIXUP_IN_COMMIT=$(grep -c "^fixup!" $1)

  # Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message
  # Also ignore fixups
  if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]] && ! [[ $FIXUP_IN_COMMIT -ge 1 ]]; then
    sed -i.bak -e "1s~^~$PREFIX ~" $1
  fi

fi

I've debugged it so far as it will go to the line with the first grep command. But there it fails. Could it be that the $1 is not injected correctly? Reverting to v8 resolved the issue.