Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add pre-commit hooks #2324

Merged
merged 17 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ documentation][visualization docs].

See [docs/contributing.rst](docs/contributing.rst).


### Pre-commit Hooks

The tracer library uses formatting/linting tools including black, flake8, and mypy.
While these are run in each CI pipeline for pull requests, they are automated to run
when you call `git commit` as pre-commit hooks to catch any formatting errors before
you commit. To initialize the pre-commit hook script to run in your development
branch, run the following command:

$ hooks/scripts/autohook.sh install

### Testing


Expand Down
101 changes: 101 additions & 0 deletions hooks/autohook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash
# MIT License
#
# Copyright (c) 2017 Nikola Kantar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Autohook
# A very, very small Git hook manager with focus on automation
# Contributors: https://github.com/Autohook/Autohook/graphs/contributors
# Version: 2.3.0
# Website: https://github.com/Autohook/Autohook


echo() {
builtin echo "[Autohook] $@";
}


install() {
hook_types=(
"pre-commit"
)

repo_root=$(git rev-parse --show-toplevel)
hooks_dir="$repo_root/.git/hooks"
autohook_linktarget="../../hooks/autohook.sh"
for hook_type in "${hook_types[@]}"
do
hook_symlink="$hooks_dir/$hook_type"
ln -sf $autohook_linktarget $hook_symlink
done
}


main() {
calling_file=$(basename $0)

if [[ $calling_file == "autohook.sh" ]]
then
command=$1
if [[ $command == "install" ]]
then
install
fi
else
repo_root=$(git rev-parse --show-toplevel)
hook_type=$calling_file
symlinks_dir="$repo_root/hooks/$hook_type"
files=("$symlinks_dir"/*)
number_of_symlinks="${#files[@]}"
if [[ $number_of_symlinks == 1 ]]
then
if [[ "$(basename ${files[0]})" == "*" ]]
then
number_of_symlinks=0
fi
fi
echo "Looking for $hook_type scripts to run...found $number_of_symlinks!"
if [[ $number_of_symlinks -gt 0 ]]
then
hook_exit_code=0
for file in "${files[@]}"
do
scriptname=$(basename $file)
echo "BEGIN $scriptname"
eval "\"$file\""
script_exit_code="$?"
if [[ "$script_exit_code" != 0 ]]
then
hook_exit_code=$script_exit_code
fi
echo "FINISH $scriptname"
done
if [[ $hook_exit_code != 0 ]]
then
echo "A $hook_type script yielded negative exit code $hook_exit_code"
exit $hook_exit_code
fi
fi
fi
}


main "$@"
1 change: 1 addition & 0 deletions hooks/pre-commit/01-run-black
1 change: 1 addition & 0 deletions hooks/pre-commit/02-run-mypy
1 change: 1 addition & 0 deletions hooks/pre-commit/03-run-flake8
2 changes: 2 additions & 0 deletions hooks/scripts/run-black.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
riot -v run -s black .
2 changes: 2 additions & 0 deletions hooks/scripts/run-flake8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
riot -v run -s flake8
2 changes: 2 additions & 0 deletions hooks/scripts/run-mypy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
riot -v run -s mypy