typicode / husky

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

Changed default hook script to avoid fail on Windows installations #1402

Open Jxlle opened 2 months ago

Jxlle commented 2 months ago

Recently in Husky v9, an issue came up on Windows installations using Husky (in my case, wsl on Windows), where the default script used by the hooks fails on some Git GUI's (in my case it was GitKraken using a Windows installed Git executable) because the path variable used in the script has a slash in the other direction (\). This commit changes this base script to the following:

#!/usr/bin/env sh
SUB=${0%/*}
. "${SUB%\\*}/h"

instead of:

#!/usr/bin/env sh
. "${0%/*}/h"

This should work with both types of path separators.

For me, this change in script fixed all my issues but I'm not sure why this problem is even occurring. I saw that some of the hooks (such as pre-commit) DO use a slash in the other direction (/) within GitKraken. Using the hooks via the Git cli on my Windows machine does NOT give problems for the original script.

Note: Paths that contain both slashes might still have problems as the above script can delete two slashes instead of one (if they are different). To avoid this, an if statement can be added but I'm not sure if this is required due to not knowing how common this situation is.

SoyDiego commented 1 month ago

Recently in Husky v9, an issue came up on Windows installations using Husky (in my case, wsl on Windows), where the default script used by the hooks fails on some Git GUI's (in my case it was GitKraken using a Windows installed Git executable) because the path variable used in the script has a slash in the other direction (\). This commit changes this base script to the following:

#!/usr/bin/env sh
SUB=${0%/*}
. "${SUB%\\*}/h"

instead of:

#!/usr/bin/env sh
. "${0%/*}/h"

This should work with both types of path separators.

For me, this change in script fixed all my issues but I'm not sure why this problem is even occurring. I saw that some of the hooks (such as pre-commit) DO use a slash in the other direction (/) within GitKraken. Using the hooks via the Git cli on my Windows machine does NOT give problems for the original script.

Note: Paths that contain both slashes might still have problems as the above script can delete two slashes instead of one (if they are different). To avoid this, an if statement can be added but I'm not sure if this is required due to not knowing how common this situation is.

Hi, I was watching your PR and I don't know if I have the same problem. My project is like the documentation:

image

When I do this, only is creating a file pre-commit but I cannot see the folder _

image

I'm doing manually npm run prepare and now I can see the folder _

image

Finally I'm trying to do a commit but always I have the same error:

image

What I'm doing wrong. I'm trying in Linux, Mac and Windows and always I have the same problem or sometimes I'm receiving the message .git not found.

Any help? @Jxlle @typicode

Thanks!