Skip to content

Instantly share code, notes, and snippets.

@androiddevnotes
Created September 17, 2024 00:13
Show Gist options
  • Save androiddevnotes/3084811d7e45c88dab015196ed2154d8 to your computer and use it in GitHub Desktop.
Save androiddevnotes/3084811d7e45c88dab015196ed2154d8 to your computer and use it in GitHub Desktop.
Automatically format Swift source files using swift-format from SwiftLang before committing changes in a Git repository. Ensure you have swift-format installed
#!/bin/sh
# Format Swift files with swift-format before committing
git diff --name-only -z --cached -- '*.swift' | \
xargs -0 -I {} sh -c 'swift-format -i {} || exit $?'
# If swift-format fails (returns non-zero), the commit will be aborted
# Re-add the files to stage the changes made by swift-format
git diff --name-only -z --cached -- '*.swift' | \
xargs -0 git add
# If you want to see what changes swift-format made before committing:
# git diff --cached
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment