Skip to content

Instantly share code, notes, and snippets.

@byaruhaf
Forked from samwize/pre-commit
Created April 22, 2022 11:09
Show Gist options
  • Select an option

  • Save byaruhaf/3f994e857eb932da24956380b7b8a3f2 to your computer and use it in GitHub Desktop.

Select an option

Save byaruhaf/3f994e857eb932da24956380b7b8a3f2 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
count=0
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
# Export files for swiftlint --use-script-input-files
for file_path in $(git diff --name-only --cached | grep ".swift$"); do
export SCRIPT_INPUT_FILE_$count=$file_path
count=$((count + 1))
done
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=$? # swiftlint 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment