当前位置:  开发笔记 > 编程语言 > 正文

阻止人们使用不同的作者名称推送git提交?

如何解决《阻止人们使用不同的作者名称推送git提交?》经验,为你挑选了2个好方法。

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

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

我知道,有些人可能会推动其他人做出的承诺,这很复杂.当然,您也应该只允许您信任的人推送到您的存储库,但如果有一种方法可以防止用户出错,那将会很棒.

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



1> dsvensson..:

我们使用以下内容来防止意外的未知作者提交(例如,当从客户的服务器或其他东西快速提交时).它应放在.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")


此脚本仅检查第一个更新的ref.人们可以通过这样做来悄悄地做出糟糕的提交:git push origin goodbranch:foo evilbranch:master

2> Anders Walde..:

使用PRE-RECEIVE挂钩(有关详细信息,请参阅githooks(5)).每次更新时,你会得到旧的沙子和新的沙子.并且可以轻松列出更改并检查它们是否具有合适的作者(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$
# ^allowed@example.com$
#
# 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

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有