How to run SwiftLint autocorrect on each Git commit
SwiftLint was developed by the nice folks @realm. It's a great open-source tool for establishing and enforcing a formal coding style in Swift. It runs on the command-line, but it can be hooked into Xcode directly too. It also has an "autocorrect" feature, which sweeps through your code and automatically fixes the most trivial violations (e.g., colon positioning, double white spaces, etc.). I created a Git commit hook which does exactly this, every time a team member makes changes.
The shell script itself is simple. Only files changed within the commit will be autocorrected. To prevent inadvertent file corrections, these changes are not automatically included in the current changeset. You will have the chance to review the corrections, and make a new commit to finalize them, or amend the previous commit.
#!/bin/sh git diff --cached --name-only | grep .swift | while read filename; do /usr/local/bin/swiftlint autocorrect --path "$filename" done
Install this shell script under the
.git/hooks directory of your Git repository. Be sure to name it
pre-commit (no file extension).
This commit hook should work fine with whichever Git GUI tool you use, as well as the command-line of course.
Note that each team member has to repeat this installation individually. There is no way to automatically distribute git commit hooks.