typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
32.33k stars 1.02k forks source link

Husky v6 - conflict between git hooks and husky hooks? #943

Closed flo-sch closed 3 years ago

flo-sch commented 3 years ago

Context I recently migrated from v4 to v6 and managed to get husky hooks working fine with the new config. However, I had hooks setup from other tools (in my case, gitmoji), installed directly inside .git/hooks/prepare-commit-msg

#!/bin/sh
# gitmoji as a commit hook
exec < /dev/tty
gitmoji --hook $1 $2

Before migrating

Using husky v4, I had the possibility to enforce hooks with husky:

// package.json
"husky": {
  "hooks": {
    "pre-commit": "lint-staged"
  }
},

And git hooks setup directly inside .git/hooks were running just fine as well.

After migrating

Using husky v6, hooks setup inside .git/hooks seem to be ignored for some reason (cannot figure out why myself). The content of files in there has not changed, and when I reset .git/hooks/prepare-commit-msg using gitmoji -r then gitmoji -i, I can see the file .git/hooks/prepare-commit-msg being removed then re-created.

If I install it as a husky hook inside .husky/prepare-commit-msg then it is active when running git commit.

BUT, my problem is that .husky is commited, and the hook would apply to anyone contributing in the project, where .git/hooks, which is not commited, allows for anyone to enable or disable specific hooks.

A few questions then:

PS: I have not tested the edge-case of conflicting hooks defined both inside .git/hooks AND .husky (same hook), but this is not really my case here (I would personally be fine with husky "taking over" in such case, to enforce a team's shared settings over user personal settings)

SalahAdDin commented 3 years ago

Did you review in the last comments? I think we had this bug migrating from 4 to 5 and the solution was deleting the .git/hooks folder. Did you try it?

flo-sch commented 3 years ago

I did not find anything mentioning that in the other issues, is that a thing? Unless there are very specific reasons behind it, I would rather avoid doing such, since those could be setup by husky or other tools

flo-sch commented 3 years ago

I just tested the following scenario and it did not help, prepare-commit-msg native git hook is still being ignored.

# move the current settings so they are ignored
mv .husky/prepare-commit-msg .husky/prepare-commit-msg2
mv .git/hooks .git/hooks2

# create an empty .git/hooks folder
mkdir .git/hooks

# Reinitialize husky & gitmoji
npx husky-init
gitmoji -i
# ✔ Gitmoji commit hook created successfully

But same status: .git/hooks/prepare-commit-msg is correctly created, and is executable with 0755 permissions, but is ignored when running git commit.

SalahAdDin commented 3 years ago

@SalahAdDin As far as I can remember, since Husky5 it does relate anymore on .git/hooks but on .husky folder.

flo-sch commented 3 years ago

Yes I'm aware that husky hooks should be moved to .husky in v6, and those work fine :)

My issue is that, after migrating to husky v6, hooks setup directly inside .git/hooks (folder which is not specific to husky) are ignored

flo-sch commented 3 years ago

Okay I think I understand now, running husky install by default sets core.hooksPath path to .husky directory, which applies to every git hooks, and result in hooks placed under .git/hooks being ignored.

In my case, gitmoji is not aware of that config and still try to to put those in there. I could tell husky to install inside .git/hooks:

{
  "scripts": {
    "prepare": "husky install .git/hooks"
  }
}

But then all hooks (even husky ones) are unversioned, up to every contributor to set or not set those (also unsure if that works in Windows systems)

Or I guess I could add prepare-commit-msg to .husky/gitignore to specifically unversion that one.

Still, I felt husky v4 was naturally integrating with git hooks, where v6 is taking over the native git config and a way that I find too opinionated for me 😕

flo-sch commented 3 years ago

I guess I will close that one then, since it seems like the intended behavior of husky v6

reda-alaoui commented 2 years ago

The fact that Husky changes core.hooksPath as if it was the only git hook provider in the world is stunning.