Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 184 lines (160 sloc) 5.51 KB
#!/usr/bin/env bash
# Git pre-commit hook, that will automagically lint your code.
# Author: Mpho Mphego <mpho112@gmail.com>
# set -e
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
######### PRINT GREEN STD ##################
function gprint (){
echo "${GREEN}$1${NORMAL}";
}
######### PRINT RED STD ##################
function rprint (){
echo "${RED}$1${NORMAL}";
}
# # Find all staged files
declare -a PYTHON_FILES=$(git diff --name-only --cached --diff-filter=AM | grep --color=never '.py$')
declare -a SHELL_FILES=$(git diff --name-only --cached --diff-filter=AM | grep --color=never '.sh$')
declare -a MARKDOWN_FILES=$(git diff --name-only --cached --diff-filter=AM | grep --color=never '.md$')
declare -a TEX_FILES=$(git diff --name-only --cached --diff-filter=AM | grep --color=never '.tex$')
declare -a YAML_FILES=$(git diff --name-only --cached --diff-filter=AM | grep --color=never '.yml$')
declare -a CPP_FILES=$(git diff --name-only --cached --diff-filter=AM | grep --color=never '.c$')
declare -a DOCKER_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep --color=never -e '\Docker*' | sed 's@.*/@@')
if git rev-parse --abbrev-ref HEAD | grep -e 'master' > /dev/null 2>&1; then
rprint "Please switch to another branch other than master";
rprint "You can by-pass this hook by appending -n or --no-verify";
rprint "eg: git commit -a --no-verify";
rprint " -----------NOT RECOMMENDED-----------";
fi
# ########## PYTHON LINTERS #############
if [ -n "${PYTHON_FILES}" ]; then
# Verify that black is installed; if not, warn and exit.
if ! command -v black >/dev/null; then
rprint 'black not on path; can not format. Please install black:'
rprint ' `pip install black`'
exit 2
fi
fi
if [ -n "${YAML_FILES}" ]; then
# Verify that yamllint is installed; if not, warn and exit.
if ! command -v yamllint >/dev/null; then
rprint 'yamllint not on path; can not format. Please install yamllint:'
rprint ' `pip install yamllint`'
exit 2
fi
fi
if [ -n "${CPP_FILES}" ]; then
# Verify that cpplint is installed; if not, warn and exit.
if ! command -v cpplint >/dev/null; then
rprint 'cpplint not on path; can not format. Please install cpplint:'
rprint ' `pip install cpplint`'
exit 2
fi
fi
######### END PYTHON LINTER ##########
########## APT VERSION ############
if [ -n "${SHELL_FILES}" ]; then
# Verify that shellcheck is installed; if not, warn and exit.
if ! command -v shellcheck >/dev/null ; then
rprint 'shellcheck not on path; can not format. Please install shellcheck:'
rprint ' `apt-get install shellcheck`'
exit 2
fi
fi
if [ -n "${TEX_FILES}" ]; then
# Verify that chktex is installed; if not, warn and exit.
if ! command -v chktex >/dev/null ; then
rprint 'chktex not on path; can not format. Please install chktex:'
rprint ' `apt-get install chktex`'
exit 2
fi
fi
######### END APT VERSION #########
########## DOCKERFILE VERSION ############
if [ -n "${DOCKER_FILES}" ]; then
# Verify that docker is installed; if not, warn and exit.
if ! command -v docker >/dev/null ; then
rprint 'docker not on path; can not format. Please install docker:'
rprint ' `apt-get install docker-ce`'
exit 2
fi
fi
######### END DOCKERFILE VERSION #########
#/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
gprint "Checking files for syntax errors."
# Python Files
if [[ "${PYTHON_FILES[@]}" ]]; then
for file in "${PYTHON_FILES[@]}"; do
if ! $(command -v pycompile) "${file}"; then
rprint "${file} compile Failed!";
exit 1;
fi
gprint "Auto-formatting code with using black."
for fi in ${file}; do
echo "file: $fi"
$(command -v isort) "${fi}"
if ! $(command -v black) -l 90 "${fi}"; then
rprint "${file} auto-formatting Failed!";
exit 1;
fi
done
done
fi
# Shell scripts
if [[ "${SHELL_FILES[@]}" ]]; then
for file in "${SHELL_FILES[@]}"; do
if ! "$(command -v shellcheck)" "${file}"; then
rprint "${file} linting Failed!";
exit 1;
fi
done
fi
# latex files
if [[ "${TEX_FILES[@]}" ]]; then
for file in "${TEX_FILES[@]}"; do
if ! "$(command -v chktex)" "${file}"; then
rprint "${file} linting Failed!";
exit 1;
fi
done
fi
# Dockerfiles
if [[ "${DOCKER_FILES[@]}" ]]; then
for file in "${DOCKER_FILES[@]}"; do
if ! "$(command -v docker)" run --rm -ti hadolint/hadolint < "${file}"; then
rprint "${file} linting Failed!";
exit 1;
fi
done
fi
# Markdown
if [[ "${MARKDOWN_FILES[@]}" ]]; then
for file in "${MARKDOWN_FILES[@]}"; do
if ! "$(command -v docker)" run --rm -it -v "$(pwd):/app" mmphego/markdownlint "${file}"; then
rprint "${file} linting Failed!";
exit 1;
fi
done
fi
# yaml
if [[ "${YAML_FILES[@]}" ]]; then
for file in "${YAML_FILES[@]}"; do
if ! "$(command -v yamllint)" "${file}"; then
rprint "${file} linting Failed!";
exit 1;
fi
done
fi
# cpp
if [[ "${CPP_FILES[@]}" ]]; then
for file in "${CPP_FILES[@]}"; do
if ! "$(command -v cpplint)" "${file}"; then
rprint "${file} linting Failed!";
exit 1;
fi
done
fi
exit 0;
You can’t perform that action at this time.