diff --git a/tools/hooks/README.md b/tools/hooks/README.md new file mode 100644 index 000000000..30a6f6954 --- /dev/null +++ b/tools/hooks/README.md @@ -0,0 +1,12 @@ +# Git Hooks + +These are scripts executed by Git at specific steps of the version control workflow, depending on their name. + +- `pre-commit` runs before attempting to commit, and prevents committing to the local master branch. + +To install, +```bash +$ ./install_hooks.bash +``` + +To read more about hooks, read the [Pro Git chapter](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). diff --git a/tools/hooks/install_hooks.bash b/tools/hooks/install_hooks.bash new file mode 100755 index 000000000..c7a7bb923 --- /dev/null +++ b/tools/hooks/install_hooks.bash @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -o errexit + +# Grab the directory of _this_ script. +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +PROJECTDIR="$(realpath "${SCRIPTDIR}"/../..)" + +# Try to link each hook into the project's .git/hooks directory. +for hook in $(ls "${SCRIPTDIR}"/*.hook); do + stub="${hook%.*}" + dest="${PROJECTDIR}/.git/hooks/$(basename ${stub})" + ln -sv "${hook}" "${dest}" +done diff --git a/tools/hooks/pre-commit.hook b/tools/hooks/pre-commit.hook new file mode 100755 index 000000000..35ce9d24a --- /dev/null +++ b/tools/hooks/pre-commit.hook @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +# prevent commit to local master branch +# taken from https://gist.github.com/aaronhoffman/ffbfd36928f9336be2436cffe39feaec#file-pre-commit + +branch=`git symbolic-ref HEAD` +if [ "$branch" = "refs/heads/master" ]; then + echo "pre-commit hook: Cannot commit to the local master branch." + echo "Please make all your changes in a new feature branch:" + echo "$ git checkout -b my-new-feature-branch" + exit 1 +fi + +exit 0