pre-receive hook for git: prevent from pushing to remote master carelessly

For the first time, I try to collaborate with my colleagues on git. Because we are afraid of ruining the master branch, I add the pre-receive hook on the remote server. At first I was trying to use the /dev/tty as suggested in

https://dev.ghost.org/prevent-master-push/

, however I realize for the remote server the ssh does not provide the prompt. I decide that the master can only be upated if the change has been submitted to stable_release branch.

2017.03.10 update:
(1) deleting stable_release and master branch is not allowed.
(2) new branch can be added only if it ends with _dev.

The code is the following:

# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".

#. /usr/share/git-core/contrib/hooks/post-receive-email
while read oldrev newrev refname
do
  # not push
  if [ "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-132a9baa84fad69ac3815df24d0857c2_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#123;&#111;&#108;&#100;&#114;&#101;&#118;&#58;&#40;&#45;&#52;&#41;&#125;&#34;&#32;&#61;&#61;&#32;&#48;&#48;&#48;&#48;&#32;&#93;&#59;&#32;&#116;&#104;&#101;&#110; &#32;&#32;&#32;&#32;&#105;&#102;&#32;&#91;&#32;&#34;&#95;&#100;&#101;&#118;&#34;&#32;&#33;&#61;&#32;&#34;" title="Rendered by QuickLaTeX.com" height="19" width="331" style="vertical-align: -5px;"/>{refname:(-4)}" ]; then
      echo "only add new username_dev branch is allowed"
      exit 1
    fi
  fi
  if [ "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-3e22c1b1818bf1acc68cdf4f742fd3d1_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#123;&#111;&#108;&#100;&#114;&#101;&#118;&#58;&#40;&#45;&#52;&#41;&#125;&#34;&#32;&#33;&#61;&#32;&#48;&#48;&#48;&#48;&#32;&#93;&#59;&#32;&#116;&#104;&#101;&#110; &#32;&#32;&#32;&#32;&#98;&#114;&#97;&#110;&#99;&#104;&#61;" title="Rendered by QuickLaTeX.com" height="19" width="294" style="vertical-align: -5px;"/>(git rev-parse --symbolic --abbrev-ref <img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-4cd4be41074d966df2fa31e6873b0e78_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#114;&#101;&#102;&#110;&#97;&#109;&#101;&#41; &#32;&#32;&#32;&#32;&#105;&#102;&#32;&#91;&#32;&#34;" title="Rendered by QuickLaTeX.com" height="19" width="107" style="vertical-align: -5px;"/>{newrev:(-4)}" == 0000 ]; then
      if [ "master" == "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-fc7832d73c2ac5666786f7add88bb15c_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#98;&#114;&#97;&#110;&#99;&#104;&#34;&#32;&#93;&#32;&#124;&#124;&#32;&#91;&#32;&#34;&#115;&#116;&#97;&#98;&#108;&#101;&#95;&#114;&#101;&#108;&#101;&#97;&#115;&#101;&#34;&#32;&#61;&#61;&#32;&#34;" title="Rendered by QuickLaTeX.com" height="19" width="246" style="vertical-align: -5px;"/>branch" ]; then
        echo "deleting master branch and stable_release branch is not allowed."
        exit 1
      fi
    fi
    if [ "master" == "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-fc7832d73c2ac5666786f7add88bb15c_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#98;&#114;&#97;&#110;&#99;&#104;&#34;&#32;&#93;&#32;&#124;&#124;&#32;&#91;&#32;&#34;&#115;&#116;&#97;&#98;&#108;&#101;&#95;&#114;&#101;&#108;&#101;&#97;&#115;&#101;&#34;&#32;&#61;&#61;&#32;&#34;" title="Rendered by QuickLaTeX.com" height="19" width="246" style="vertical-align: -5px;"/>branch" ]; then
      stable_head="<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-c2492814b8eb878897d6fa341adfff40_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#40;&#103;&#105;&#116;&#32;&#115;&#104;&#111;&#119;&#45;&#114;&#101;&#102;&#32;&#45;&#115;&#32;&#115;&#116;&#97;&#98;&#108;&#101;&#95;&#114;&#101;&#108;&#101;&#97;&#115;&#101;&#41;&#34; &#32;&#32;&#32;&#32;&#32;&#32;&#105;&#102;&#32;&#91;&#32;&#34;&#109;&#97;&#115;&#116;&#101;&#114;&#34;&#32;&#61;&#61;&#32;&#34;" title="Rendered by QuickLaTeX.com" height="19" width="404" style="vertical-align: -5px;"/>branch" ]; then
        if [ "<img src="https://dingxf.cn/blog/wp-content/ql-cache/quicklatex.com-b5935252b939963dd02df6277f399b1a_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="&#110;&#101;&#119;&#114;&#101;&#118;&#34;&#32;&#33;&#61;&#32;&#34;" title="Rendered by QuickLaTeX.com" height="12" width="102" style="vertical-align: 0px;"/>stable_head" ]; then
          echo "The stable_release is not updated. push to stable release first"
          echo "If you are a normal user, don't try to push to master branch. Please work your own dev branch. You can push your dev branch. Follow these instructions(change username to your own):"
          echo "git checkout -b username_dev"
          echo "git branch -D master"
          echo "git checkout master"
          echo "git checkout username_dev"
          echo "git push origin username_dev"
          exit 1
        else
          echo "You are pushing to the master branch. Be careful and be responsible for what you did. Now I give you 7 seconds to regret"
          for((i=0;i&lt;7;++i)); do
            printf "."
            sleep 1
          done
          echo ""
          exit 0
        fi
      fi
      exit 0
    fi
  fi
  exit 0
done