9

My husky script:

  "husky": {
    "hooks": {
      "pre-commit": "sh ./tools/githooks/pre-commit.sh"
    }
  }

Let's say I am doing a git commit -m "I want that text". How can I access to my commit message within the shell script? I tried to echo $HUSKY_GIT_PARAMS and $HUSKY_GIT_STDIN within the shell script but no success

Potatoes
  • 300
  • 3
  • 13

2 Answers2

10

A pre-commit hook would not access the commit message, because the hook is triggered before the commit creation.

A commit-msg hook is the right hook for checking a commit message content.

And it is available with husky in 2019

"commit-msg": "echo $HUSKY_GIT_PARAMS"

Update 2020, as commented by galethil

HUSKY_GIT_PARAMS is removed in version 5.
Instead Git parameters should be used directly in scripts (e.g. $1)


Note, since 2019, commit c4e1ed1 (Dec. 2020, Husky v5.0.5) mentions:

Previous HUSKY_GIT_PARAMS environment variable is replaced by native params $1, $2, etc.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 4
    `HUSKY_GIT_PARAMS` is removed in version 5. Instead Git parameters should be used directly in scripts (e.g. $1) . – galethil Nov 30 '20 at 12:30
  • 2
    @galethil Thank you for this update. I have included your comment in the answer for more visibility. – VonC Nov 30 '20 at 12:33
  • I think this doesn't work in 7.x. I echo $HUSKY_GIT_PARAMS from inside my script and its empty. – jcollum May 03 '22 at 20:02
  • @jcollum I agree. I have edited the answer to include the evolution. – VonC May 03 '22 at 21:13
  • well I'm not getting git params either :/ https://github.com/typicode/husky/issues/1141 – jcollum May 04 '22 at 00:12
0

A solution that has worked for me (ubuntu:22.04)

In the .husky folder add a bash script, for example "commit-msg", to print the message like in this example:

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

commit_message="$(cat "$1")"

echo $commit_message
Miguel
  • 1
  • 1