git - 防止人们使用不同的作者姓名推送 git commit?

标签 git

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

有没有办法强制使用一组已知的提交作者?可以通过 ssh 访问“中央”存储库。

我知道这很复杂,因为有些人可能正在 push 其他人所做的提交。当然,您也应该只允许您信任的人推送到您的存储库,但如果这里有一种方法可以防止用户错误,那就太好了。

在 git 中有解决这个问题的简单方法吗?

最佳答案

我们使用以下内容来防止意外的未知作者提交(例如,从客户的服务器或其他东西进行快速提交时)。它应该放在 .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": "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 - 防止人们使用不同的作者姓名推送 git commit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/117006/

相关文章:

python - Heroku:应用程序与 buildpack 不兼容:

git - 在另一个文件夹中删除和重新创建后保留文件的 Git 历史记录

git - 如何禁用 Microsoft.Build.Tasks.Git.LocateRepository

linux - 将本地 git 配置复制到 docker 容器中

java - IntelliJ Git 集成插件 -> "New Branch"将大写 F 放入 "feature/abc"

windows - 通过 cygwin 从 Windows 运行 git

Git:不为只读操作创建 index.lock

git - 从另一个功能分支创建新功能分支?

git - 你能改变 .git 文件夹的位置吗?

java - Intellij 想法,Git : Revert commit mechanism not working