Skip to content

chore: add gofmt check to pre-commit hook - #343

Merged
c03rad0r merged 5 commits into
mainfrom
chore/gofmt-precommit-hook
Aug 19, 2026
Merged

c03rad0r merged 5 commits into
mainfrom
chore/gofmt-precommit-hook

Conversation

@Amperstrand

Copy link
Copy Markdown
Collaborator

Extends hooks/pre-commit to also check gofmt -l on staged .go files. Prevents the 16-file gofmt cleanup situation from recurring.

Combined with the existing dependency sync check, the pre-commit hook now catches:

  1. Dependency version drift across go.mod files
  2. Unformatted Go source files

Install: git config core.hooksPath hooks

Also recommend adding gofmt to CI (test.yml):

  gofmt-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version-file: src/go.mod
      - name: gofmt check
        run: |
          cd src
          if [ -n "$(gofmt -l .)" ]; then
            echo "::error::gofmt found unformatted files"
            gofmt -l .
            exit 1
          fi

@Amperstrand
Amperstrand force-pushed the chore/gofmt-precommit-hook branch from 78cd3ef to 35e910b Compare August 17, 2026 13:49
Prevents gofmt violations from being committed. Runs alongside
the existing dependency sync check.

Install with: git config core.hooksPath hooks
@Amperstrand
Amperstrand force-pushed the chore/gofmt-precommit-hook branch from 35e910b to a911079 Compare August 18, 2026 06:43
@Amperstrand

Copy link
Copy Markdown
Collaborator Author

Cold-review item (committed ELF binary): resolved. Same blob as #341 (7de4e476, 13MB unstripped) — entered via an unrelated commit under the hook work. Branch rebuilt onto current main with only the gofmt-hook commit (a911079); binary gone, bash -n + functional gofmt-check verification re-run (flags a deliberately unformatted staged file, passes a clean one), CI green. Side effect of the rebuild: no longer stacked on #336's SSID commit — the diff is now the hook alone, so #341→#343 ordering pressure is gone.

@felixfelix-bot felixfelix-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cold review — new findings only (1 issue previously raised about ELF binary, resolved, not repeated).

1 major:

Staged deletion of a .go file will crash the hook. git diff --cached --name-only -- '.go' includes files staged for deletion. gofmt -l tries to open the non-existent file, errors out, and because set -e is active, the script exits immediately — blocking the commit. Fix: add --diff-filter=d to exclude deletions:
git diff --cached --name-only --diff-filter=d -- '
.go'

1 minor:

  • Unquoted command substitution gofmt -l $(...) — filenames with spaces would break. Use xargs or while-read loop

gofmt -l received staged-but-deleted paths (--name-only includes
them), errored under set -e, and blocked the commit. Filter with
--diff-filter=d and feed gofmt one file per line via xargs -I{}
instead of unquoted command substitution (spaces-safe, no
macOS-incompatible mapfile). Functionally verified: staged deletion
+ unformatted file flags exactly the unformatted file.

Review findings: cold review 2026-08-18 (major + minor).
@Amperstrand

Copy link
Copy Markdown
Collaborator Author

Cold-review response — major + minor fixed in 1eef16d (CI green):

  • Major (staged deletion crashes hook): confirmed by inspection, fixed. --diff-filter=d excludes deletions from the gofmt list; files are fed one-per-line via xargs -I{} (spaces-safe, and portable — no mapfile, so it works in OpenWrt's ash too). Functionally verified: staged deletion + unformatted file flags exactly the unformatted file, exit 1, no crash.

@felixfelix-bot felixfelix-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at 1eef16d: --diff-filter=d + one-file-per-line feed, verified with staged deletion, ash-portable. Resolved. Good to merge.

@c03rad0r
c03rad0r self-requested a review August 19, 2026 13:15
@c03rad0r
c03rad0r merged commit ce016c5 into main Aug 19, 2026
16 checks passed
Amperstrand pushed a commit that referenced this pull request Aug 21, 2026
Fresh gofmt pass over main after the #299/#312/#327/#342/#343/#348/
#349/#351 merge wave (the wave brought new unformatted code in its
wake — scanner.go struct alignment from #312, test files across
modules, config_schema.go).

Includes the validator.go import regrouping from the previous round
of this PR (7b124b0, originally Felix's fix): stdlib and third-party
groups separated with a blank line — that file came to main via #337
carrying the misgrouping.

Supersedes the pre-wave branch; rebuilt fresh from current main.
gofmt -l: clean across src/. Builds + tests green in touched modules
(wgm, tollgate_protocol); import-paths (117) and deps-sync (125)
contract checks pass.
felixfelix-bot pushed a commit to felixfelix-bot/tollgate-module-basic-go that referenced this pull request Sep 20, 2026
* chore: add gofmt check to pre-commit hook

Prevents gofmt violations from being committed. Runs alongside
the existing dependency sync check.

Install with: git config core.hooksPath hooks

* fix(hooks): gofmt check survives staged deletions, spaces-safe

gofmt -l received staged-but-deleted paths (--name-only includes
them), errored under set -e, and blocked the commit. Filter with
--diff-filter=d and feed gofmt one file per line via xargs -I{}
instead of unquoted command substitution (spaces-safe, no
macOS-incompatible mapfile). Functionally verified: staged deletion
+ unformatted file flags exactly the unformatted file.

Review findings: cold review 2026-08-18 (major + minor).

---------

Co-authored-by: Amperstrand <amperstrand@localhost>
Co-authored-by: amperstand <atlas@oh-my-opencode.com>
Co-authored-by: c03rad0r <1100745+c03rad0r@users.noreply.github.com>
@Amperstrand
Amperstrand deleted the chore/gofmt-precommit-hook branch October 11, 2026 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants