Last active
January 4, 2021 23:45
-
-
Save rbanffy/4d3fe7557f5828a5160bdd38d43597c8 to your computer and use it in GitHub Desktop.
A suggested pre-commit hook for projects with lots of legacy Python code
This file contains 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 | |
set -euo pipefail | |
function lint_added_file () { | |
# This is called for every file added in the current branch. This | |
# is the place we should feel more free to validate files more | |
# rigorously so that nothing we don't like can enter the codebase. | |
# Get the type of the file and choose what to do. | |
type=$(file --mime-type -b "$1") | |
case $type in | |
"text/x-shellscript") | |
shellcheck "$1" | |
;; | |
"text/x-python") | |
isort -faas "$1" | |
flake8 "$1" | |
black -l 79 "$1" | |
;; | |
*) | |
echo "File $1 was not processed" | |
esac | |
} | |
function lint_modified_file () { | |
# This is called for every file modified in the current branch. We | |
# can't be too rigorous here, as we risk failing validation for | |
# regions that were not changed. | |
# Get the type of the file and choose what to do. | |
type=$(file --mime-type -b "$1") | |
case $type in | |
"text/x-shellscript") | |
shellcheck "$1" | |
;; | |
"text/x-python") | |
flake8 "$1" | |
;; | |
*) | |
echo "File $1 was not processed" | |
esac | |
} | |
# Iterate over the files added on this branch | |
for file in $(git diff --name-only --diff-filter=A develop) | |
do | |
lint_added_file "$file" | |
done | |
# Iterate over the files modified on this commit | |
for file in $(git diff --name-only --diff-filter=M HEAD) | |
do | |
lint_modified_file "$file" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment