yano3nora
8/17/2017 - 4:19 AM

[git: pre-script] Deny commit and push to specific branch by pre-script in .git/hooks - #git

[dev: pre-script] Deny commit and push to specific branch by pre-script in .git/hooks - #git

What is ?

.git/hooks/ 配下の pre-pushpre-commit に記述することで、ローカルの Git クライアントから当該リポジトリの特定ブランチへの PUSH / COMMIT 等を抑止するスクリプト。

What for ?

GitHub-Flow では基本的に origin/masterorigin/developclonefetch & merge はするが 直接 commit push はしない(基本的に PR → レビュー → 管理者によるマージ受け入れの流れで運用するため)。この運用を徹底するためのスクリプト。

Notice

ローカルクライアントへの直設定なので、作業者の分だけこの設定が必要になる。抜け漏れミス防止するために、ホスティングサーバ上でも origin/master origin/develop への PUSH 抑止設定ができるならしておくべき。


Sample Code

Pre-push

cd {githome}/.git/hooks/
vi pre-push
chmod 775 pre-push
#!/bin/sh

# if the branch is master, then fail.

branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
       "$(git describe --contains --all HEAD)"

if [ "${branch##refs/heads/}" = "master" ] || [ "${branch##refs/heads/}" = "develop" ]; then
    echo "DENY PUSH TO MASTER!! FXXK!!"
    exit 1
fi

Pre-commit

cd {githome}/.git/hooks/
vi pre-commit
chmod 775 pre-commit
#!/bin/sh

# if the branch is master, then fail.

branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
       "$(git describe --contains --all HEAD)"

if [ "${branch##refs/heads/}" = "master" ] || [ "${branch##refs/heads/}" = "develop" ]; then
  echo "DON'T COMMIT TO MASTER!! FXXK!!"
  exit 1
fi

Refs