防止用户使用不同的作者名称推送git commit? [英] Prevent people from pushing a git commit with a different author name?

查看:118
本文介绍了防止用户使用不同的作者名称推送git commit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在git中,每个用户都需要在其本地git配置文件中指定正确的作者。当他们推送到集中的裸仓库时,仓库中的提交消息将具有他们在提交到他们自己的仓库时使用的作者姓名。



是否有强制执行的方法是否使用了一组已知的提交作者?通过ssh可以访问中央存储库。



我知道有些人可能会推送其他人提交的提交,这很复杂。当然,你也应该只允许你信任的人推送你的存储库,但如果有一种方法来防止用户错误,这将是很好的。



在git中的这个问题的简单解决方案?我们使用以下命令来防止意外的未知作者提交(例如,当从客户的服务器执行快速提交或某事时)。它应该放在.git / hooks / pre-receive中,并且可执行。

 #!/ usr / bin / env python 
# - * - coding:utf-8 - * -
从itertools导入子流程
导入islice,izip
导入sys

旧的,新的, branch = sys.stdin.read()。split()

作者= {
John Doe:john.doe@example.com
}

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 (提交,作者,电子邮件,信息):
打印** 80
打印错误:未知作者!
print - * 80
proc = subprocess.Popen([git,rev-list,--max-count = 1,--pretty = short,commit ],stdout = subprocess.PIPE)
print proc.stdout.read()。strip()
print** 80
提升SystemExit(1)
$ b $ (islice(data,0,None,3),islice(data,1,None,3),islice(data,2,None,3)):
_ commit_hash = commit.split()
如果作者不是作者:
print_error(commit_hash,author,email,Unknown Author)
elif authors [author]!= email:
print_error(commit_hash,作者,电子邮件,未知电子邮件)


In git, it is up to each user to specify the correct author in their local git config file. When they push to a centralized bare repository, the commit messages on the repository will have the author names that they used when committing to their own repository.

Is there a way enforce that a set of known authors for commits are used? The "central" repository will be accessible via ssh.

I know that this is complicated by the fact that some people may be pushing commits that were made by others. Of course, you should also only allow people you trust to push to your repositories, but it would be great if there was a way to prevent user error here.

Is there a simple solution to this problem in git?

解决方案

We use the following to prevent accidental unknown-author commits (for example when doing a fast commit from a customer's server or something). It should be placed in .git/hooks/pre-receive and made executable.

#!/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": "john.doe@example.com"
}

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")

这篇关于防止用户使用不同的作者名称推送git commit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆