From 3e42869bbb51ba1738aaa85c19ad2f01979d4cc3 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Tue, 19 Jul 2022 14:07:50 -0600 Subject: [PATCH] fixing git hook scripts --- git-hooks/post-checkout | 4 ++-- git-hooks/post-commit | 14 +++++++++----- git-hooks/post-update | 4 ++-- git-hooks/pre-commit | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 git-hooks/pre-commit diff --git a/git-hooks/post-checkout b/git-hooks/post-checkout index 7cd01cb..ffea63c 100644 --- a/git-hooks/post-checkout +++ b/git-hooks/post-checkout @@ -5,9 +5,9 @@ sed -i "/^GIT_HASH=\".*\"/ s//GIT_HASH=\"${SHORT_HASH}\"/" .env DIRTY_STATE=$(git diff --stat) if [ -n "$DIRTY_STATE" ]; then - sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_TAG=true/" .env + sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_DIRTY_STATE=true/" .env else - sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_TAG=false/" .env + sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_DIRTY_STATE=false/" .env fi GIT_TAG=$(git tag --points-at HEAD) diff --git a/git-hooks/post-commit b/git-hooks/post-commit index acecd9f..ffea63c 100644 --- a/git-hooks/post-commit +++ b/git-hooks/post-commit @@ -5,13 +5,10 @@ sed -i "/^GIT_HASH=\".*\"/ s//GIT_HASH=\"${SHORT_HASH}\"/" .env DIRTY_STATE=$(git diff --stat) if [ -n "$DIRTY_STATE" ]; then - sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_TAG=true/" .env + sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_DIRTY_STATE=true/" .env else - sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_TAG=false/" .env + sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_DIRTY_STATE=false/" .env fi - -SHORT_HASH=$(git rev-parse --short HEAD) -sed -i "/^GIT_HASH=\".*\"/ s//GIT_HASH=\"${SHORT_HASH}\"/" .env GIT_TAG=$(git tag --points-at HEAD) if [ -n "$GIT_TAG" ]; then @@ -19,3 +16,10 @@ if [ -n "$GIT_TAG" ]; then else sed -i "/^GIT_TAG=\".*\"/ s//GIT_TAG=\"\"/" .env fi + +GIT_BRANCH=$(git branch --show-current) +if [ -n "$GIT_BRANCH" ]; then + sed -i "/^GIT_BRANCH=\".*\"/ s//GIT_BRANCH=\"${GIT_BRANCH}\"/" .env +else + sed -i "/^GIT_BRANCH=\".*\"/ s//GIT_BRANCH=\"\"/" .env +fi diff --git a/git-hooks/post-update b/git-hooks/post-update index c2b1c96..f755dbf 100644 --- a/git-hooks/post-update +++ b/git-hooks/post-update @@ -5,9 +5,9 @@ sed -i "/^GIT_HASH=\".*\"/ s//GIT_HASH=\"${SHORT_HASH}\"/" .env DIRTY_STATE=$(git diff --stat) if [ -n "$DIRTY_STATE" ]; then - sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_TAG=true/" .env + sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_DIRTY_STATE=true/" .env else - sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_TAG=false/" .env + sed -i "/^GIT_DIRTY_STATE=.*/ s//GIT_DIRTY_STATE=false/" .env fi GIT_TAG=$(git tag --points-at HEAD) diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit new file mode 100644 index 0000000..d6b6976 --- /dev/null +++ b/git-hooks/pre-commit @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# get bash colors and styles here: +# http://misc.flogisoft.com/bash/tip_colors_and_formatting +C_RESET='\e[0m' +C_RED='\e[31m' +C_GREEN='\e[32m' +C_YELLOW='\e[33m' + +function __run() #(step, name, cmd) +{ + local color output exitcode + + printf "${C_YELLOW}[%s]${C_RESET} %-20s" "$1" "$2" + output=$(eval "$3" 2>&1) + exitcode=$? + + if [[ 0 == $exitcode || 130 == $exitcode ]]; then + echo -e "${C_GREEN}OK!${C_RESET}" + else + echo -e "${C_RED}NOK!${C_RESET}\n\n$output" + exit 1 + fi +} + +modified="git diff --diff-filter=M --name-only --cached | grep \".php$\"" +ignore="resources/lang,resources/views,bootstrap/helpers,database/migrations,bin" +phpcs="vendor/bin/phpcs --report=code --colors --report-width=80 --ignore=${ignore}" + +__run "1/1" "code sniffer" "${modified} | xargs -r ${phpcs}" +# __run "2/3" "php lint" "${modified} | xargs -r php -l" +# __run "3/3" "phpstan" "${modified} | xargs -r vendor/bin/phpstan analyse"