husky icon indicating copy to clipboard operation
husky copied to clipboard

Please document how to use Husky with an existing prepare-commit-msg hook

Open jcollum-nutrien opened this issue 2 years ago • 5 comments

Troubleshoot

  • [x] Before creating an issue, please check: https://typicode.github.io/husky/#/?id=troubleshoot

Context ohmyzsh, Node 14

 ✗ cat node_modules/husky/package.json| grep version
  "version": "7.0.4",

I have an existing hook that appends a ticket number to all commit messages so I don't have to deal with it:

✗ cat .git/hooks/prepare-commit-msg
#!/bin/bash
COMMIT_MSG_FILE=$1

branch=`git branch --show-current`
## clipped out some branch name checking here 
regex="^(feat|hotfix|bug)\/([A-z]{2,5}-[0-9]{3,9}).*$"
original=`cat $1` # grab original message

# echo "$branch ${branch} regex ${regex} original ${original}"
if [[ "$branch" =~ $regex ]]; then
    type="${BASH_REMATCH[1]}"
    ticket="${BASH_REMATCH[2]}"
    # echo "type  ${type}, ticket: ${ticket}, original commit message: ${original}"
    printf "$type($ticket): $original" > "$COMMIT_MSG_FILE"
else
    echo "Error: Branch name ($branch) does not match pattern: $regex" >&2
    exit 1
fi

Working fine until I added Husky to the project. Now I don't get COMMIT_MSG_FILE arg as $1, so I can't do anything with it.

Why don't I just put this in Husky script? Because the team hasn't agreed to use this hook, some of them prefer to do it manually for some reason.

pre-commit husky script:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

echo "HUSKY_GIT_PARAMS $HUSKY_GIT_PARAMS 0: $0 1: $1 2: $2"
env | grep -i husky
yarn test
npx pretty-quick --staged
./.husky/check-branch-name.sh
./.husky/run-prepare-commit-msg-if-present.sh $1

run-prepare-commit-msg-if-present.sh

FILE=".git/hooks/prepare-commit-msg"
echo 'checking for prepare-commit-msg hook'
echo "\$1 $1"
if test -f "$FILE"; then
    echo 'running prepare-commit-msg'
    sh "$FILE" $1
fi

When I run it

HUSKY_GIT_PARAMS  0: .husky/pre-commit 1:  2:
husky_skip_init=1
yarn run v1.22.18
$ jest --passWithNoTests
No tests found, exiting with code 0
✨  Done in 1.19s.
🔍  Finding changed files since git revision ce11a3f.
🎯  Found 3 changed files.
✅  Everything is awesome!
checking for prepare-commit-msg hook
$1
running prepare-commit-msg
$original
type  feat, ticket: agro-769, original commit message:
.git/hooks/prepare-commit-msg: line 45: : No such file or directory

So how do I get Husky to fire off my personal prepare-commit-msg hook? HUSKY_GIT_PARAMS is empty.

jcollum-nutrien avatar May 03 '22 20:05 jcollum-nutrien

Been about 2 weeks, does anyone have advice here?

jcollum-nutrien avatar May 16 '22 22:05 jcollum-nutrien

Just tried something and it happened to work:

  1. edit .git/hooks/prepare-commit-msg in the repo that has husky

  2. find the block that says: . "$(dirname "$0")/husky.sh" -- if this is not present I really don't know what to do next, maybe run the install script for prepare-commit-msg ?

  3. add your message-altering script in front of that block

Works fine.

I added some logging:

echo "husky prepare commit message, inputs $0 $1 $2 $3"

Result:

husky prepare commit message, inputs .git/hooks/prepare-commit-msg .git/COMMIT_EDITMSG message

and then my message is altered as I suspected it would.

jcollum-nutrien avatar May 16 '22 22:05 jcollum-nutrien

Turns out that won't work. Husky is always overwriting my altered prepare-commit-msg hook when I run yarn. So I'm back to square one.

jcollum-nutrien avatar May 24 '22 18:05 jcollum-nutrien

Having used git since ~0.9, I had to work out that husky does things slightly differently from what I was used to (e.g. pre-commit); Husky's way is much better and more modern, but the docs do suck.

The important bits:

  1. Husky only supports Git 2.9
  2. Husky uses git's own native core.hooksPath config setting (see) - added in Git 2.9.
  3. That means that it doesn't copy the scripts into .git/hooks as pre-commit does.

So, to get it to work I:

  1. moved all the hooks I wanted to keep to .husky/.
  2. completely deleted .git/hooks.
  3. ensured the repo's .git/config contained → hooksPath = ./husky/.

doublethefish avatar Jun 09 '22 08:06 doublethefish

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 08 '22 09:08 stale[bot]