Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

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

We have three main branches in our workflow.

TEST (experimental), RELEASE (features going to next release), and MASTER (released only)

We take feature branches from RELEASE, merge feature branches first to TEST and if they are ok, merge those approved feature branches to RELEASE.

My problem is: as TEST branch contains some commits/features that we will not be releasing ever, we don't want it to merged into RELEASE or MASTER by mistake (or intentionally).

I read somewhere that it is not possible or feasible to prevent merges in local repositories, and i don't think it would solve my problem.

So, probably it is better to prevent updates to MASTER or RELEASE branch refs in main repository (by pushing to origin) when the new ref contains a specific commit ID of TEST branch in its commit log.

So i'll make a specific commit only to TEST branch, and record its Commit ID.

Whenever someone wants to push to master or release branch, i'll check if that push will update my refs/heads/master or refs/heads/RELEASE to a commit ref which contains that bad Commit ID in its history and abort.

As i'm no BASH or GIT master, does anybody have such an update hook that we can apply to our main repository?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...