Skip to content

fix(install): exit non-zero when a hook is skipped - #198

Merged
mrz1836 merged 1 commit into
masterfrom
fix/install-exit-nonzero-when-skipped
Nov 10, 2026
Merged

mrz1836 merged 1 commit into
masterfrom
fix/install-exit-nonzero-when-skipped

Conversation

@mrz1836

@mrz1836 mrz1836 commented Nov 10, 2026

Copy link
Copy Markdown
Owner

What Changed

  • go-pre-commit install now exits non-zero unless every requested hook type is installed. When another tool's hook is already in place and --force is not given, that hook is left untouched, the existing warnings are printed (Hook already exists: pre-commit (use --force to overwrite), and No hooks were installed when nothing was installed), the remaining hook types are still installed, and the command fails with Error: hooks not installed: pre-commit (use --force to overwrite).
  • A partial install (some hook types installed, some skipped) also fails. A partial install that hits any other error already did, and run likewise 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 with no hook types given.
  • New sentinel errors ErrHooksNotInstalled and ErrNoHookTypes in internal/errors.
  • install --help and the README's "Installing Hooks" section describe how existing hooks are handled and what the exit status means.
  • New testutil.IsolateGit and testutil.NewGitRepo: tests run git in a temporary repository with a temporary HOME, no global or system git config, and no inherited GIT_* variables.

Why It Was Necessary

With another tool's hook at <hooks>/pre-commit, go-pre-commit install printed 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

  • New TestRunInstallExitStatus (cmd/go-pre-commit/install_exit_test.go) runs the CLI through run() in a temporary repository and checks the exit status, the output, and the hook files:
    • another tool's hook without --force: exits 1, the hook is left byte-for-byte, the warnings and error are printed
    • another tool's hook among others: exits 1, the other hook types are installed
    • empty --hook-type: exits 1
    • --force over another tool's hook: exits 0
    • reinstalling over go-pre-commit's own hook: exits 0
  • The first three cases failed on master with exit 0 and the exact output from the report.
  • Updated TestInstallCmd_VerboseAndExistingHook, which asserted the old exit-0 behaviour, to expect ErrHooksNotInstalled and 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, and go-pre-commit run --all-files all pass.

Impact / Risk

  • Behavior change: a script that runs go-pre-commit install in 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 --force or remove the other hook.
  • Unchanged: a fresh install, reinstalling over go-pre-commit's own hook, --force, and ENABLE_GO_PRE_COMMIT=false (still warns and exits 0).
  • Production code changes are limited to install's summary and exit status, plus two sentinel errors. The rest is tests, test helpers, and docs.

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.
@github-actions github-actions Bot added the bug-P3 Lowest rated bug, affects nearly none or low-impact label Nov 10, 2026
@github-actions github-actions Bot added the size/L Large change (201-500 lines) label Nov 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage Analysis

🟢 Overall Coverage: 91.7%
📊 Initial coverage report - no baseline available for comparison

Coverage Metrics

Metric Value Grade Trend
Percentage 91.7% 🏆 A 📊
Statements 3.4K/3.7K 🏆 A First report
Quality Score 91.7/100 🏆 A 📈

Coverage Breakdown

█████████████░░ 91.7%

Resources


Generated via go-coverage • 2026-11-10 04:27:15 UTC

github-actions Bot added a commit that referenced this pull request Nov 10, 2026
🏷️ Generated with GoFortress
@mrz1836
mrz1836 merged commit 8c7beaa into master Nov 10, 2026
28 checks passed
@github-actions
github-actions Bot deleted the fix/install-exit-nonzero-when-skipped branch November 10, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-P3 Lowest rated bug, affects nearly none or low-impact size/L Large change (201-500 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant