Skip to content

Instantly share code, notes, and snippets.

@tahirmt
Forked from teameh/swiftlint.sh
Created March 2, 2023 23:20
Show Gist options
  • Save tahirmt/7f2aba3ec7c482c181d1673b64e5ddb5 to your computer and use it in GitHub Desktop.
Save tahirmt/7f2aba3ec7c482c181d1673b64e5ddb5 to your computer and use it in GitHub Desktop.
Swiftlint pre-commit script
#!/bin/bash
set -e
if [ "$CI" == true ]; then
echo "CI == true, skipping this script"
exit 0
fi
SWIFT_LINT=./Pods/SwiftLint/swiftlint
if ! [[ -e "${SWIFT_LINT}" ]]; then
echo "${SWIFT_LINT} is not installed."
exit 0
fi
echo "SwiftLint $(${SWIFT_LINT} version)"
if [[ $* == *--all* ]]; then
${SWIFT_LINT} autocorrect
${SWIFT_LINT} lint
exit 0
fi
# Always lint AppDelegate to prevent "No lintable files found at paths: ''"
# caused by a combination --use-script-input-files and --force-exclude
# See https://github.com/realm/SwiftLint/issues/2619
export SCRIPT_INPUT_FILE_0="Sources/AppDelegate.swift"
count=1
# make for .. in work with the shitty spaces in our filenames
OIFS="$IFS"
IFS=$'\n'
# Changed files not added to stage area yet
for file_path in $(git diff --diff-filter=d --name-only | grep ".swift$"); do
export SCRIPT_INPUT_FILE_$count=$file_path
count=$((count + 1))
done
# Changed files added to stage area
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
# Newly added untracked files
for file_path in $(git ls-files --others --exclude-standard | grep ".swift$"); do
export SCRIPT_INPUT_FILE_$count=$file_path
count=$((count + 1))
done
if [ "$count" -ne 0 ]; then
export SCRIPT_INPUT_FILE_COUNT=$count
$SWIFT_LINT autocorrect --use-script-input-files --force-exclude
$SWIFT_LINT lint --use-script-input-files --force-exclude
else
echo "No files to lint!"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment