A formatter is easiest to adopt in one sweep: run it once over the codebase, review the diff, and commit it on its own, so the reordering never mixes with a change in behavior. After that, a hook or a CI check keeps new code in order.
swiftstepdown --lint Sources | wc -l # how much would move
swiftstepdown Sources # move it
git diff --statCommit the result by itself, and add its hash to .git-blame-ignore-revs so git blame skips it:
git rev-parse HEAD >> .git-blame-ignore-revs
git config blame.ignoreRevsFile .git-blame-ignore-revs#!/bin/sh
# .git/hooks/pre-commit: check the order of the Swift files this commit touches
export PATH="$HOME/.mint/bin:$PATH" # Git apps run hooks without your shell's PATH
files=$(git diff --cached --name-only --diff-filter=ACM -- '*.swift')
[ -z "$files" ] && exit 0
if command -v swiftstepdown >/dev/null 2>&1; then
exec swiftstepdown --lint $files
fi
echo "warning: swiftstepdown is not installed, so this commit's order was not checked" >&2Save it as .git/hooks/pre-commit and chmod +x it. It reads the files as they are on disk, so stage
the whole file. When it fails, run swiftstepdown on the files it names and stage them again;
git commit --no-verify skips the hook.
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 SwiftStepdown to .pre-commit-config.yaml; it builds the
tagged version itself and reorders the Swift files each commit touches:
repos:
- repo: https://github.com/acfriedman/SwiftStepdown
rev: 0.1.1
hooks:
- id: swiftstepdownAdd a Run Script phase after Compile Sources, so Xcode shows what is out of order beside the code as you build. With a local install:
export PATH="$HOME/.mint/bin:$PATH"
if command -v swiftstepdown >/dev/null 2>&1; then
swiftstepdown --lint "$SRCROOT" || true
else
echo "warning: swiftstepdown is not installed; see https://github.com/acfriedman/SwiftStepdown#get-started"
fi|| true keeps findings from failing the build; drop it to make them errors.
swiftstepdown --lint Sources fails the job when anything is out of order, and prints each finding in
compiler format, which GitHub Actions and most CI logs link back to the line.