GIT hook to prevent an experimental branch pushed to a release, or master branch

与世无争的帅哥 提交于 2019-11-29 12:51:34

Here's an update hook that should work for that. You can specify commits that shouldn't be allowed into your RELEASE or MASTER branch by giving them tags like forbidden/junk or forbidden/debugging. If a forbidden commit is found, the tag name will be included in the rejection message.

refname="$1"
oldrev="$2"
newrev="$3"
case "$refname" in
  refs/heads/RELEASE|refs/heads/MASTER)
    for forbidden in $(git tag -l 'forbidden/*'); do
      if [ $(git merge-base "$forbidden" $newrev) = $(git rev-parse "$forbidden") ]; then
        echo "Push to $refname contains commit $forbidden" >&2
        exit 1
      fi
    done
    ;;
esac
exit 0

Note that if you have a branch that contains several problem commits you must create a forbidden tag for the earliest one, not just the final commit in the series. So if history like the following where B,C, and D are all forbidden just tagging D as forbidden would not prevent E from being merged in and bringing B along with it.

A---B----C----D
     \
      ---E

This issue needs to be solved as a management issue, not an automation issue. The problem is that TEST likely contains the majority of commits from the other two branches as well. So you won't be able to effectively identify commits that come from TEST. In our environment, for example, we regularly update experimental branches with newer commits from the master branch.

You need someone to act as a release manager, to ensure that if something bad does gets merged into master, then it doesn't get deployed before the issue is resolved. The problem isn't necessarily a bad merge, per se. The problem is deploying a bad merge.

One tool you might find useful is at Bitbucket.org, where they have a rudimentary approval mechanism for commits. It's only advisory, but could be helpful for you to track which commits have been approved and which might have gotten merged in by mistake.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!