Skip to content

chore(pre-commit hook): Add clang-format check to pre-commit hook #4

@SizzleUnrlsd

Description

@SizzleUnrlsd

Description

We should enforce a consistent C/C++ code style by running clang-format as part of the Git pre-commit hook. This prevents unformatted code from being committed and reduces review noise.

Goals

  • Ensure all staged C/C++ sources are formatted according to the project’s .clang-format.
  • Fail the commit if formatting is required (or optionally auto-apply formatting and re-stage).
  • Keep the hook fast and only operate on staged files.

Proposed implementation

  • Integrate clang-format into the existing pre-commit workflow (e.g., via pre-commit framework).
  • Use .clang-format at repo root as the single source of truth.
  • Run only on relevant file extensions:
    • *.c, *.h, *.cc, *.cpp, *.cxx, *.hpp, *.hxx, *.inl
  • Operate only on staged files (avoid scanning the full repo).

Suggested behavior (pick one)

  1. Check-only (recommended for CI parity)
  • Run clang-format and fail if the output would change files.
  • Print the list of files requiring formatting and how to fix:
    • clang-format -i <files> (or pre-commit run -a)
  1. Auto-fix
  • Run clang-format -i on staged files, then re-stage modified files.
  • Commit proceeds if everything is formatted.

Acceptance criteria

  • Committing unformatted C/C++ code is blocked (or auto-fixed) by the hook.
  • Hook runs in reasonable time on typical commits (only staged files).
  • Works on macOS and Linux (and ideally Windows if supported).
  • Clear developer instructions are added to CONTRIBUTING.md (or equivalent).

Tasks

  • Add clang-format hook to .pre-commit-config.yaml (or hook scripts if not using pre-commit config).
  • Ensure .clang-format is present and documented (or add it if missing).
  • Restrict hook to staged C/C++ files only.
  • Add documentation in CONTRIBUTING.md (install + how to run manually).
  • Add a small test/CI job (optional) that runs pre-commit run --all-files to keep CI consistent.

Notes

  • Prefer using clang-format from a pinned LLVM/clang toolchain version (or document the required minimum version) to avoid formatting diffs across environments.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions