Skip to content

Latest commit

 

History

History
90 lines (66 loc) · 3.25 KB

Setup.md

File metadata and controls

90 lines (66 loc) · 3.25 KB

Setup

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.

A first branch

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 write

The 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.

Git pre-commit hook

#!/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" >&2

Save 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.

The pre-commit framework

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: swiftwhy

It reads every Swift file in the repository. To read one folder, set args: [Sources].

Xcode build phase

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

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" Sources

The job fails when a change has no reason, and prints each finding in compiler format, which most CI logs link back to the line.

With a coding assistant

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.