fix(init): use prek when installing hooks if pre-commit is missing - #2065
fix(init): use prek when installing hooks if pre-commit is missing#2065DSeaStar wants to merge 3 commits into
Conversation
cz init already treated prek as installed, but always ran `pre-commit install`, which raised FileNotFoundError when only prek was on PATH. Use the available installer, and ask when both exist. Fixes commitizen-tools#2018
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2065 +/- ##
=======================================
Coverage 98.24% 98.25%
=======================================
Files 61 61
Lines 2799 2815 +16
=======================================
+ Hits 2750 2766 +16
Misses 49 49 ☔ View full report in Codecov by Harness. |
woile
left a comment
There was a problem hiding this comment.
Thanks for the PR, one comment only
| ask when both are on PATH. | ||
| """ | ||
| installers = project_info.available_hook_installers() | ||
| if not installers: |
There was a problem hiding this comment.
What if the user doesn't have any installed and doesn't want to use any of these? Like myself.
Maybe if nothing is installed, it should prompt:
No pre-commit hook detected, skipping question
so users who want this know how to act accordingly (install pre-commit and retry).
What do you think?
There was a problem hiding this comment.
Agreed — if neither pre-commit nor prek is installed we skip the question now and print that message.
If neither pre-commit nor prek is on PATH, skip the hook-type question instead of failing init. Users who want hooks can install a tool and retry.
The InitFailedError branch in _ask_hook_installer was flagged by codecov as the only uncovered line of the PR. Add a test where the installer is available during the hook-type question but disappears before the install step, so the guard is exercised instead of removed.
|
Addressed in c9ea08e: when neither I also noticed codecov flagged one uncovered line — the Could you take another look when you get a chance? Thanks! |
Description
cz initalready treatedprekas a valid hook installer (is_pre_commit_installed()), but the install step always ranpre-commit install. When onlyprekis on PATH, that raisesFileNotFoundError.This change:
pre-commit,prek)<installer> install --hook-type ...Fixes #2018
Checklist
Was generative AI tooling used to co-author this PR?
Generated-by: Cursor Grok following the guidelines
Code Changes
uv run poe alllocally to ensure this change passes linter check and testsManual testing: unit tests cover only-prek, only-pre-commit, both (prompt), and neither (InitFailedError).
ruff checkandmypypass on the touched files. I did not run the full interactivecz initUI in a throwaway repo.Documentation Changes
uv run poe doclocally to ensure the documentation pages renders correctlyDocs change is a one-line note in
docs/commands/init.md. I did not rebuild the full mkdocs site.Expected Behavior
prekinstalled:cz initrunsprek installpre-commitinstalled: same as today (pre-commit install)InitFailedErrormentioning both toolsSteps to Test This Pull Request
prekonly (nopre-commiton PATH)cz initand select a hook typeprekand noFileNotFoundErrorpre-commit, then with both (should prompt)Additional Context
Owner-confirmed in #2018 (
issue-status: wait-for-implementation). Desired behavior is from the issue: treat the two tools as interchangeable, auto-select when only one is present, ask when both are.