Repository navigation
fix(install): exit non-zero when a hook is skipped - #198
Merged
Merged
Conversation
When another tool's hook was already in place and --force was not given, install warned "Hook already exists" and "No hooks were installed" but exited 0. A script or CI step that ran install could not tell from the exit status that the hook was missing, so commits then went unchecked. install now fails unless every requested hook type is installed. The other tool's hook is still left untouched and reported, the remaining hook types are still installed, and the error names the skipped ones. A partial install fails as well, as a partial install that hits any other error already does, and the "Run 'git commit'" hints are only printed after a full install. An empty --hook-type list, which also installed nothing, is now rejected. Reinstalling over go-pre-commit's own hook and --force over another tool's hook still exit 0. Add testutil.IsolateGit and testutil.NewGitRepo so tests run git in a temporary repository with an isolated HOME and no global or system git config.
Contributor
Code Coverage Analysis🟢 Overall Coverage: 91.7% Coverage Metrics
Coverage Breakdown
ResourcesGenerated via go-coverage • 2026-11-10 04:27:15 UTC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Changed
go-pre-commit installnow exits non-zero unless every requested hook type is installed. When another tool's hook is already in place and--forceis not given, that hook is left untouched, the existing warnings are printed (Hook already exists: pre-commit (use --force to overwrite), andNo hooks were installedwhen nothing was installed), the remaining hook types are still installed, and the command fails withError: hooks not installed: pre-commit (use --force to overwrite).runlikewise fails when any one check fails. The "Run 'git commit'…" hints are now printed only after a full install; before, they were printed even when the pre-commit hook itself was the one skipped.--hook-type "", which installed nothing and exited 0, is rejected withno hook types given.ErrHooksNotInstalledandErrNoHookTypesininternal/errors.install --helpand the README's "Installing Hooks" section describe how existing hooks are handled and what the exit status means.testutil.IsolateGitandtestutil.NewGitRepo: tests run git in a temporary repository with a temporary HOME, no global or system git config, and no inheritedGIT_*variables.Why It Was Necessary
With another tool's hook at
<hooks>/pre-commit,go-pre-commit installprinted two warnings and exited 0. A script or CI step running it could not tell the hook was missing, so commits then went unchecked.Testing Performed
TestRunInstallExitStatus(cmd/go-pre-commit/install_exit_test.go) runs the CLI throughrun()in a temporary repository and checks the exit status, the output, and the hook files:--force: exits 1, the hook is left byte-for-byte, the warnings and error are printed--hook-type: exits 1--forceover another tool's hook: exits 0masterwith exit 0 and the exact output from the report.TestInstallCmd_VerboseAndExistingHook, which asserted the old exit-0 behaviour, to expectErrHooksNotInstalledand an untouched hook; added a case for an empty hook-type list.magex format:fix,magex lint(0 issues),magex test,magex test:race,magex tidy, andgo-pre-commit run --all-filesall pass.Impact / Risk
go-pre-commit installin a repository where another tool owns a requested hook now fails instead of silently continuing. That is the intended fix; such a script should pass--forceor remove the other hook.--force, andENABLE_GO_PRE_COMMIT=false(still warns and exits 0).install's summary and exit status, plus two sentinel errors. The rest is tests, test helpers, and docs.