Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access commit message with Husky pre-commit hook?

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

like image 272
Potatoes Avatar asked Sep 10 '19 04:09

Potatoes


People also ask

How do you bypass a pre-commit hook?

Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.

What is husky pre-commit?

Husky is a very popular (1 million downloads a month) npm package that allows custom scripts to be ran against your repository. Husky works with any project that uses a package. json file. It also works out of the box with SourceTree!


1 Answers

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.

like image 108
VonC Avatar answered Sep 18 '22 11:09

VonC