Last active
February 16, 2016 12:02
-
-
Save OakNinja/380a602714fbac87121c to your computer and use it in GitHub Desktop.
Pre-commit-hook that runs static analysis on staged python and js files. On errors, each error is printed and the commit is aborted .
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/sh | |
| echo '### Commit if static analysis pass' | |
| IFS=$'\n' | |
| staged_files=($(git diff --staged --name-only --no-color)) | |
| if [ ${#staged_files[@]} -eq 0 ]; then | |
| echo "### No staged files, exiting" | |
| exit 1 | |
| else | |
| echo '### Running static analysis on staged files' | |
| fi | |
| python_files=() | |
| js_files=() | |
| for i in "${staged_files[@]}" | |
| do | |
| if [[ $i =~ \.py$ ]]; then | |
| python_files+=($i) | |
| fi | |
| if [[ $i =~ \.js$ ]]; then | |
| js_files+=($i) | |
| fi | |
| done | |
| pass=true | |
| echo '### Running static analysis for Python files' | |
| for i in "${python_files[@]}" | |
| do | |
| result=$(env/bin/flake8 ${i}) | |
| if [ "$result" != "" ]; then | |
| echo "$result" | |
| pass=false | |
| fi | |
| done | |
| echo '### Running static analysis for JS files' | |
| npm list -g jshint > /dev/null | |
| if [ $? -eq 0 ] | |
| then | |
| for i in "${js_files[@]}" | |
| do | |
| result=$(jshint ${i}) | |
| if [ "$result" != "" ]; then | |
| echo "$result" | |
| pass=false | |
| fi | |
| done | |
| else | |
| echo '### jshint is not installed, skipping js linting. install with npm install -g jshint' | |
| fi | |
| if ! $pass; then | |
| echo '### Staged file(s) had linting errors, fix before committing or use flag --no-verify' | |
| exit 1 | |
| else | |
| echo '### Passed\n' | |
| exit 0 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment