forked from mozilla/standards-positions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·50 lines (44 loc) · 1.32 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
#!/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"
# 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"
source "$venv/bin/activate"
pip3 install -r requirements.txt
elif [ requirements.txt -nt "$venv" ]; then
source "$venv/bin/activate"
pip3 install --upgrade --upgrade-stategy eager -r requirements.txt
else
source "$venv/bin/activate"
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 ! python3 activities.py validate; then
echo "Validation failed; \`git commit --no-verify\` to suppress validation."
exit 1
fi