Skip to content

Instantly share code, notes, and snippets.

@NikKovIos
Created January 20, 2026 22:44
Show Gist options
  • Select an option

  • Save NikKovIos/c854ce5ac350590398dcc73f3b77e2f3 to your computer and use it in GitHub Desktop.

Select an option

Save NikKovIos/c854ce5ac350590398dcc73f3b77e2f3 to your computer and use it in GitHub Desktop.
Swiftlint only modified files
#!/bin/bash
# Variables
START_DATE=$(date +"%s")
SWIFTLINT="${PODS_ROOT}/SwiftLint/swiftlint"
SWIFTFORMAT="${PODS_ROOT}/SwiftFormat/CommandLineTool/swiftformat"
# Check that tools exist
[[ -x "${SWIFTLINT}" ]] || { echo "Error: SwiftLint not found at ${SWIFTLINT}"; exit 1; }
[[ -x "${SWIFTFORMAT}" ]] || { echo "Error: SwiftFormat not found at ${SWIFTFORMAT}"; exit 1; }
echo "SwiftLint version: $("${SWIFTLINT}" version)"
echo "SwiftFormat version: $("${SWIFTFORMAT}" --version)"
echo "..."
# All swift files in one array with unique values
all_files=()
while IFS= read -r line; do
[[ -n "$line" ]] && all_files+=("$line")
done < <(
{
git diff --diff-filter=d --name-only # staged
git diff --cached --diff-filter=d --name-only # unstaged
git ls-files --others --exclude-standard # added
} | grep '\.swift$' | sort -u
)
# Linting
if [[ ${#all_files[@]} -gt 0 ]]; then
"${SWIFTFORMAT}" "${all_files[@]}"
"${SWIFTLINT}" --fix "${all_files[@]}"
"${SWIFTLINT}" "${all_files[@]}"
fi
# Final metrics
END_DATE=$(date +"%s")
DIFF=$((END_DATE - START_DATE))
MINUTES=$((DIFF / 60))
SECONDS=$((DIFF % 60))
echo "..."
echo "Total files found: ${#all_files[@]}"
echo "Time: ${MINUTES}m ${SECONDS}s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment