阻止人们使用其他作者姓名推送git commit?
时间:2020-03-06 14:33:35 来源:igfitidea点击:
在git中,由每个用户决定在其本地git配置文件中指定正确的作者。当他们推送到集中式裸存储库时,存储库上的提交消息将具有在提交到自己的存储库时使用的作者姓名。
有没有一种方法可以强制使用一组已知的提交者?可以通过ssh访问"中央"存储库。
我知道这很复杂,因为有些人可能会推送其他人所做的提交。当然,我们还应该只允许我们信任的人推送到存储库,但是如果这里有一种防止用户错误的方法,那就太好了。
在git中有解决此问题的简单方法吗?
解决方案
我们可以做的是创建一堆不同的用户帐户,将它们全部放在同一组中,并赋予该组对存储库的写访问权限。然后,我们应该能够编写一个简单的传入挂钩,以检查执行脚本的用户是否与变更集中的用户相同。
我从来没有这样做过,因为我信任将代码检入到我的存储库中的人,但是如果有一种方法,那可能就是上面解释的方法。
git最初并不是旨在像svn那样在大型中央存储库中工作。也许我们可以根据需要从其他人那里撤出,如果他们的作者设置不正确,则拒绝撤出?
如果我们想管理面向git repo的互联网的权利,我建议我们关注Gitosis,而不要自己动手做。身份由私钥/公钥对提供。
也请阅读我在这里拉皮条的内容。
使用PRE-RECEIVE挂钩(有关详细信息,请参见githooks(5))。在那里,每个ref的更新都会得到旧的sha和新的sha。并且可以轻松列出更改并检查它们是否具有适当的作者(git rev-list --pretty = format:"%an%ae%n" oldsha..newsha)。
这是一个示例脚本:
#!/bin/bash
#
# This pre-receive hooks checks that all new commit objects
# have authors and emails with matching entries in the files
# valid-emails.txt and valid-names.txt respectively.
#
# The valid-{emails,names}.txt files should contain one pattern per
# line, e.g:
#
# ^.*@0x63.nu$
# ^[email protected]$
#
# To just ensure names are just letters the following pattern
# could be used in valid-names.txt:
# ^[a-zA-Z ]*$
#
NOREV=0000000000000000000000000000000000000000
while read oldsha newsha refname ; do
# deleting is always safe
if [[ $newsha == $NOREV ]]; then
continue
fi
# make log argument be "..$newsha" when creating new branch
if [[ $oldsha == $NOREV ]]; then
revs=$newsha
else
revs=$oldsha..$newsha
fi
echo $revs
git log --pretty=format:"%h %ae %an%n" $revs | while read sha email name; do
if [[ ! $sha ]]; then
continue
fi
grep -q -f valid-emails.txt <<<"$email" || {
echo "Email address '$email' in commit $sha not registred when updating $refname"
exit 1
}
grep -q -f valid-names.txt <<<"$name" || {
echo "Name '$name' in commit $sha not registred when updating $refname"
exit 1
}
done
done
我们使用以下内容来防止意外的未知作者提交(例如,从客户服务器或者其他设备进行快速提交时)。应将其放在.git / hooks / pre-receive中并使其可执行。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
from itertools import islice, izip
import sys
old, new, branch = sys.stdin.read().split()
authors = {
"John Doe": "[email protected]"
}
proc = subprocess.Popen(["git", "rev-list", "--pretty=format:%an%n%ae%n", "%s..%s" % (old, new)], stdout=subprocess.PIPE)
data = [line.strip() for line in proc.stdout.readlines() if line.strip()]
def print_error(commit, author, email, message):
print "*" * 80
print "ERROR: Unknown Author!"
print "-" * 80
proc = subprocess.Popen(["git", "rev-list", "--max-count=1", "--pretty=short", commit], stdout=subprocess.PIPE)
print proc.stdout.read().strip()
print "*" * 80
raise SystemExit(1)
for commit, author, email in izip(islice(data, 0, None, 3), islice(data, 1, None, 3), islice(data, 2, None, 3)):
_, commit_hash = commit.split()
if not author in authors:
print_error(commit_hash, author, email, "Unknown Author")
elif authors[author] != email:
print_error(commit_hash, author, email, "Unknown Email")

