aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-17 14:21:40 +0200
committeruser <user@clank>2026-08-17 14:21:40 +0200
commit1f52c39ab0875162be013e6d78720a780df1d889 (patch)
tree29e9d72aa85a39d5aec52fafcecd80deb3bdf2a7
parentsl0ppty: a dead pane says what it ran (diff)
downloadslosh-1f52c39ab0875162be013e6d78720a780df1d889.tar.gz
slosh-1f52c39ab0875162be013e6d78720a780df1d889.tar.xz
slosh-1f52c39ab0875162be013e6d78720a780df1d889.zip
sl0ppty: clang-format, and a hook that runs it on what you staged
`make hooks` points `core.hooksPath` at `.githooks`, and from then on committing C formats it first, so what lands in git is already formatted. `make fmt` does the whole tree, `make fmt-check` only reports. The reformat itself is the next commit. **The config was chosen by measuring, not by taste.** LLVM is the base because it is what this code was already written in by hand -- two spaces, 80 columns, `char *p`, braces attached -- but LLVM alone rewrote 38% of the lines. Three disagreements account for most of that, and each is a real house idiom rather than an accident: - `if (!p) return false;` on one line, which is the guard idiom throughout. LLVM splits every one of them in two, two thirds of the churn on its own. `WithoutElse`, so an `if` that has an `else` still opens up: a one-line `if` hiding an `else` is the arrangement that hides a bug. - Comments are not reflowed. They are prose, wrapped by hand, and the breaks sometimes carry meaning -- a paragraph, a list, a diagram of a pane. A comment that runs long is a thing for a person to fix, not for a formatter to even out. - Include order is left alone. It is grouped by hand here and is not alphabetical within a group; sorting it would be churn at best and, at worst, a build that depends on an order nobody wrote down. Those three, plus one-line `case` labels and loops, bring the reformat from 38% to 17% -- and what is left is alignment and the hand-packed tables, which is the part a formatter is *for*. The vendored terminal core is excluded everywhere: `.githooks/pre-commit`, `make fmt` and `make fmt-check` all filter `^vendor/`. It is pinned by commit (D12) and reformatting it would turn every future re-vendor into a merge. **The hook refuses a partly staged file rather than widening the commit.** `clang-format -i` rewrites the working tree, so re-staging afterwards would commit work somebody deliberately left out with `git add -p`. It says which files and what to do instead. It also says when it formatted something, because a hook that edits your commit silently is a hook people learn to distrust, and it fails with the package name when `clang-format` is missing rather than letting unformatted C through the one check meant to stop it. `--no-verify` still escapes. Verified in a scratch clone, all four paths: a badly formatted file is committed formatted; a partly staged one is refused with `HEAD` unchanged and the index untouched; a commit with no C in it draws no comment; and `--no-verify` puts unformatted code in, which is what asking for it means.
-rw-r--r--.clang-format37
-rwxr-xr-x.githooks/pre-commit51
-rw-r--r--Makefile24
-rw-r--r--docs/design.md20
4 files changed, 131 insertions, 1 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..c44e758
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,37 @@
+# How the C in this tree is formatted. `make fmt` applies it, `make fmt-check`
+# asks, and the pre-commit hook in `.githooks/` does it for you (`make hooks`).
+#
+# LLVM as the base because it is what this code was already written in by hand:
+# two-space indent, 80 columns, `char *p`, braces attached. Every setting below
+# is one place the hand style and the default disagreed, chosen by measuring the
+# reformat rather than by taste -- LLVM alone rewrote 38% of the lines, and these
+# bring it to 17%, all of it alignment and table packing.
+---
+Language: Cpp
+BasedOnStyle: LLVM
+
+# `if (!p) return false;` is everywhere in this code and is the house idiom for a
+# guard. LLVM splits every one of them onto two lines, which was two thirds of
+# the churn on its own. `WithoutElse`, so an `if` with an `else` still opens up:
+# a one-line `if` with a hidden `else` is the arrangement that hides a bug.
+AllowShortIfStatementsOnASingleLine: WithoutElse
+AllowShortLoopsOnASingleLine: true
+# `case ACT_ZOOM: app_toggle_zoom(a, 0); return true;` -- an action table reads
+# as a table when its rows are rows.
+AllowShortCaseLabelsOnASingleLine: true
+
+# Comments here are prose, wrapped by hand, and the line breaks are sometimes
+# deliberate -- a paragraph break, a list, an ASCII diagram of a pane. Reflowing
+# them is noise in every diff and destroys the shaped ones. A comment that is too
+# long is a thing for a person to fix, not a formatter.
+ReflowComments: false
+
+# Include order in this tree is grouped by hand (`_GNU_SOURCE`-sensitive system
+# headers, then ours) and is not alphabetical within a group. Sorting it would be
+# churn at best, and at worst a build that depends on an order nobody wrote down.
+SortIncludes: Never
+
+# A signature that does not fit is wrapped at its arguments, not by putting the
+# return type on a line of its own. The latter is an LLVM habit that reads, in a
+# file of one-line static helpers, as a different kind of declaration.
+PenaltyReturnTypeOnItsOwnLine: 1000
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
new file mode 100755
index 0000000..87a7eb8
--- /dev/null
+++ b/.githooks/pre-commit
@@ -0,0 +1,51 @@
+#!/bin/sh
+# Format the C being committed, so what lands in git is already formatted.
+#
+# Install with `make hooks`, which points `core.hooksPath` here. Hooks are not
+# versioned by git, so a hook in a repo has to be a file somebody opts into --
+# there is no way to ship one that runs on clone, and a repo that could would be
+# a repo that runs code on clone.
+#
+# Skip a commit with `git commit --no-verify` when you mean to.
+set -e
+
+# Only what is staged, only C, and never the vendored tree: libghostty-vt is
+# somebody else's code, pinned by commit (D12), and reformatting it would turn
+# every future re-vendor into a merge.
+files=$(git diff --cached --name-only --diff-filter=ACMR \
+ | grep -E '\.[ch]$' | grep -v '^vendor/' || true)
+[ -n "$files" ] || exit 0
+
+if ! command -v clang-format >/dev/null 2>&1; then
+ echo "pre-commit: clang-format is not installed, and this commit touches C." >&2
+ echo " install it (apt install clang-format / brew install clang-format)," >&2
+ echo " or commit with --no-verify if you know what you are doing." >&2
+ exit 1
+fi
+
+# A file with unstaged changes is the one case where formatting in place is
+# dangerous: `clang-format -i` rewrites the *working tree*, and re-staging it
+# would commit work that was deliberately left out of the index. So say so and
+# stop, rather than quietly widening the commit somebody carefully built with
+# `git add -p`.
+partial=""
+for f in $files; do
+ if ! git diff --quiet -- "$f"; then partial="$partial $f"; fi
+done
+if [ -n "$partial" ]; then
+ echo "pre-commit: these files are only partly staged, so formatting them" >&2
+ echo " in place would add work you left out of the commit:" >&2
+ for f in $partial; do echo " $f" >&2; done
+ echo " stage them fully, stash the rest, or use --no-verify." >&2
+ exit 1
+fi
+
+clang-format -i $files
+git add -- $files
+
+# Nothing is left to check: the files are formatted and staged. Said out loud
+# because a hook that changes your commit and does not mention it is a hook
+# people learn to distrust.
+changed=$(git diff --cached --name-only -- $files | tr '\n' ' ')
+[ -n "$changed" ] && echo "pre-commit: formatted$(printf '%s' " $changed" | sed 's/ *$//')"
+exit 0
diff --git a/Makefile b/Makefile
index 50e2fda..5e4544b 100644
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,8 @@ else
say = @:
endif
-.PHONY: all clean vendor run test retest test-live test-all smoke help coverage docs
+.PHONY: all clean vendor run test retest test-live test-all smoke help coverage \
+ docs fmt fmt-check hooks
.DEFAULT_GOAL := help
help: ## show this
@@ -243,6 +244,27 @@ coverage: $(COV_DIR)/sl0ppty $(COV_UNITS) ## how much of the code the tests run
done
@contrib/coverage
+# The C in this tree, minus the vendored library: that one is pinned by commit
+# (D12) and reformatting it would turn every re-vendor into a merge.
+CSRC := $(shell git ls-files '*.c' '*.h' 2>/dev/null | grep -v '^vendor/')
+
+fmt: ## format the C in place (.clang-format)
+ $(Q)clang-format -i $(CSRC)
+ $(say) " [FMT] $(words $(CSRC)) files"
+
+fmt-check: ## ...or just say which files are not formatted
+ @bad=$$(for f in $(CSRC); do \
+ clang-format "$$f" | cmp -s - "$$f" || echo "$$f"; \
+ done); \
+ if [ -n "$$bad" ]; then \
+ echo "not formatted:"; echo "$$bad" | sed 's/^/ /'; \
+ echo "run: make fmt"; exit 1; \
+ else echo "all $(words $(CSRC)) files formatted"; fi
+
+hooks: ## install the pre-commit hook (formats staged C)
+ $(Q)git config core.hooksPath .githooks
+ @echo "core.hooksPath -> .githooks (git commit --no-verify to skip)"
+
clean: ## remove our build output (not the vendor lib)
rm -rf build
diff --git a/docs/design.md b/docs/design.md
index 3775fef..d24641f 100644
--- a/docs/design.md
+++ b/docs/design.md
@@ -48,3 +48,23 @@ src/kdl.c a hand-rolled KDL subset, for configs and layouts
About 15k lines of C. The vendored terminal core does VT parsing, scrollback,
selection, images and key encoding; everything above is ours.
+
+## How it is formatted
+
+`clang-format`, with the config in `.clang-format`. `make fmt` applies it,
+`make fmt-check` only reports, and `make hooks` installs a pre-commit hook that
+formats the C you staged so what lands in git is already formatted.
+
+The base is LLVM, because that is what this code was written in by hand — two
+spaces, 80 columns, `char *p`, braces attached. The settings that differ from it
+were chosen by measuring the reformat rather than by taste: LLVM alone rewrote 38%
+of the lines, mostly by splitting `if (!p) return false;` in two and reflowing
+comments that are prose. Keeping those two and leaving include order alone brings
+it to 17%, which is alignment and table packing.
+
+Comments are not reflowed. They are prose here, wrapped by hand, and sometimes the
+line breaks carry meaning — a paragraph, a list, a diagram of a pane. A comment
+that runs long is a thing for a person to fix.
+
+The vendored terminal core is never formatted: it is pinned by commit (D12), and
+reformatting it would turn every re-vendor into a merge.