Skip to content

feat(cli): add --json output to status and run - #200

Merged
mrz1836 merged 2 commits into
masterfrom
feat/json-output
Nov 10, 2026
Merged

mrz1836 merged 2 commits into
masterfrom
feat/json-output

Conversation

@mrz1836

@mrz1836 mrz1836 commented Nov 10, 2026

Copy link
Copy Markdown
Owner

What Changed

  • go-pre-commit status --json prints one JSON document on stdout and nothing else: the version, whether the system is enabled, the config mode (modular/legacy) and path, the repository root, the hooks directory, and for each hook type (pre-commit, pre-push, commit-msg, post-commit) its state (ready, outdated, not_executable, other, missing, error), path, executable, and outdated.
  • go-pre-commit run --json prints one JSON document on stdout when the run ends: each check's result (passed, failed, skipped), duration_ms, files, changed_files, and its error, suggestion, and tool output when it failed; the files checked; totals; and warnings. All human output goes to stderr instead, including every tool installation message, also when a golangci-lint release download falls back to go install, so stdout holds only the JSON document. The exit status is unchanged.
  • A tool that is missing and not installed (automatic installs off, or the verified release install and its fallback failed) is a failed check in the report, with the install error as its output and the install command as its suggestion; lint is skipped instead with --graceful.
  • When either command cannot report, it prints {"version", "error": {"code", "message"}} and exits 1. Codes: config_not_found, config_invalid, not_a_repository, git_failed, no_checks_to_run, run_failed. --json cannot be combined with run --show-checks.
  • New checks.ChangeReporter: eof, whitespace, and fumpt report the files they rewrote (fumpt now runs gofumpt -l -w); the runner records them in CheckResult.ChangedFiles. changed_files is [] when nothing changed and null for checks that cannot tell.
  • config.Load records which configuration it read (Config.Source); the tool installer and its progress tracker write to a settable output (tools.SetOutput); output.NewWithWriters builds a formatter with explicit writers.
  • README section "Machine-Readable Output (--json)" and both commands' help document every field, state, and error code.

Why It Was Necessary

Scripts, editors, and CI steps can only read status and run results as human text, whose wording, symbols, and stream (stdout or stderr) vary. Whether an installed hook is outdated (a byte comparison with the script the binary would write) is only visible in status's text, and which files a fixing check rewrote was not reported at all.

Testing Performed

  • Command-level tests run the CLI in-process in temporary git repositories with an isolated HOME and git config, capture the process's real stdout and stderr, and require stdout to be exactly one JSON document:
    • every hook state (including outdated and not executable at once, and an unreadable hook), modular and legacy config, disabled system, no config, invalid config, not a repository;
    • a run with a passing check, a failing check, and fixing checks that changed files (whitespace fails, fumpt passes); a failing check with a suggestion and tool output; all passing on staged files; disabled; no files; no matching checks; --json with --show-checks;
    • tool install failures with nothing but git on PATH: automatic installs off, and a golangci-lint release install whose go install fallback also fails (the install messages reach stderr only), failed and skipped with --graceful;
    • the exact key set of every document, so a renamed or dropped field fails the build; the help text lists every field, state, and code.
  • Installer tests route the install, retry, and golangci-lint fallback messages (with the fake release server and a fake go install, and temporary GOBIN, GOPATH, PATH, and HOME) to the writer set with SetOutput, with nothing on stdout.
  • fumpt runs against a fake gofumpt on a PATH holding only git, so no test can install a tool or reach the network. Unit tests cover RunWithChanges for eof and whitespace, the runner's ChangedFiles (including panics and checks without files), progress output routing, and config source recording.
  • Manual: real gofumpt reports a reformatted file; the built binary keeps stdout to the JSON document when a golangci-lint install falls back and fails; text output of status and run is identical to the previous release apart from timings.
  • magex format:check, magex lint (0 issues), magex test, magex test:race, magex test:fuzz time=5s, magex tidy, magex deps:audit, and go-pre-commit run --all-files pass; the touched packages also pass with -count=2 -shuffle=on.

Impact / Risk

  • Breaking changes: none. The new flags are opt-in and the text output is unchanged; install messages still go to stdout without --json.
  • fumpt now passes -l to gofumpt to learn which files it rewrote; formatting is unchanged, and error output is still gofumpt's stderr.
  • Text summary vs JSON: a check skipped with --graceful is reported as skipped in JSON, while the existing text summary counts it as passed.
  • Stability: field names are documented; new fields may be added later, and existing ones keep their meaning.

Scripts, editors, and CI steps can read the results of status and run
only as human text, whose wording, symbols, and stream (stdout or
stderr) vary. Whether an installed hook is outdated is known only from
status's text, and which files a fixing check rewrote is not reported.

status --json prints one JSON document on stdout: the version, whether
the system is enabled, the config mode (modular or legacy) and path,
the repository root, the hooks directory, and for each hook type its
state (ready, outdated, not_executable, other, missing, or error),
whether it is executable, and whether it is outdated.

run --json prints one JSON document on stdout when the run ends: each
check's result (passed, failed, or skipped), duration, files, the files
it changed, and its error, suggestion, and tool output when it failed;
the files checked; the totals; and any warnings. All human output goes
to stderr instead, including every tool installation message, also
when a golangci-lint release download falls back to go install, and
the exit status is unchanged. A tool that is not installed, because
automatic installs are off or the install failed, is a failed check
(skipped with --graceful for lint) whose output is the install error.

When either command cannot report, it prints an error document,
{"version", "error": {"code", "message"}}, and exits 1. --json cannot
be combined with run --show-checks.

To report changed files, eof, whitespace, and fumpt implement a new
checks.ChangeReporter interface (fumpt now runs gofumpt -l -w), and the
runner records the files in CheckResult.ChangedFiles. config.Load
records which configuration it read, and the tool installer writes its
messages to a settable output (tools.SetOutput). The text output
without --json is unchanged. The README and both commands' help
document the fields, and tests pin the exact set of keys in each
document.
@github-actions github-actions Bot added the feature Any new significant addition label Nov 10, 2026
@github-actions github-actions Bot added the size/XL Very large change (>500 lines) label Nov 10, 2026
TestRunJSONAllPassed failed on CI: fumpt ran and tried to install
gofumpt, which CI does not have. The test's legacy configuration
disables fumpt, but the legacy .github/.env.base never overrides a
value already in the environment, and other tests in the package load
configuration that enables fumpt without restoring the environment
afterwards. It passed locally only because gofumpt was on PATH.

Every --json test now starts with no go-pre-commit configuration in
the environment (restored when it ends), so its own configuration file
is the only one it sees. Each one also runs with nothing but git on
PATH and with empty GOPATH and GOBIN, and its configuration turns
automatic installs off, so no test can use or install a tool from the
machine it runs on; tests that need gofumpt put the fake one on PATH.
TestRunJSONAllPassed sets the leaked value itself and checks that only
the configured checks run.
@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage Analysis

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

Coverage Metrics

Metric Value Grade Trend
Percentage 92.7% 🏆 A 📊
Statements 3.8K/4.1K 🏆 A First report
Quality Score 92.7/100 🏆 A 📈

Coverage Breakdown

█████████████░░ 92.7%

Resources


Generated via go-coverage • 2026-11-10 05:02:38 UTC

github-actions Bot added a commit that referenced this pull request Nov 10, 2026
🏷️ Generated with GoFortress
@mrz1836
mrz1836 merged commit fb2fbe1 into master Nov 10, 2026
28 checks passed
@github-actions
github-actions Bot deleted the feat/json-output branch November 10, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Any new significant addition size/XL Very large change (>500 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant