Skip to content

Instantly share code, notes, and snippets.

@samwize
Forked from candostdagdeviren/pre-commit
Last active May 9, 2022 07:25
Show Gist options
  • Save samwize/b7727eef477b641b5bfd3b510f31ac59 to your computer and use it in GitHub Desktop.
Save samwize/b7727eef477b641b5bfd3b510f31ac59 to your computer and use it in GitHub Desktop.
Git Pre-Commit hook with SwiftLInt
#!/bin/bash
SWIFT_LINT=./Pods/SwiftLint/swiftlint
if [[ -e "${SWIFT_LINT}" ]]; then
# Export files in SCRIPT_INPUT_FILE_$count to lint against later
count=0
while IFS= read -r file_path; do
export SCRIPT_INPUT_FILE_$count="$file_path"
count=$((count + 1))
done < <(git diff --name-only --cached --diff-filter=d | grep ".swift$")
export SCRIPT_INPUT_FILE_COUNT=$count
if [ "$count" -eq 0 ]; then
echo "No files to lint!"
exit 0
fi
echo "Found $count lintable files! Linting now.."
$SWIFT_LINT --use-script-input-files --strict --config .swiftlint.yml
RESULT=$? # swiftline exit value is number of errors
if [ $RESULT -eq 0 ]; then
echo "🎉 Well done. No violation."
fi
exit $RESULT
else
echo "⚠️ WARNING: SwiftLint not found in $SWIFT_LINT"
echo "⚠️ You might want to edit .git/hooks/pre-commit to locate your swiftlint"
exit 0
fi
@samwize
Copy link
Author

samwize commented May 9, 2022

Exclude deleted files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment