chore: add shared hook config and improve Husky scripts#662
Conversation
📝 WalkthroughWalkthroughThese changes systematize the Git hook infrastructure by introducing a shared environment setup script, implementing conditional fallback logic for local and global tool invocation across all Husky hooks, and integrating a comprehensive pre-commit configuration with multiple validation and linting stages. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment Tip CodeRabbit can approve the review once all CodeRabbit's comments are resolved.Enable the |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
.husky/pre-push (1)
31-55: Consider extracting the shared hook logic to reduce duplication.The prek and pre-commit branches have identical logic for computing
--from-refand running the hooks. This could be extracted into a helper function, though the current explicit form is also readable.♻️ Optional refactor to reduce duplication
+run_shared_hooks() { + tool="$1" + if git rev-parse '@{u}' >/dev/null 2>&1; then + FROM=$(git merge-base HEAD '@{u}') + "$tool" run --from-ref "$FROM" --to-ref HEAD + elif git rev-parse HEAD~1 >/dev/null 2>&1; then + "$tool" run --from-ref HEAD~1 --to-ref HEAD + else + "$tool" run --all-files + fi +} + # prek: same hooks as .pre-commit-config.yaml — optional if not installed locally. # Falls back to pre-commit if prek is absent (same config file). if command -v prek >/dev/null 2>&1; then echo "pre-push: running prek on outgoing commits..." - if git rev-parse '@{u}' >/dev/null 2>&1; then - FROM=$(git merge-base HEAD '@{u}') - prek run --from-ref "$FROM" --to-ref HEAD - elif git rev-parse HEAD~1 >/dev/null 2>&1; then - prek run --from-ref HEAD~1 --to-ref HEAD - else - prek run --all-files - fi + run_shared_hooks prek elif command -v pre-commit >/dev/null 2>&1; then echo "pre-push: running pre-commit on outgoing commits..." - if git rev-parse '@{u}' >/dev/null 2>&1; then - FROM=$(git merge-base HEAD '@{u}') - pre-commit run --from-ref "$FROM" --to-ref HEAD - elif git rev-parse HEAD~1 >/dev/null 2>&1; then - pre-commit run --from-ref HEAD~1 --to-ref HEAD - else - pre-commit run --all-files - fi + run_shared_hooks pre-commit else🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.husky/pre-push around lines 31 - 55, Extract the duplicated logic that computes FROM and runs hooks into a reusable shell function (e.g., run_outgoing_commits or compute_from_and_run) and call it for both prek and pre-commit branches; the function should accept the command name ("prek run" or "pre-commit run") or the full command to execute, compute FROM using git merge-base HEAD '@{u}' fallback to HEAD~1 or --all-files, and then invoke the provided command with --from-ref "$FROM" --to-ref HEAD (or --all-files) so you remove the repeated blocks that currently call prek run and pre-commit run and reuse the single helper instead.scripts/husky-env.sh (1)
1-2: Add a shell directive for shellcheck and documentation clarity.Since this file is sourced (not executed directly), a shebang isn't strictly required. However, adding a shell directive comment helps shellcheck analyze correctly and documents the expected shell.
📝 Proposed fix
+# shellcheck shell=sh # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@scripts/husky-env.sh` around lines 1 - 2, Add a shell directive comment to help shellcheck and document the expected shell for this sourced script (husky-env.sh): insert a line such as "# shellcheck shell=bash" immediately after the existing SPDX header comments so static analysis and editors know the intended shell; you may also include an editor-modeline like "# -*- shell: bash -*-" if desired for extra clarity..husky/pre-commit (1)
17-22: Minor inconsistency in npx invocation.Line 14 uses
npx --no -- lint-stagedbut line 21 usesnpx vitest runwithout--no --. Consider making the style consistent. The--noflag prevents npx from prompting to install the package if it's not found, which provides clearer failure behavior.📝 Proposed fix
else - npx vitest run + npx --no -- vitest run fi🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.husky/pre-commit around lines 17 - 22, The pre-commit script is inconsistent: it uses `npx --no -- lint-staged` earlier but falls back to `npx vitest run` in the vitest branch; update the vitest invocation to use the same no-prompt style by replacing `npx vitest run` with `npx --no -- vitest run` (or alternatively remove `--no --` from the lint-staged invocation if you prefer prompting), ensuring you modify the branch that currently calls `npx vitest run` so both invocations follow the same `--no --` convention.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In @.husky/pre-commit:
- Around line 17-22: The pre-commit script is inconsistent: it uses `npx --no --
lint-staged` earlier but falls back to `npx vitest run` in the vitest branch;
update the vitest invocation to use the same no-prompt style by replacing `npx
vitest run` with `npx --no -- vitest run` (or alternatively remove `--no --`
from the lint-staged invocation if you prefer prompting), ensuring you modify
the branch that currently calls `npx vitest run` so both invocations follow the
same `--no --` convention.
In @.husky/pre-push:
- Around line 31-55: Extract the duplicated logic that computes FROM and runs
hooks into a reusable shell function (e.g., run_outgoing_commits or
compute_from_and_run) and call it for both prek and pre-commit branches; the
function should accept the command name ("prek run" or "pre-commit run") or the
full command to execute, compute FROM using git merge-base HEAD '@{u}' fallback
to HEAD~1 or --all-files, and then invoke the provided command with --from-ref
"$FROM" --to-ref HEAD (or --all-files) so you remove the repeated blocks that
currently call prek run and pre-commit run and reuse the single helper instead.
In `@scripts/husky-env.sh`:
- Around line 1-2: Add a shell directive comment to help shellcheck and document
the expected shell for this sourced script (husky-env.sh): insert a line such as
"# shellcheck shell=bash" immediately after the existing SPDX header comments so
static analysis and editors know the intended shell; you may also include an
editor-modeline like "# -*- shell: bash -*-" if desired for extra clarity.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 393b47e0-9f21-4bfa-aca5-5cd79c2da7ac
📒 Files selected for processing (7)
.husky/commit-msg.husky/pre-commit.husky/pre-push.pre-commit-config.yamlCONTRIBUTING.mdscripts/husky-env.shscripts/install-openshell.sh
Summary
Adds a repo-wide .pre-commit-config.yaml (shell, Docker, ruff, nemoclaw ESLint/Prettier, SPDX script, gitleaks, hygiene hooks) and documents prek as the recommended runner with pre-commit as an alternative.
Hardens Husky (shared scripts/husky-env.sh, SPDX on hook scripts, shellcheck-safe @{u} quoting) and fixes SC2206 in scripts/install-openshell.sh.
Goal: consistent checks locally and a clear path for CI later.
Changes
Type of Change
Testing
make checkpasses.npm testpasses.make docsbuilds without warnings. (for doc-only changes)Checklist
General
Code Changes
make formatapplied (TypeScript and Python).Doc Changes
update-docsagent skill to draft changes while complying with the style guide. For example, prompt your agent with "/update-docscatch up the docs for the new changes I made in this PR."How to verify
(or: prek install && prek run --all-files)
Summary by CodeRabbit