-
-
Save stupergenius/5ca6e4d723b964aadc4ead8a23033032 to your computer and use it in GitHub Desktop.
Git Pre-Commit hook with SwiftLInt
This file contains 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/bash | |
if [ "$SKIP_SWIFTLINT" = "true" ]; then | |
exit 0 | |
fi | |
if which swiftlint >/dev/null; then | |
if [ "$1" = "buildphase" ]; then | |
swiftlint lint | |
elif [ "$1" = "postcommit" ]; then | |
count=0 | |
swArgs="--use-script-input-files" | |
# Find changed files | |
for file_path in "$(git ls-files -m --exclude-from=.gitignore | grep ".swift$")"; do | |
export SCRIPT_INPUT_FILE_$count="$file_path" | |
count=$((count + 1)) | |
done | |
# Find staged files | |
for file_path in "$(git diff --diff-filter=d --name-only --cached | grep ".swift$")"; do | |
export SCRIPT_INPUT_FILE_$count="$file_path" | |
count=$((count + 1)) | |
done | |
# Make the count avilable as global variable ##### | |
export SCRIPT_INPUT_FILE_COUNT=$count | |
# Lint files or exit if no files found for lintint ##### | |
if [ "$count" -ne 0 ]; then | |
echo "Linting and auto-correcting files before commit..." | |
swiftlint lint $swArgs && swiftlint autocorrect $swArgs # autocorrects before commit | |
else | |
# echo "No files to lint!" | |
exit 0 | |
fi | |
else | |
echo "Please provide a mode: buildphase or postcommit" | |
exit 1 | |
fi | |
RESULT=$? | |
if [ $RESULT -ne 0 ]; then | |
echo "" | |
echo "Violation found of the type ERROR! Must fix before commit!" | |
fi | |
exit $RESULT | |
else | |
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment