ver 1 → .git/hooks/pre-push 覚書(railsアプリ編)
hotfix
作業中に別タスク( feature
→ develop
)のプルリクが通り、hotfix
終了後に誤って git push -f origin develop
をやらかして develop
を上書きしてしまった。 feature
ブランチは残っていたのでプルリクを出し直してレビュワーに焼き土下座をしたが、
-
develop
にgit push -f
したらアカン - 誤って
git push -f
しても、pre-push
フックで弾いて欲しい
というわけでこちらを参考に .git/hooks/pre-push
を修正
.git/hooks/pre-push
# !/bin/bash
protected_branch='master|develop'
current_branch=$(git symbolic-ref HEAD | sed -e 's,refs/heads/\(.*\),\1,')
push_command=$(ps -ocommand= -p $PPID)
is_destructive='force|delete|\-f'
policy="[Policy] Never force push or delete the $protected_branch branch! (Prevented with pre-push hook.)"
proc_exit(){
echo $policy
exit 1
}
prevent_force_push() {
if [[ $push_command =~ $is_destructive ]]; then
if [[ $current_branch =~ $protected_branch ]] || [[ $push_command =~ $protected_branch ]]; then
proc_exit
fi
fi
}
prevent_delete() {
delete_reg=' :('$protected_branch')'
if [[ $push_command =~ $delete_reg ]]; then
proc_exit
fi
}
proc_test() {
activator scalastyle
ss=$?
activator test:scalastyle
tss=$?
activator test
t=$?
exit $(($ss || $tss || $t))
}
prevent_force_push
prevent_delete
proc_test
railsアプリ の場合は proc_test
を次のように書き換える。
bundle exec rubocop
cop=$?
bundle exec rspec spec
pec=$?
exit $(($cop || $pec))