0

我有一个位于服务器上的裸 git 存储库和一个使用 git-shell 进行 ssh 通信的用户。

问题是当我与该用户在服务器上推送我的提交时,我无法强制使用用户名和用户电子邮件。

我在用户家中设置~/.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 中重写用户名和用户电子邮件?

4

2 回答 2

0

简单的答案:你不能。当 git 将提交推送到远程仓库时,它会完全保持不变。提交者的名称是提交的一部分,因此更改名称会更改提交。

但是你可以

  1. 强制您的客户仅使用某些用户名
  2. 或重写提交以具有某些用户名(使用git filter-branch

后者对于 repo 的用户来说会有些不便,因为他们不会fetch接受他们刚刚push编辑的内容,但从技术上讲这是可能的。尽管如此,我还是会简单地控制名称commitpush舞台。

如果您使用前一种方式,请使用这样的预接收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
于 2015-12-03T15:12:56.123 回答
0

感谢用户 user3159253 和这个其他问题Can git pre-receive hooks evaluate thecoming 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
于 2015-12-04T10:10:46.077 回答