diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000..9461545b --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,53 @@ +#!/bin/sh +LINT=$(which swiftlint) + +if [[ -e "${LINT}" ]]; then + echo "πŸš€ SwiftLint μ‹œμž‘..." + echo "πŸ” lint 적용 경둜: $(pwd)" + 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 +else + echo "SwiftLintκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€, κ³΅μ‹λ¬Έμ„œλ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”. 🌐 https://github.com/realm/SwiftLint" + exit 1 +fi + +##### Check for modified files in unstaged/Staged area ##### +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 + +echo "${SCRIPT_INPUT_FILE_COUNT}" + +# lint rule μ •μ˜ 파일 +RESULT=$($LINT lint --quiet --use-script-input-files --config .precommit.yml) + +if [ "$RESULT" == '' ]; then + printf "✨ SwiftLint μ μš©μ„ μ™„λ£Œν–ˆμŠ΅λ‹ˆλ‹€!! κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€πŸ‘ πŸ‘ πŸ‘\n" +else + echo "" + printf "βœ” SwiftLint Failed μ•„λž˜ λ‚΄μš©μ„ ν™•μΈν•΄μ£Όμ„Έμš”:\n" + while read -r line; do + FILEPATH=$(echo $line | cut -d : -f 1) + L=$(echo $line | cut -d : -f 2) + C=$(echo $line | cut -d : -f 3) + TYPE=$(echo $line | cut -d : -f 4 | cut -c 2-) + MESSAGE=$(echo $line | cut -d : -f 5 | cut -c 2-) + DESCRIPTION=$(echo $line | cut -d : -f 6 | cut -c 2-) + if [ $TYPE == 'warning' ]; then + printf "\n 🚧 $TYPE\n" + elif [ $TYPE == 'error' ]; then + printf "\n 🚨 $TYPE\n" + fi + printf " βœ” $FILEPATH:$L:$C\n" + printf " πŸ“Œ $MESSAGE: - $DESCRIPTION\n" + done <<< "$RESULT" + + printf "\n πŸš‘ μ»€λ°‹μ‹€νŒ¨!! Swiftlint rule에 맞게 μ½”λ“œλ₯Ό λ³€κ²½ν•΄μ£Όμ„Έμš”:)\n" + exit 1 +fi