2017-10-17 228 views
0

如何防止文件(.json)被git鉤子提交?如何防止文件(.json)被git鉤子提交?

我有一個.json這是在服務器上呢。所以我不能使用gitignore。但是現在我不想讓任何人改變這個文件(提交它),因爲它會破壞一些東西。我想要使​​用本地鉤子。

我怎麼能在提交中得到特殊文件?

請問你能給我一個說明如何做到這一點?

感謝您的幫助。

+0

你想防止自動生成的文件或代碼文件被提交? – LethalProgrammer

+0

沒有抱歉,解釋有點複雜。我希望鉤子檢查一個特殊的文件是否會被提交,如果該文件在提交中,則中止提交。 – Coder949

+2

是的,它有可能做到。你可以編寫預先提交的鉤子規則,這個規則將在提交時觸發,如果存在則刪除文件,然後提交。 – LethalProgrammer

回答

3

一個pre-commit樣本:

#!/bin/bash 

ipath="foo/bar.json" 
git diff --cached --name-only | if grep -qE "^$ipath$";then 
    git reset HEAD -- "$ipath" 
    echo "Warning: $ipath is removed from the index. It's not allowed to be committed." 
fi 

git diff --cached --name-only列出了所有更改的文件,這些文件將被提交。如果在列表中找到foo/bar.json,那麼git reset HEAD -- foo/bar.json會將其從索引中刪除,以便它未提交但留在工作樹中。

它適合你。但是你不能確保它爲別人做。例如,其他貢獻者可能會將其從本地存儲庫中刪除。您需要的是中央存儲庫中的pre-receive掛鉤,即服務器端的掛鉤。如果傳入的提交觸及foo/bar.json,它會拒絕任何推送。

pre-receive樣品:

#!/bin/bash 

ipath="foo/bar.json" 
zero="0000000000000000000000000000000000000000" 

while read old new name;do 
    if [ "$zero" == "$old" ];then 
     #creating new ref, do something here 
     continue 
    fi 

    if [ "$zero" == "$new" ];then 
     #deleting a ref, do something here 
     continue 
    fi 

    #updating a ref, check if the incoming commits touch `foo/bar.json` 
    git diff $old..$new --name-only | if grep -qE "^$ipath$";then 
     c=$(git log $old..$new --pretty=%H -- $ipath) 
     echo "Error: $ipath is changed in:" 
     echo $c 
     echo "Error: $ipath is not allowed to be committed and pushed." 
     exit 1 
    fi 
done 

貢獻者接收git push後的錯誤消息。他們必須修改他們的提交併在另一次嘗試之前刪除foo/bar.json的更改。在pre-receive中,如果需要,您需要處理deleting a refcreating a ref

+0

謝謝!問題是服務器端不支持它。所以我必須在當地進行。但第一部分將爲我在本地做對嗎? – Coder949