Skip to content

Instantly share code, notes, and snippets.

@hidao80
Last active September 3, 2023 02:56
Show Gist options
  • Select an option

  • Save hidao80/98bd629acd4d407307d74ec152a08987 to your computer and use it in GitHub Desktop.

Select an option

Save hidao80/98bd629acd4d407307d74ec152a08987 to your computer and use it in GitHub Desktop.
git hooks for laravel and Redmine
#!/bin/bash
<< COMMENT
Copyright (c) 2022 hidao80
Released under the MIT license
https://opensource.org/licenses/mit-license.php
COMMENT
#
# Message pre-process
#
msg=`cat $1 | sed -e '/^#/d'`
while [ "`echo "$msg" | sed -n -e "1p"`" = "" ]; do
msg=`echo "$msg" | sed -e "1d"`
done
status=0
# Prefix check
if [[ ! "$msg" =~ ^(追加|修正|改善|更新|削除|改名|移動|交換|[Aa]dd|[Ff]ix|[Ii]mprove|[Uu]pdate|[Rr]emove|[Rr]ename|[Mm]ove|[Cc]hange): ]]; then
cat - << EOF
コミットメッセージの見出しが不正です: $msg
構文:
[Prefix] <Message body> <ticket_no> <working_hours>
見出しとして使えるもの:
追加: または add:
修正: または fix:
改善: または improve:
更新: または update:
削除: または remove:
改名: または rename:
移動: または move:
交換: または change:
EOF
status=1
fi
# Body check
if [[ ! "$msg" =~ \#[0-9]+ ]]; then
cat - << EOF
チケット番号が記入されていません: $msg
ticket noの例:
#1234
EOF
status=2
fi
if [[ ! "$msg" =~ @[0-9]+(\.[0-9]+)?h ]]; then
cat - << EOF
作業時間が記入されていません: $msg
working hoursの例:
@1.75h
EOF
status=3
fi
cat storage/logs/phpstan.log
exit $status
#!/bin/bash
<< COMMENT
Copyright (c) 2022 hidao80
Released under the MIT license
https://opensource.org/licenses/mit-license.php
COMMENT
php_target_files=`git diff --cached --name-only | grep .php`
js_target_files=`git diff --cached --name-only | grep ".js\|.vue\|.jsx\|.tsx\|.ts\|.json\|.html\|.twig"`
phpcs=`composer global config bin-dir --absolute 2> /dev/null`/phpcs
phpstan=`composer global config bin-dir --absolute 2> /dev/null`/phpstan
viewer=code
if [ -e ./phpcs.xml ]; then
# Defaults to what is in the home directory
phpcs_ruleset=~/phpcs.xml
else
# Use what is in the root of the repository.
phpcs_ruleset=./phpcs.xml
fi
# For when you are using other shells in your main
export NODE_PATH=`npm root -g`
if [ -e ./storage/logs ]; then
# for Laravel
phpstan_extension="-c `composer global config home --absolute 2> /dev/null`/vendor/nunomaduro/larastan/extension.neon"
output=./storage/logs/git_hook.log
else
# for non-Laravel
phpstan_extension=""
output=./git_hook.log
fi
# clear pre-commit log
echo "" > $output
# If there are no js/json files, exit
if [ -z "$js_target_files" ]; then
echo "[eslint] js/json files not found." >> $output 2>&1
else
echo "[eslint] running..." >> $output 2>&1
eslint $js_target_files >> $output 2>&1
fi
# If there are no PHP files, exit
if [ -z "$php_target_files" ]; then
echo "[larastan] php files not found." >> $output 2>&1
else
echo "[phpcs] running..." >> $output 2>&1
$phpcs --standard=$phpcs_ruleset $php_target_files >> $output 2>&1
echo "[larastan] running..." >> $output 2>&1
php -d memory_limit=-1 $phpstan analyse -l 5 $phpstan_extension $php_target_files >> $output 2>&1
fi
# Open the log in some editor.
$viewer $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment