- 
      
- 
        Save vladimir-bebeshko/0a58f2d5cb89ab7280ee3ce2695824a6 to your computer and use it in GitHub Desktop. 
    Git Client-side pre-commit hook for SwiftLint
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| # | |
| # hook script for swiftlint. It will triggered when you make a commit. | |
| # | |
| #TODO: move this content to separate file ('lint'), add separate file for swiftformat ('format'), use them here in pre-commit | |
| GUI=0 | |
| LINT_PATH_FOR_GUI="/usr/local/bin/swiftlint" | |
| printColored() { | |
| if (( $# < 1 )); then | |
| return 0 | |
| fi | |
| if (( $GUI )); then | |
| printf "$1\n" | |
| else | |
| printf "\e[$2m$1\e[39m\n" | |
| fi | |
| } | |
| if (( $GUI )); then | |
| LINT=$LINT_PATH_FOR_GUI | |
| else | |
| LINT=$(which swiftlint) | |
| fi | |
| CURRENT_DIR=$(pwd) | |
| if [[ -e "${LINT}" ]]; then | |
| echo "SwiftLint Start..." | |
| else | |
| echo "SwiftLint does not exist, download from https://github.com/realm/SwiftLint" | |
| exit 1 | |
| fi | |
| if [[ -e "$(which zsh)" ]]; then | |
| COMMAND=$(tail -1 ~/.zsh_history | awk -F ":0;" '{print $2}') | |
| elif [[ -e "$(which bash)" ]]; then | |
| COMMAND=$(tail -1 ~/.bash_history) | |
| fi | |
| SHORT_SWITCHES=$(echo "$COMMAND" | grep -Eio "\s-\w+(\s|$)" | grep "a") | |
| LONG_SWITCHES=$(echo "$COMMAND" | grep -Eio "(--all)") | |
| if [[ $SHORT_SWITCHES == "" && $LONG_SWITCHES == "" ]]; then | |
| INCLUDE_ALL="NO" | |
| else | |
| INCLUDE_ALL="YES" | |
| fi | |
| #{ git diff --cached --name-only --diff-filter=d -z -- **/*.swift ; git diff --name-only --diff-filter=d -z -- **/*.swift } | xargs -0 -I{} -n1 swiftlint --quiet --path $CURRENT_DIR/{} | |
| RESULT=$(git diff --cached --name-only --diff-filter=d -z -- "**/*.swift" | xargs -0 -n1 -I{} $LINT --quiet --path $CURRENT_DIR/{}) | |
| if (( $GUI )) || [[ $INCLUDE_ALL == "YES" ]]; then | |
| RESULT+=$(git diff --name-only --diff-filter=d -z -- "**/*.swift" | xargs -0 -n1 -I{} $LINT --quiet --path $CURRENT_DIR/{}) | |
| fi | |
| if [[ "$RESULT" == '' ]]; then | |
| printColored "SwiftLint Finished." 32 | |
| else | |
| echo "" | |
| printColored "SwiftLint Failed. Please check below:" 31 | |
| 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" == 'error' ]; then | |
| printColored "\n $TYPE" 31 | |
| else | |
| printColored "\n $TYPE" 33 | |
| fi | |
| printColored " $FILEPATH:$L:$C" 90 | |
| printf " $MESSAGE - $DESCRIPTION\n" | |
| done <<< "$RESULT" | |
| printf "\nCOMMIT ABORTED. Please fix them before commiting.\n" | |
| exit 1 | |
| fi | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This script swiftlints only files to be committed.
There's one flow: it uses a hardcoded path to
.zsh_historyto determine wether git commit was invoked with an-aor--alloption. There may be a better way to check it.