我有一个位于服务器上的裸git存储库和一个用于ssh通信的git-shell用户。
问题是当我在该服务器上提交提交时,我无法强制使用用户名和用户电子邮件。
我在用户家中设置了〜/ .gitshrc:
export GIT_AUTHOR_NAME="John Doe"
export GIT_COMMITTER_NAME="John Doe"
以及~/.gitconfig
档案
[user]
name = John Doe
email = johndoe@example.com
但我在git日志中得到的只是用户名和用户电子邮件设置客户端。
如何在git-shell中重写用户名和用户邮箱?
答案 0 :(得分:1)
简单的答案:你不能。当git将提交推送到远程仓库时,它会将完全保持不变。提交者的名称是提交的一部分,因此更改名称将改变提交。
但是你可以
git filter-branch
)后者对于回购的用户来说有点不方便,因为他们fetch
不是他们只是push
,而是从技术角度来看它是可能的。更进一步,我会在commit
和push
阶段简单地控制名称。
如果您使用前一种方式,请使用pre-receive这样的git挂钩:
#!/bin/sh
while read oldrev newrev refname; do
for commit in $(git rev-list $newrev --not $oldrev); do
USER_NAME=$(git show -s --pretty=format:%an)
USER_EMAIL=$(git show -s --pretty=format:%ae)
COMMITTER_NAME=$(git show -s --pretty=format:%cn)
COMMITTER_EMAIL=$(git show -s --pretty=format:%ce)
# now perform checks you need and ...
if <check-failed> ; then
echo "Some explaining messages"
exit 1
fi
done
done
答案 1 :(得分:0)
感谢用户user3159253和另一个问题Can git pre-receive hooks evaulate the incoming commit? 我设法做了以下白名单过滤器:
#!/bin/bash
#
# Git pre-receive hook for validating commits authorship
# with linux account full name format : Firstname FULLNAME <email@address>
#
# Adapted from blacklist https://github.com/spuder/git-hooks/blob/master/pre-commit
#
# Extract full linux user name and email address
realName=$(getent passwd `whoami`| cut -d ':' -f 5 | cut -d ',' -f 1 |cut -d '<' -f 1|cut -d' ' -f1,2)
realEmail=$(getent passwd `whoami`| cut -d ':' -f 5 | cut -d ',' -f 1 |cut -d '<' -f 2|cut -d'>' -f1)
check_user() {
if [[ "$1" != "$realName" ]]; then
echo "You are commiting $sha1 as $2 $1 user which I don't like. Reveal your identity $realName!"
exit 1
fi
}
check_email() {
if [[ "$1" != "$realEmail" ]]; then
echo "You are commiting with $2 email $1 which I don't like. Reveal your email $realEmail!"
exit 1
fi
}
check_commit() {
# Check Author Email
check_email $(git log -1 --pretty=format:%ae $1) "author"
# Check Comitter Email
check_email $(git log -1 --pretty=format:%ce $1) "commiter"
# Check Author Name
check_user "$(git log -1 --pretty=format:%an $1)" "author"
# Check Comitter Name
check_user "$(git log -1 --pretty=format:%cn $1)" "commiter"
}
# Check all incoming commits
# https://stackoverflow.com/questions/22546393/can-git-pre-receive-hooks-evaulate-the-incoming-commit
NULL_SHA1="0000000000000000000000000000000000000000" # 40 0's
new_list=
any_deleted=false
while read oldsha newsha refname; do
case $oldsha,$newsha in
*,$NULL_SHA1) # it's a delete
any_deleted=true;;
$NULL_SHA1,*) # it's a create
new_list="$new_list $newsha";;
*,*) # it's an update
new_list="$new_list $newsha";;
esac
done
git rev-list $new_list --not --all |
while read sha1; do
objtype=$(git cat-file -t $sha1)
case $objtype in
commit) check_commit $sha1;;
esac
done