2012-03-09 80 views
13

我的計劃是使用git來跟蹤/ etc中的更改,但是在提交時我希望讓進行更改的人員通過添加 - 命令行上的作者選項。使用預先提交的鉤子阻止特定作者的git落實

所以我想停止意外的提交作爲根。

我試着創建這個預提交鉤子,但它不工作 - 即使我在提交行指定作者,git var仍然返回根目錄。

AUTHOR=`git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/\1/p'` 
if [ "$AUTHOR" == "root <[email protected]>" ]; 
then 
    echo "Please commit under your own user name instead of \"$AUTHOR\":" 
    echo 'git commit --author="Adrian"' 
    echo "or if your name is not already in logs use full ident" 
    echo 'git commit --author="Adrian Cornish <[email protected]>"' 
    exit 1 
fi 
exit 0 
+1

這是令人驚訝似乎沒有成爲一種方式來獲得這方面的資料。我可以確認'git var GIT_AUTHOR_IDENT'顯示原始作者,而不是通過'--author'提供的作者... – Borealid 2012-03-09 00:42:25

+0

切向也是https://gist.github.com/tripleee/16767aa4137706fd896c – tripleee 2014-06-09 12:32:39

回答

10

的Git的當前版本不會使通過環境變量,命令行參數或標準輸入提供給Git的掛鉤--author信息。然而,而不需要使用--author命令行的,你可以指導用戶設置GIT_AUTHOR_NAMEGIT_AUTHOR_EMAIL環境變量:

#!/bin/sh 
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1 
NAME=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p') 
EMAIL=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p') 
[ "${NAME}" != root ] && [ "${EMAIL}" != "[email protected]" ] || { 
    cat <<EOF >&2 
Please commit under your own name and email instead of "${NAME} <${EMAIL}>": 
GIT_AUTHOR_NAME="Your Name" GIT_AUTHOR_EMAIL="[email protected]" git commit 
EOF 
    exit 1 
} 

--author說法,這些環境變量控制提交的作者。由於這些環境變量在Git環境中,因此它們也處於pre-commit掛鉤的環境中。並且因爲他們處於pre-commit掛鉤的環境中,所以他們被傳遞給git var GIT_AUTHOR_IDENT,使用它們就像git commit那樣。

不幸的是,設置這些變量比使用--author方便得多。我建議聯繫Git開發人員並要求他們在啓動pre-commit鉤子之前設置這些環境變量(使用通過--author傳遞的值)。

+0

感謝這和我完全一樣通緝。我在GIT_AUTHOR_NAME和GIT_AUTHOR_EMAIL上試用了git var,它一直都是空白 - 這就解釋了原因。 – 2012-03-09 14:49:32

+0

這是不正確anylonger,似乎與GIT 2.7.4(mabye也早)工作。將'echo $ GIT_AUTHOR_NAME'放在'pre-commit'鉤子中打印出作者的名字(無論是在git config中設置的名稱還是通過'--author'傳遞的名稱)。 'git var GIT_AUTHOR_IDENT'似乎也適用於'--author'。 – lumbric 2018-03-10 19:06:03

0

我使用了以下內容,將其添加到系統.bashrc中。

它不會吸引那些真正紮根並住在那個殼裏的民衆,(壞!) 但是,當民間使用sudo時,它確實保持我的日誌有用。 我也在試着用git保存一個/ etc的更新日誌 - 這樣我就可以看到每個月做了些什麼。

#I want everyone to check in changes to /etc files, but also want their names even when they use sudo. 
export GIT_COMMITTER_EMAIL=${USER}@ourcompany.co.nz 
export GIT_AUTHOR_EMAIL=${USER}@ourcompany.co.nz 

https://serverfault.com/questions/256754/correct-user-names-when-tracking-etc-in-git-repository-and-committing-as-root

相關問題