diff --git a/README.md b/README.md index 67a24019b6c..8709d4291f1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/hooks/autohook.sh b/hooks/autohook.sh new file mode 100755 index 00000000000..0273c53dd28 --- /dev/null +++ b/hooks/autohook.sh @@ -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 "$@" diff --git a/hooks/pre-commit/01-run-black b/hooks/pre-commit/01-run-black new file mode 120000 index 00000000000..4ef81b4bab1 --- /dev/null +++ b/hooks/pre-commit/01-run-black @@ -0,0 +1 @@ +../scripts/run-black.sh \ No newline at end of file diff --git a/hooks/pre-commit/02-run-mypy b/hooks/pre-commit/02-run-mypy new file mode 120000 index 00000000000..58b731a8c77 --- /dev/null +++ b/hooks/pre-commit/02-run-mypy @@ -0,0 +1 @@ +../scripts/run-mypy.sh \ No newline at end of file diff --git a/hooks/pre-commit/03-run-flake8 b/hooks/pre-commit/03-run-flake8 new file mode 120000 index 00000000000..4679e1dcf8b --- /dev/null +++ b/hooks/pre-commit/03-run-flake8 @@ -0,0 +1 @@ +../scripts/run-flake8.sh \ No newline at end of file diff --git a/hooks/scripts/run-black.sh b/hooks/scripts/run-black.sh new file mode 100755 index 00000000000..558e63ceae6 --- /dev/null +++ b/hooks/scripts/run-black.sh @@ -0,0 +1,2 @@ +#!/bin/sh +riot -v run -s black . diff --git a/hooks/scripts/run-flake8.sh b/hooks/scripts/run-flake8.sh new file mode 100755 index 00000000000..9561a404fc4 --- /dev/null +++ b/hooks/scripts/run-flake8.sh @@ -0,0 +1,2 @@ +#!/bin/sh +riot -v run -s flake8 diff --git a/hooks/scripts/run-mypy.sh b/hooks/scripts/run-mypy.sh new file mode 100755 index 00000000000..6598ac87818 --- /dev/null +++ b/hooks/scripts/run-mypy.sh @@ -0,0 +1,2 @@ +#!/bin/sh +riot -v run -s mypy