1

In my Next project I'm trying to use husky pre-commit to reformat my code before committing. Whenever I commit, the code is reformatted but it is only applied after the commit. So after this reformat I have to commit again.

This is the pre-commit file

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

npm run lint:fix

My package.json

{
  "scripts": {
    "lint": "next lint",
    "lint:fix": "next lint --fix",
},
"devDependencies": {
    "eslint": "8.10.0",
    "eslint-config-next": "12.1.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "husky": "^7.0.4"
}
}

Is there a way to make the pre-commit hook finish before the actual commit?

Kwekel
  • 23
  • 3
  • Have you tried: #!/bin/sh npm run lint:fix . "$(dirname "$0")/_/husky.sh" – cr1pto Sep 01 '22 at 06:36
  • Thanks for the suggestion, but it does not work. It still only reformats after the commit. – Kwekel Sep 01 '22 at 06:57
  • How are you running `git commit`? Specifically, are you running `git commit -a`? Some hooks (badly written ones) misbehave if you use `git commit` with `--only` or `--include` or anything that invokes one of these two. – torek Sep 01 '22 at 08:13
  • I'm using git commit -am"message" – Kwekel Sep 01 '22 at 08:43
  • It's a bit of a nuisance, I have to agree, but I think it's just how it's designed. Husky modifies the files in your sandbox, not in the index. But I actually think it's kind of a feature: if I `git add` all my changes and do `git commit`, I can see any changes that my husky-driven pre-commit hooks make by using `git diff` before trying to commit again. It lets me audit what husky did and confirm I'm OK with it, maybe even rerun my test suites before committing again. – joanis Sep 01 '22 at 12:39
  • But sometimes I do wish there was a "apply these changes and commit anyway" option. It could be a prompt letting me say y/n if I want to do that. – joanis Sep 01 '22 at 12:41
  • And actually, I replaced my husky-driven `commitlint` by the Python-based `gitlint` and that's one of the best things I like about `gitlint`: if the commit message doesn't comply, I get prompted with "gitlint: Your commit message contains violations." followed by "Continue with commit anyways (this keeps the current commit message)? [y(es)/n(no)/e(dit)]", which I really love. It's a big improvement over commitlint which just aborts with an error message. – joanis Sep 01 '22 at 13:12
  • 2
    If it works with `git add -u; git commit -m "message"`, it's a badly constructed hook. There are a lot of these in existence, which is one (of several) reasons I recommend *avoiding* the `-a` option with `git commit`. – torek Sep 02 '22 at 01:29
  • [pre-commit is for validation not for post-processing](https://stackoverflow.com/a/76189942/1725151) – Guildenstern May 11 '23 at 17:40
  • @Guildenstern While you're technically correct, you'll have a significant part of the JavaScript/TypeScripts community disagreeing with you, since it's commonplace to have husky apply automatic formatting rules and modify the commit in the pre-commit hook. At first I was shocked at that, but now that I'm used to it I've come around and think it's a good idea. – joanis May 12 '23 at 00:59

1 Answers1

-1

You should && git add -A . in the pre-commit hook The final solution like

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

npm run lint:fix  && git add -A .
egro
  • 1