diff options
| author | gbc dev <gbc@localhost> | 2026-07-18 20:57:37 +0200 |
|---|---|---|
| committer | gbc dev <gbc@localhost> | 2026-07-18 20:57:37 +0200 |
| commit | 4901afc59a43841a7cd9e5aee5fbdbd5966a8171 (patch) | |
| tree | ce5fe093e2896cf583ae0fbec123b3999777f8d4 | |
| parent | README: fix source paths in Architecture table (src/ prefix) (diff) | |
| download | sl0pboy-4901afc59a43841a7cd9e5aee5fbdbd5966a8171.tar.gz sl0pboy-4901afc59a43841a7cd9e5aee5fbdbd5966a8171.tar.xz sl0pboy-4901afc59a43841a7cd9e5aee5fbdbd5966a8171.zip | |
hooks: clang-format pre-commit (changed lines only) + .clang-format
git-clang-format scopes formatting to the lines each commit touches -
a full-file reformat would destroy the deliberately dense hand
formatting (opcode tables, multi-statement helpers). The hook re-adds
formatted files (git-clang-format --staged fixes the worktree, not the
index) and refuses partially-staged C files rather than mixing staged
and unstaged changes. Enable once: git config core.hooksPath hooks
| -rw-r--r-- | .clang-format | 23 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rwxr-xr-x | hooks/pre-commit | 45 |
3 files changed, 69 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e338db1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,23 @@ +# House style for sl0pboy/gbos C: 4-space, attached braces, dense-but-readable. +# Tuned to minimize churn against the existing hand-formatted code. +BasedOnStyle: LLVM +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +ColumnLimit: 100 +PointerAlignment: Right +BreakBeforeBraces: Attach +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLoopsOnASingleLine: true +AllowShortBlocksOnASingleLine: Always +AlignTrailingComments: true +SpacesBeforeTrailingComments: 2 +AlignConsecutiveMacros: Consecutive +SortIncludes: Never +IndentCaseLabels: true +MaxEmptyLinesToKeep: 2 +KeepEmptyLinesAtTheStartOfBlocks: true +AlignArrayOfStructures: None +ReflowComments: false @@ -14,6 +14,7 @@ Resize the terminal and `SIGWINCH` triggers a full repaint. ## Build & run ```sh +git config core.hooksPath hooks # once: clang-format pre-commit hook make ./build/sl0pboy path/to/rom.gb # just play it (ROM goes last) ./build/sl0pboy --test 30 rom.gb # run 30s, serial -> stderr (test roms) diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..7b1778c --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,45 @@ +#!/bin/sh +# pre-commit: clang-format the C lines this commit touches. +# +# Scoped to *changed lines* via git-clang-format, NOT whole files: the +# codebase deliberately packs multiple statements per line (opcode tables, +# ALU helpers) and a full-file reformat would destroy that. New/edited +# lines converge on .clang-format style; untouched history stays intact. +# +# Enable once per clone: git config core.hooksPath hooks +# Bypass for one commit: git commit --no-verify + +# staged C files (added/copied/modified/renamed) +staged=$(git diff --cached --name-only --diff-filter=ACMR -- '*.c' '*.h') +[ -z "$staged" ] && exit 0 + +if ! command -v git-clang-format >/dev/null 2>&1; then + echo "pre-commit: git-clang-format not found (install clang-format)." >&2 + echo "pre-commit: bypass with: git commit --no-verify" >&2 + exit 1 +fi + +# git-clang-format --staged applies fixes to the WORKTREE, so we must re-add +# the files afterwards. That is only safe when index == worktree for every +# staged C file; refuse partially-staged files instead of mixing changes. +partial=$(git diff --name-only -- $staged) +if [ -n "$partial" ]; then + echo "pre-commit: partially staged C files (index != worktree):" >&2 + printf ' %s\n' $partial >&2 + echo "pre-commit: stage them fully (git add) or bypass with --no-verify." >&2 + exit 1 +fi + +out=$(git clang-format --staged --quiet --extensions c,h 2>&1) +rc=$? +if [ $rc -ge 2 ]; then # real error (1 = "changes made") + echo "pre-commit: git-clang-format failed:" >&2 + echo "$out" >&2 + exit 1 +fi +if [ $rc -eq 1 ]; then + git add -- $staged + echo "pre-commit: clang-formatted changed lines in:" >&2 + printf ' %s\n' $staged >&2 +fi +exit 0 |
