2017-04-12 57 views
1

我希望能夠設置我們的分支機構,以便合併只能從開發分支進入主人。Git只允許從開發合併到主人

我知道這聽起來很嚴厲,我應該問自己這個問題,我不信任團隊中的開發人員......暫時我不會因爲他們只是熟悉Git。及時我會刪除限制,但在此之前這將是一個有用的。可能嗎?

謝謝, 馬克。

+0

你不能只是恢復他們的變化,如果他們把事情弄糟了? – Vallentin

+0

是的我可以和謝謝你的回覆,但沒有解決問題 –

+1

如果你可以設置一個鉤子並且完成它,你不希望每隔一週就把一個凌亂的主人恢復。節省時間和精力。 – kowsky

回答

2

作爲kowsky answered,可以在服務器端的預接收鉤子中執行此操作。但令人驚訝的是,主要是因爲Git的分支概念有點滑。

我寫了一個預接收鉤子,這樣做,除其他外, the code is available here。請參閱merges_from函數,並記下它上面的註釋。由於在StackOverflow中鏈接有點不合適,我也會在這裏包含實際的功能。 (請注意,此代碼是指具有積極古老的版本的Git的工作,所以它不使用現代的功能,如git for-each-ref --merged。)


# $1 is a merge commit. Find all its parents, except the first 
# (which is "on" the branch being merged-into and hence not relevant). 
# For each remaining parent, find which branch(es) contain them. 
# If those branch heads *are* them, consider that the "source" of the merge. 
# 
# Note that this means if branches A and B both name commit 1234567, 
# and you merge commit 1234567 into branch master, this considers 
# the merge (with its one parent) to merge both branches A and B. 
# That may not be ideal, but it is what we get... 
merges_from() 
{ 
    local branches b src 

    set -- $($GIT rev-list --parents --no-walk $1) 
    shift 2 
    for i do 
     # git branch --contains may print something like 
     # * (no branch) 
     # or 
     # * (detached from blah) 
     # if HEAD is detached. This should not happen in 
     # a bare repo, but if it does, we'll add HEAD to 
     # our list. 
     branches=$($GIT branch --merged $i | 
      sed -e 's/\* (.*)/HEAD/' -e 's/^[* ]//') 
     set -- $branches 
     src="" 
     for b do 
      if [ $($GIT rev-parse $b) = $i ]; then 
       src="${src}${src:+ }$b" 
      fi 
      [ "$src" == "" ] && src="-" 
     done 
     echo $src 
    done 
} 
+0

感謝百萬Torek,我會給我一個機會,我得到的第一個機會。不幸的是,我每週只能在這個特定的項目上得到幾個小時。我會迴應我的成功。 –

0

它並不嚴格,它非常有意義,可以防止嚴重事故。

說了這麼一句話:你可以通過git hooks來控制這樣的事情。這些是在某些事件上執行的腳本。在你的情況下,服務器端預接收或客戶端預推鉤像thisthis將有所幫助。