diff --git a/fake_home/.githooks/pre-commit b/fake_home/.githooks/pre-commit new file mode 100755 index 0000000..eb53fd1 --- /dev/null +++ b/fake_home/.githooks/pre-commit @@ -0,0 +1,76 @@ +#!/bin/bash + +# TO "INSTALL" +# +# Put this file in your ~/.githooks directory (or similar) +# Make sure the filename is 'pre-commit' +# Then add the following to your ~/.gitconfig, uncommented: +# +# [core] +# hooksPath = ~/.githooks + + +# This hook is called by "git commit" with no arguments. It should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. + +branch="$(git rev-parse --abbrev-ref HEAD)" + +if [ "$branch" = "master" ]; then + echo "You can't commit directly to master branch" + exit 1 +fi + +if test -f "$(git rev-parse --show-toplevel)/.git/MERGE_HEAD"; then + echo "Skipping pre-commit check for merge commit" + exit 0 +fi + +# Stash any changes not staged for commit +STASH_NAME=pre-commit-$(date +%s) +git stash save -q --keep-index $STASH_NAME + +# Returns to original pre-stash state, if necessary +pop() { + STASH_NUM=$(git stash list | grep $STASH_NAME | sed -re 's/stash@\{(.*)\}.*/\1/') + + # Only necessary if anything was stashed + if [ -n "$STASH_NUM" ]; then + git stash pop -q stash@{$STASH_NUM} + fi +} + +build() { + dir="$(echo $PWD | sed 's@/home/[^/]\+/@~/@')" + make test &> /dev/null + result="$?" + if [[ "$result" == 2 ]]; then + echo + elif [[ "$result" != 0 ]]; then + notify-send "Tests failed during commit in $dir" + else + notify-send "Tests passed in $dir" + fi + pop +} + +~/.githooks/pre-commit-build.sh "$STASH_NAME" + +# # Redirect output to stderr. +# exec 1>&2 +# +# # If the Makefile does not contain the 'test' command, `make test` will exit with '2' +# # This is not considered a test failure! +# if ! (make test || test $? = 2); then +# cat <<\EOF +# Error: `make test` exists in the Makefile, but failed +# +# If you know what you are doing you can skip this check using: +# +# git commit --no-verify +# EOF +# pop +# exit 1 +# fi +# +# pop diff --git a/fake_home/.githooks/pre-commit-build.sh b/fake_home/.githooks/pre-commit-build.sh new file mode 100755 index 0000000..dc1dc64 --- /dev/null +++ b/fake_home/.githooks/pre-commit-build.sh @@ -0,0 +1,20 @@ +#!/bin/bash +STASH_NAME="$1" + +pop() { + STASH_NUM=$(git stash list | grep $STASH_NAME | sed -re 's/stash@\{(.*)\}.*/\1/') + + # Only necessary if anything was stashed + if [ -n "$STASH_NUM" ]; then + git stash pop -q stash@{$STASH_NUM} + fi +} + +dir="$(echo $PWD | sed 's@/home/[^/]\+/@~/@')" +notify-send "Running $dir tests..." +if ! (make test &> /dev/null || test $? = 2); then + notify-send "Tests failed during commit in $dir" +else + notify-send "Tests passed in $dir" +fi +pop diff --git a/fake_home/.shell_aliases b/fake_home/.shell_aliases index 55469ff..327ed6d 100755 --- a/fake_home/.shell_aliases +++ b/fake_home/.shell_aliases @@ -19,7 +19,7 @@ function helpdef { } function wait-for { - while ! $@; do sleep 1; done + while ! bash -c "$@"; do sleep 1; done } function slides() {