|
#!/bin/bash |
|
|
|
# "pre-commit" occurs before a git commit is accepted |
|
# This hook runs when you run the command git commit. |
|
# The hook runs before a commit is accepted to git, |
|
# and if the hook script fails (ie if the script |
|
# returns a non-zero exit code) the commit is aborted. |
|
|
|
#******************************* |
|
# If any command fails, exit immediately with that command's exit status |
|
set -eo pipefail |
|
|
|
# Find all changed files for this commit |
|
# Compute the diff only once to save a small amount of time. |
|
CHANGED_FILES=$(git diff --name-only --cached --diff-filter=ACMR) |
|
# Get only changed files that match our file suffix pattern |
|
get_pattern_files() { |
|
pattern=$(echo "$*" | sed "s/ /\$\\\|/g") |
|
echo "$CHANGED_FILES" | { grep "$pattern$" || true; } |
|
} |
|
# Get all changed python files |
|
PY_FILES=$(get_pattern_files .py) |
|
|
|
if [[ -n "$PY_FILES" ]] |
|
then |
|
# Run black against changed python files for this commit |
|
black --check $PY_FILES |
|
echo "black passes all altered python sources." |
|
# Run flake8 against changed python files for this commit |
|
flake8 $PY_FILES |
|
echo "flake8 passed!" |
|
fi |
|
|
|
#******************************* |
|
# This pre-commit hook watches for incorrect commit authors and unsigned commits using the script below. |
|
|
|
globalEmail=`git config --global --get user.email` |
|
signingKey=`git config --global --get user.signingkey` |
|
|
|
if [[ $globalEmail != $signingKey ]]; |
|
then |
|
echo "Commit email and global git config email differ" |
|
echo "Global commit email: "$globalEmail"" |
|
echo "Committing email expected: $workEmail" |
|
exit 1 |
|
fi |
|
|
|
if [[ $signingKey -eq "" ]]; |
|
then |
|
echo "No signing key found. Check global gitconfig" |
|
exit 1 |
|
else |
|
echo "" |
|
exit 0 |
|
fi |
|
|
|
# EOF |