-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lefthook causes git commit to hang indefinitely when internet connection is unavailable #610
Comments
Hey! Thank you for this issue. I'd love to understand the reason of this problem too. Could you please show the output when you run LEFTHOOK_VERBOSE=true git commit Or even without git lefthook run pre-commit -v |
Hey @mrexox, sure thing. Thanks for taking a look.
This unfortunately seems to hang before producing any output: It's as if lefthook isn't even being invoked, which makes me wonder if it's some git config issue on my side. What's odd is that adding
This does run lefthook as normal when wifi is off, with normal expected output. So it's some kind of interaction with git for sure. |
Thank you. That's confusing. Could you show the content of the |
For sure, sorry about the delay. Here's that: $ cat .git/hooks/pre-commit #!/bin/sh
if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi
call_lefthook()
{
dir="$(git rev-parse --show-toplevel)"
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')
if lefthook -h >/dev/null 2>&1
then
lefthook "$@"
elif test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif command -v npx >/dev/null 2>&1
then
npx @evilmartians/lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1
then
swift package --disable-sandbox plugin lefthook "$@"
else
echo "Can't find lefthook in PATH"
fi
}
call_lefthook run "pre-commit" "$@" Here's my global $ cat ~/.gitconfig # This is Git's per-user configuration file.
[user]
name = Evan Purcer
email = ep@e2.gg
[init]
defaultBranch = main
[core]
pager = delta
[interactive]
diffFilter = delta --color-only
[add.interactive]
useBuiltin = false # required for git 2.37.0
[delta]
navigate = true # use n and N to move between diff sections
light = false # set to true if you're in a terminal w/ a light background color (e.g. the default macOS terminal)
side-by-side = true
line-numbers = false
[merge]
conflictstyle = diff3
[diff]
colorMoved = default |
Your pointing out elif command -v npx >/dev/null 2>&1
then
npx @evilmartians/lefthook "$@" If I replace that line with this: elif command -v npx >/dev/null 2>&1
then
# npx @evilmartians/lefthook "$@"
./frontend/node_modules/.bin/lefthook "$@" then everything works as expected – awkward path required though, since I've installed lefthook locally for this project via npm, not globally. What's the recommended setup for this sort of case? A bit more context, I installed lefthook with npm install --save-dev and after adding npm run install-githooks |
I guess two clear solutions are jumping out at me: --- elif test -f "$dir/node_modules/lefthook/bin/index.js"
+++ elif test -f "$dir/frontend/node_modules/lefthook/bin/index.js"
then
--- "$dir/node_modules/lefthook/bin/index.js" "$@"
+++ "$dir/frontend/node_modules/lefthook/bin/index.js" "$@" Is there any way to accomplish B automatically when I'd like to avoid installing globally if possible – it'd be great if |
Oh, I see, you have node_modules not in the repo root. This is a known issue but there's no general solution for it yet. Yes, the fallback to npx is bad and the only recommendation is to have lefthook installed globally. Anyway thank you for creating this issue I will try to figure this out and provide some fix. The main complication is that The fix will probably require some extra config option. I haven't thought out this yet. If you have an idea, please share it with me. |
Hey thanks for the explanation, makes total sense. I'm open to anything here and I know it's a significant ask to add config options to something with (wonderfully) minimal surface area. With admittedly not a ton of broader context outside the npm use case - maybe adding an # lefthook.yml
npm-root: "frontend/"
pre-commit:
parallel: true
commands:
lint-format:
root: "frontend/"
... Alternately (or maybe both?) an // package.json
"scripts": {
"install-githooks": "lefthook install --npm-root frontend/"
... which would have the same effect the config above. For now, I can use this // package.json
"scripts": {
"install-githooks": "lefthook install && perl -pi -e 's/node_modules/frontend\\/node_modules/g' ../.git/hooks/pre-commit"
...
// note - using perl because sed doesn't behave well on osx vs linux (by the way, I'd be happy to contribute a PR with any of the above – but understand if it's better this be handled by maintainers) |
@mrexox Sorry about my huge delay in taking a look at this. I can confirm that on v1.5.5 with wifi off I see the hanging behavior, and after upgrading to v1.6.7 lefthook behaves exactly as desired. This is without any other config changes which is an unexpected bonus. Appreciate your work here! |
🔧 Summary
Lefthook causes git commit to hang indefinitely when internet connection is unavailable. Adding
LEFTHOOK=0
to the start of the git commit command makes the command succeed instantly.Want to add – really love the tool, its design/simplicity are amazing compared to alternatives I've dealt with. I'd love to understand what is causing this particular issue and if some small configuration change can fix it OR if this is due to some environment-specific issue on my side. I work from low-connectivity settings often and being blocked from committing changes normally (with linting/formatting) at these times is very inconvenient.
Lefthook version
Steps to reproduce
git commit -m "test"
LEFTHOOK=0 git commit -m "test"
Expected results
The git commit command succeeds, with lefthook invoked as normal, and with identical output as when internet connectivity was available. I wouldn't expect
git commit
to behave differently when internet connectivity is disabled vs enabled.Actual results
The git commit command hangs indefinitely, with no output (from lefthook or anything else).
Possible Solution
Could some remote version check be happening that I'm unaware of? I scanned the documentation and searched through all open/closed repo issues, couldn't find anything relevant to internet connectivity.
Logs / Screenshots
Device information:
The text was updated successfully, but these errors were encountered: