强制 git-shell 用户名覆盖给定的用户名

Force git-shell username to overwrite given username

我在服务器上有一个空的 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中重写用户名和用户邮箱?

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

但是你可以

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

后者对于 repo 的用户来说有点不方便,因为他们 fetch 不是他们刚刚 push 编辑的内容,但从技术上讲这是可能的。尽管如此,我还是会在 commitpush 阶段保持对名称的简单控制。

如果您采用前一种方式,请使用 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

感谢用户 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 [[ "" != "$realName" ]]; then
      echo "You are commiting $sha1 as   user which I don't like. Reveal your identity $realName!"
      exit 1
     fi
}

check_email() {
     if [[ "" != "$realEmail" ]]; then
      echo "You are commiting with  email  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 ) "author"
    # Check Comitter Email
    check_email $(git log -1 --pretty=format:%ce ) "commiter"

    # Check Author Name
    check_user "$(git log -1 --pretty=format:%an )" "author"
    # Check Comitter Name
    check_user "$(git log -1 --pretty=format:%cn )" "commiter"
}


# Check all incoming commits
# 

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