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

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

Open
zaunermax opened this issue Apr 30, 2024 · 0 comments
Open

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

zaunermax opened this issue Apr 30, 2024 · 0 comments

Comments

@zaunermax
Copy link

zaunermax commented Apr 30, 2024

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.

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