SwiftWhy needs no sweep over old code: a story covers only what its branch changes, so a project can start with its next branch. A hook or a CI check then keeps every branch after it honest.
On a branch with some work on it:
swiftwhy draft --write Sources # start the story, a note to fill in for each change
swiftwhy check Sources # see what is still missing as you writeThe draft has a theme for each commit, with the commit's message as its prose, so good commit messages are most of a story already. Replace each placeholder with the reason, and commit the story with the branch. Branches opened before a project adopted SwiftWhy have no story; draft one, or merge them first.
#!/bin/sh
# .git/hooks/pre-commit: check that the branch's story gives a reason for every change
export PATH="$HOME/.mint/bin:$PATH" # Git apps run hooks without your shell's PATH
if command -v swiftwhy >/dev/null 2>&1; then
exec swiftwhy check Sources
fi
echo "warning: swiftwhy is not installed, so this commit's story was not checked" >&2Save it as .git/hooks/pre-commit and chmod +x it. It reads the files as they are on disk, and
passes on the base branch itself. When it fails, write the notes it names and commit again;
git commit --no-verify skips the hook, which is useful for work in progress.
To check only before sharing work, save the same script as .git/hooks/pre-push instead.
For a team, commit the script as .githooks/pre-commit instead, and have each clone run
git config core.hooksPath .githooks once.
With pre-commit, add SwiftWhy to .pre-commit-config.yaml; it builds the
tagged version itself and checks the story on each commit:
repos:
- repo: https://github.com/acfriedman/SwiftWhy
rev: 0.1.0
hooks:
- id: swiftwhyIt reads every Swift file in the repository. To read one folder, set args: [Sources].
Add a Run Script phase, so Xcode shows each change that lacks a reason beside the code as you build:
export PATH="$HOME/.mint/bin:$PATH"
if command -v swiftwhy >/dev/null 2>&1; then
cd "$SRCROOT" && swiftwhy check . || true
else
echo "warning: swiftwhy is not installed; see https://github.com/acfriedman/SwiftWhy#get-started"
fi|| true keeps findings from failing the build; drop it to make them errors.
CI usually checks out a commit and no branch, and often without history, so name the branch and fetch enough to find where it left its base. With GitHub Actions:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- run: swiftwhy check --base origin/main --branch "$GITHUB_HEAD_REF" SourcesThe job fails when a change has no reason, and prints each finding in compiler format, which most CI logs link back to the line.
A story is easiest to write while the change is being made, by whoever is making it. When an assistant
writes the code, ask it for the story too: swiftwhy draft --write lays out every note, and
swiftwhy check names each one still missing, by the link it should be headed with. Review the reasons
as you would the code; they are the part of the change only its author can supply.