Skip to content

Instantly share code, notes, and snippets.

@bumaociyuan
Created June 20, 2016 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bumaociyuan/68d2bed785f0d647f58b93743193347e to your computer and use it in GitHub Desktop.
Save bumaociyuan/68d2bed785f0d647f58b93743193347e to your computer and use it in GitHub Desktop.
swiftlint 的pre-commit 脚本, 自动在commit之前格式化代码
#!/bin/bash
# 这是一个pre-commit 的脚本
# Run SwiftLint
START_DATE=$(date +"%s")
echo $START_DATE
SWIFT_LINT=/usr/local/bin/swiftlint
# Run SwiftLint for given filename
run_swiftlint() {
local filename="${1}"
if [[ "${filename##*.}" == "swift" ]]; then
# echo ${filename}
${SWIFT_LINT} autocorrect --path "${filename}"
git add -- "${filename}"
# check lint
# ${SWIFT_LINT} lint --path "${filename}"
fi
}
if [[ -e "${SWIFT_LINT}" ]]; then
echo "SwiftLint version: $(${SWIFT_LINT} version)"
# Run for unstaged
# git diff --name-only | while read filename; do run_swiftlint "${filename}"; done
# Run for staged
git diff --cached --name-only | while read filename; do run_swiftlint "${filename}"; done
else
echo "${SWIFT_LINT} is not installed."
echo "Plz install swiftlint use brew"
echo "brew install swiftlint"
exit 0
fi
END_DATE=$(date +"%s")
DIFF=$(($END_DATE - $START_DATE))
echo "SwiftLint took $(($DIFF / 60)) minutes and $(($DIFF % 60)) seconds to complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment