Install hooks to core.hooksPath instead of refusing#1692
Conversation
|
@j178 we switched over to prek, but this one is annoying because prek wants to force not setting |
There was a problem hiding this comment.
Pull request overview
This PR changes prek install to install hooks to the directory specified by core.hooksPath when set, instead of refusing to proceed with a "Cowardly refusing" error. This resolves issue #23 and makes prek install idempotent when used with tools like devenv that manage git hooks via core.hooksPath.
Changes:
- Refactored
has_hooks_path_set()toget_hooks_path()which returns the configured path asOption<PathBuf>instead of a boolean - Removed the "cowardly refusing" check that prevented installation when
core.hooksPathwas set - Added logic to install hooks to the configured
core.hooksPathdirectory with a write permission check
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| crates/prek/src/git.rs | Refactored has_hooks_path_set() to get_hooks_path() which returns Option<PathBuf> with UTF-8 validation |
| crates/prek/src/cli/install.rs | Removed the "cowardly refusing" check and added logic to install to core.hooksPath when set, with write permission validation |
📦 Cargo Bloat ComparisonBinary size change: +0.00% (23.9 MiB → 23.9 MiB) Expand for cargo-bloat outputHead Branch ResultsBase Branch Results |
c425bf1 to
4d7651c
Compare
4d7651c to
ad2324a
Compare
ad2324a to
239062c
Compare
239062c to
63501c3
Compare
7c00b08 to
fa5d0e8
Compare
fa5d0e8 to
5958d74
Compare
When `core.hooksPath` is set, install hooks to that directory instead of bailing. This makes `prek install` idempotent and ensures hooks end up where git will actually look for them. Relative `core.hooksPath` values are resolved against the git common dir, matching git's behavior. If the configured path is not writable, bail with a clear message using `faccess` for proper cross-platform writability checks. Closes j178#23 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5958d74 to
44175af
Compare
|
@j178 curious what you think about this solution :) |
|
Thanks! I haven’t had much time for open source lately, but I’ll get back to you soon. |
|
I don't think silently writing to a global hooks path is a good idea. This will obviously affect all repositories which may not be desired, especially since prek will return an error if a repository doesn't have a config file. It may also overwrite a pre-existing global hook. For example, in my case I have a hook script that you symlink to the hook names like Personally my ideal solution would be if I could set a global config option to run |
I understand why your workflow works the way it does, but it feels backwards that git providers a way to tell where to install git hooks and then prek just bails out when that option is set. I'd suggest that by default prek supports the option, optionally warns about where it's installing the hook and allows a knob to disable respecting the option for such cases. Would that work for you? |
|
The issue I'm having is that https://github.com/cachix/git-hooks.nix sets |
No, I don’t think prek changes any Git configuration. What makes you think that? |
My mistake, I've changed the description of what happens :) |
This doesn’t seem right either. Since prek uses the same logic as pre-commit, it should bail out too. Could you take a look at that? ❯ git init
❯ git config core.hooksPath hooks
❯ pre-commit install
[ERROR] Cowardly refusing to install hooks with `core.hooksPath` set.
hint: `git config --unset-all core.hooksPath` |
git-hooks installation sets core.hooksPath to .git/hooks which doesn't work with prek. Unset it before installing hooks. j178/prek#1692 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@domenkozar Could you take a look at #1723? It adds a |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1692 +/- ##
==========================================
- Coverage 91.57% 90.34% -1.23%
==========================================
Files 96 96
Lines 18842 18860 +18
==========================================
- Hits 17255 17040 -215
- Misses 1587 1820 +233 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
core.hooksPathis set, install hooks to that directory instead of bailing with "Cowardly refusing"prek installidempotent and ensures hooks end up where git will actually look for themMotivation
Tools like devenv set
core.hooksPathto manage git hooks. When prek is used as one of those hooks, runningprek installa second time fails because it seescore.hooksPathis already set. The current behavior also means hooks installed to.git/hookswould never run whencore.hooksPathpoints elsewhere.Instead of refusing, prek should install to the configured path (where git will actually look for hooks). This resolves #23.
Test plan
prek installwithcore.hooksPathunset works as beforeprek installwithcore.hooksPathset to a writable directory installs hooks thereprek installtwice withcore.hooksPathset succeeds (idempotent)prek installwithcore.hooksPathset to a read only directory gives a clear error🤖 Generated with Claude Code