Skip to content

fix: prevent lefthook run from overwriting global hooks#1371

Merged
mrexox merged 2 commits intoevilmartians:masterfrom
ivy:fix/sync-hooks-core-hookspath-guard
Apr 3, 2026
Merged

fix: prevent lefthook run from overwriting global hooks#1371
mrexox merged 2 commits intoevilmartians:masterfrom
ivy:fix/sync-hooks-core-hookspath-guard

Conversation

@ivy
Copy link
Copy Markdown
Contributor

@ivy ivy commented Apr 2, 2026

Hey folks! 👋 This PR fixes a bug in lefthook that was causing it to overwrite global Git pre-commit hooks.

Context

When git config --global core.hooksPath is set, but the local repo doesn't set git config core.hooksPath, lefthook run overwrites the hook.

lefthook install doesn't have this issue (it has a guard against this).

Script to reproduce (runs in a throwaway Docker container): https://gist.github.com/ivy/36674310695d5d078a2ca9a25324113d

lefthook version

# This checkout:
$ git describe --tags
v2.1.4-3-gafac4661

# When I discovered it:
$ lefthook --version
lefthook version 2.1.4

Changes

Red-green refactor:

  • Added a failing test that reproduces the bug
  • Moved the core.hooksPath guard into a new shared method: installHooks()
  • Updated the Install and Run codepaths to use the shared method

ivy added 2 commits April 2, 2026 14:34
syncHooks bypasses the ensureHooksPathUnset guard that Install() uses,
silently overwriting global hooks when core.hooksPath is configured.
These tests pin the expected behavior so the fix can be verified.
Install() and syncHooks() both create hook files but only Install()
checked core.hooksPath first. This allowed the auto-install path
(triggered by config changes during a commit) to silently overwrite
a user's global hooks directory.

Introduce installHooks() as the single entry point for hook creation,
pairing ensureHooksPathUnset() with createHooksIfNeeded() so the guard
can never be bypassed. syncHooks() now warns and skips when core.hooksPath
is set, rather than overwriting the configured directory.
@ivy ivy requested a review from mrexox as a code owner April 2, 2026 23:37
@ivy ivy changed the title Fix/sync hooks core hookspath guard fix: prevent lefthook run from overwriting global hooks Apr 2, 2026
@mrexox
Copy link
Copy Markdown
Member

mrexox commented Apr 3, 2026

Thank you very much! The code looks good! I'll release the new version in a few days 👍

@mrexox mrexox merged commit 3503a3b into evilmartians:master Apr 3, 2026
14 checks passed
@ivy ivy deleted the fix/sync-hooks-core-hookspath-guard branch April 5, 2026 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants