-
Notifications
You must be signed in to change notification settings - Fork 73
/
pre-commit
executable file
·51 lines (43 loc) · 1.26 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
# This is a pre-commit hook that validates code formatting.
#
# Install this by running the script with an argument of "install",
# which adds a symlink at .git/hooks/precommit; or run the equivalent:
#
# $ ln -s ../../hooks/pre-commit .git/pre-commit
root="$(git rev-parse --show-toplevel 2>/dev/null)"
venv="$root/venv"
pip="$venv/bin/pip"
python="$venv/bin/python"
# Some sanity checking.
hash python3 || exit 1
[[ -n "$root" ]] || exit 1
# Installation.
if [[ "$1" == "install" ]]; then
hook="$root"/.git/hooks/pre-commit
if [[ -e "$hook" ]]; then
echo "Hook already installed:"
ls -l "$hook"
else
ln -s ../../pre-commit "$hook"
echo "Installed git pre-commit hook at $hook"
fi
exit
fi
# venv setup
if [ ! -d "$venv" ]; then
echo "Setting up python venv"
python3 -m venv "$venv"
fi
source "$venv/bin/activate"
if [ requirements.txt -nt "$venv" ]; then
"$pip" install --upgrade --upgrade-strategy eager -r requirements.txt
fi
exec 1>&2
# Perform validation.
trap 'git stash pop -q' EXIT
git stash push -k -u -q -m "pre-commit stash from $(date '+%FT%T%z')"
if ! "$python" activities.py validate; then
echo "Validation failed; \`git commit --no-verify\` to suppress validation."
exit 1
fi