I'd like to check "commit-msg" before "pre-commit"
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,
-
run git commit command.
-
"pre-commit"(eslint takes 5 minutes) ※status ok.
-
"commit-msg" . ※status ng -> fail commit and noticed invalid commit message.
-
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?
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"
I have the same issue. @typicode Can you explain your comment ?
Facing same problem here. No updates or workarounds?
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
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.
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 ....
I wonder what the historical (?) reasoning is for git to have it that way... 🤔
closed without a workaround?
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.
I have the same issue.. Do we have any work around for same?
same issue here, validate commit msg before any action should save most of dev times
I just wrote the lint-staged script into commit-msg and it worked.
npm run lint-commit -- --edit $1 && npm run lint-staged
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...