LoginSignup
0
0

More than 5 years have passed since last update.

.git/hooks/pre-push 覚書 ver.2

Posted at

ver 1 → .git/hooks/pre-push 覚書(railsアプリ編)

hotfix 作業中に別タスク( featuredevelop )のプルリクが通り、hotfix 終了後に誤って git push -f origin develop をやらかして develop を上書きしてしまった。 feature ブランチは残っていたのでプルリクを出し直してレビュワーに焼き土下座をしたが、

  • developgit 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))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0