Created
September 17, 2024 00:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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