husky icon indicating copy to clipboard operation
husky copied to clipboard

I'd like to check "commit-msg" before "pre-commit"

Open generalworksinc opened this issue 7 years ago • 13 comments

Now husky hook do "pre-commit" before "commit-msg", but I'd like to check "commit-msg" before "pre-commit".

Because, in my project, "pre-commit" run "eslint --fix" all js/vue files and it takes about 5 minutes.

if commit with invalid commit-message, now,

  1. run git commit command.

  2. "pre-commit"(eslint takes 5 minutes) ※status ok.

  3. "commit-msg" . ※status ng -> fail commit and noticed invalid commit message.

  4. fix commit message.

5 run git commit command again. 6. "pre-commit"(eslint takes 5 minutes) ※status ok. 7. "commit-msg" ※status ok -> success commit.

total 10 minutes on "pre-commit".

if husky colud checked "commit-msg" before "pre-commit", eslint run one time(just 5 minutes) on example.

I think It's difficult solves on husky, becase "commit-msg" is Git hook command.

Any idea?

generalworksinc avatar Jan 10 '19 17:01 generalworksinc

Hi @GeneralworksInc,

I would try speeding up linting. Check if eslint has cache enabled and just lint staged files with https://github.com/okonet/lint-staged

Or, it's maybe a little hacky, but you can try something like:

"commit-msg": "validate-message && lint"

typicode avatar Jan 15 '19 18:01 typicode

I have the same issue. @typicode Can you explain your comment ?

ruscon avatar Dec 19 '19 12:12 ruscon

Facing same problem here. No updates or workarounds?

Pacheco95 avatar Jan 29 '22 14:01 Pacheco95

Did you find a solution? Really annoying to run tests, linting etc and then be told there's something wrong with the commit msg after

dominictobias avatar Apr 07 '22 10:04 dominictobias

Hi @generalworksinc,

I would try speeding up linting. Check if eslint has cache enabled and just lint staged files with https://github.com/okonet/lint-staged

Or, it's maybe a little hacky, but you can try something like:

"commit-msg": "validate-message && lint"

Hi @typicode. I'm not sure how "speed up your linting" is a valid response here. What about unit or any other tests a project may run?

It makes complete sense that commitmsg should run before precommit.

Can we fix this? It is not reasonable to have to wait for tests to pass before being informed the short text input you just wrote is invalid.

mateodelnorte avatar Apr 22 '22 22:04 mateodelnorte

Hi @mateodelnorte,

It's unfortunately not possible to change Git hooks order. It's defined at Git level, so pre-commit always run before commit-msg.

What I meant is that 5 minutes for linting a project is too long. In my experience it can be improved with caching and using lint-staged.

Maybe there's way to speed up tests as well, Jest has a few options like --onlyChanged.

You can skip both hooks with git commit -n ....

typicode avatar Apr 26 '22 05:04 typicode

I wonder what the historical (?) reasoning is for git to have it that way... 🤔

MattTreichelYeah avatar May 11 '22 21:05 MattTreichelYeah

closed without a workaround?

bryanprimus avatar Jul 24 '22 08:07 bryanprimus

The proposed workaround is to combine what's currently in the pre-commit hook and the commit-msg hook into a single commit-msg hook.

I don't think this works for a pre-commit hook that's set up to make minor alterations code, for example if the linter is run with automatic fixeruppers (typically a --fix command line argument), or if the pre-commit hook has been set up to run prettier. What I'm observing is that source code changes made during the commit-msg hook get left behind by the commit (staged but not committed). Have I got that right?

Unfortunately there may be no way to square this circle at the Husky level. As @typicode says

It's unfortunately not possible to change Git hooks order. It's defined at Git level, so pre-commit always run before commit-msg.

So closing it under these regrettable circumstances is not unreasonable.

Antony74 avatar Aug 09 '22 15:08 Antony74

I have the same issue.. Do we have any work around for same?

monika-pant avatar Mar 31 '23 16:03 monika-pant

same issue here, validate commit msg before any action should save most of dev times

jackykwandesign avatar Apr 07 '23 17:04 jackykwandesign

I just wrote the lint-staged script into commit-msg and it worked.

npm run lint-commit -- --edit $1 && npm run lint-staged

movahedan avatar Apr 21 '24 05:04 movahedan

It seems to me that as of now the only way to fail early on a wrong Git commit message would be to have some kind of git-commit wrapper (eventually a Git alias) that first checks the -m / --message= parameter value, which is kind of impractical...

mathieu-b avatar Aug 14 '25 13:08 mathieu-b