From 4e16e2b17961075cd698ee5380941d5593a74e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 26 Sep 2018 16:50:07 +0200 Subject: [build] add pre-push hook for checking code formatting --- scripts/hooks/common.sh | 23 +++++++++++++++++++++++ scripts/hooks/pre-push | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 scripts/hooks/common.sh create mode 100755 scripts/hooks/pre-push diff --git a/scripts/hooks/common.sh b/scripts/hooks/common.sh new file mode 100644 index 0000000000..4d333689f8 --- /dev/null +++ b/scripts/hooks/common.sh @@ -0,0 +1,23 @@ +if [ "$1" = '--install' ]; then + mkdir -p ".git/hooks" + if [ -e ".git/hooks/$HOOK" ] || [ -L ".git/hooks/$HOOK" ]; then + if [ "$(readlink .git/hooks/$HOOK)" != "scripts/hooks/$HOOK" ]; then + echo "Can't install $HOOK because it has custom content" + else + echo "$HOOK hook is already installed" + fi + exit 1 + fi + echo "Installing $HOOK hook" + ln -s ../../scripts/hooks/$HOOK .git/hooks/$HOOK + exit 0 +elif [ "$1" = '--uninstall' ]; then + if [ "$(readlink .git/hooks/$HOOK)" = "scripts/hooks/$HOOK" ]; then + echo "Removing $HOOK hook" + rm .git/hooks/$HOOK + exit 0 + else + echo "$HOOK hook is not installed" + exit 1 + fi +fi diff --git a/scripts/hooks/pre-push b/scripts/hooks/pre-push new file mode 100755 index 0000000000..84bdafd70e --- /dev/null +++ b/scripts/hooks/pre-push @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +cd "$(git rev-parse --show-toplevel 2>/dev/null)" +HOOK=$(basename "${BASH_SOURCE[0]}") +source "scripts/hooks/common.sh" + +echo -n "Checking code formatting..." +RESULT=$(scripts/nitpick/format.js) +if [ ! -z "$RESULT" ]; then + echo -e " \033[1;31mChanged\033[0m" + echo -e -n "\033[1mDo you want to see the diff [y/N]?\033[0m " + read < /dev/tty + if [[ $REPLY =~ ^[Yy]$ ]]; then + echo "$RESULT" + echo "" + fi + + echo -e -n "\033[1mContinue to push? [Y/n]?\033[0m " + read < /dev/tty + if [[ $REPLY =~ ^[Nn]$ ]]; then + exit 1 + fi +else + echo -e " \033[32mOK\033[0m" +fi -- cgit v1.2.1