如何要求(或敦促)git提交者提供相关的作者信息?

8
99%的时间,我们的开发团队都会在本地开发环境上提交代码,这样提交记录中的作者信息是正确的。但在高压情况下,有些人会直接在开发服务器上进行修改并从那里提交代码。这种情况下,提交记录中的作者信息将默认为共享部署帐户。
有没有一种方法可以在提交时提示用户输入他们的作者信息?他们不会记得使用--author参数。
我尝试过以下几种方法,但都无法实现:
1. 使用客户端钩子来拒绝提交中包含“deploy”字符串的作者字符串。 2. 查找一个始终提示输入作者信息的设置。 3. 尝试根据用于登录到服务器的ssh密钥自动设置作者。
是否有一种方法能使其中之一起作用或者有更好的方式呢?

2
你的团队讨论过禁止在开发服务器上直接编辑代码吗? - Aaron Kurtzhals
1
肯定的。我想这种情况会在未来发生,但现在只有在“紧急情况”下才会发生,而且要采取措施完全防止它并不是我作为“新人”能够做到的。 - Ry4an Brase
不要寻找技术解决方案。惩罚那些违反公司开发规范的员工。 - user229044
@meagar 我同意,但有一个折中方案。如果我在这份新工作中工作了三个星期,我的同事(他们不是“我的”员工)不能像以前那样承诺(非常少,只在“紧急情况”下),因为“新人”设置了一个阻止它的钩子,你肯定明白这是次优的。首先要进行温和的教育,需要知道谁在做什么以及多久 - 这就是为什么这个问题问如何设置良好的作者信息而不是防止提交(这很容易)。 - Ry4an Brase
3个回答

6

好的,我找到了一种方法来实现这个,但它仅适用于我们使用基于密钥的身份验证通过ssh独占访问此框的情况。

在共享部署用户的/home/deploy/.ssh/authorized_keys文件中,我为每个密钥添加了一个环境变量,因此我的密钥看起来像这样:

environment="GIT_AUTHOR_EMAIL=ry4an@host.com",environment="GIT_AUTHOR_NAME=Ry4an on Dev" ssh-rsa AAAA...cXBcmHr ry4an@host.com

还需要添加以下内容:

PermitUserEnvironment yes

将文本添加到/etc/ssh/sshd_config文件中。

虽然可以这样做,但如果存在user.name=ASK设置,我更愿意了解一下。


0

很遗憾,我也没有找到一个真正令人满意的解决方案。 我会列出我迄今为止找到的所有解决方案(我认为你在问题中已经提出了最好的三个解决方案)。

(1)禁止未使用--author提交

将以下脚本保存为pre-commit钩子,放在.git/hooks/pre-commit中以禁止提交:

#!/bin/sh

if echo $GIT_AUTHOR_NAME | grep -q "XXX" 2> /dev/null; then
    echo 'You tried to commit as XXX, please use  git commit --author="your name".'
    return 1
fi

将用户名设置为XXX:

git config user.name XXX

警告:此方法仅适用于普通提交。合并提交将被提交为XXX。可能有一种方法也可以使其工作。

(2a) 在提交之前提示作者信息(使用钩子)

使用钩子设置作者的唯一方法似乎是使用post-commit钩子修改提交,请参见此处

这是一种奇怪的黑客方式,具有丑陋的副作用(例如,在提交消息中显示的作者不正确)。

#!/bin/sh

# env variable to prevent recursion
test -z $AMMEND_COMMIT || exit 0
export AMMEND_COMMIT=1

exec < /dev/tty
echo -n "Author for previous commit: "
read author

git commit -q --no-edit --amend --author "$author"

使用以下脚本根据最近的提交获取作者建议:
#!/bin/sh

# TODO this doesn't work for repos with <15 commits
NUM_LAST_COMMITS=15  # how many commits in the past to look for authors

# env variable to prevent recursion
test -z $ammend_commit || exit 0
export ammend_commit=1

exec < /dev/tty

last_authors=$(git shortlog -s -e HEAD~${NUM_LAST_COMMITS}..HEAD|sed 's/^\W*[0-9]*\W*//g')
echo "Select an author from list or enter an author:"
echo
echo "$last_authors" | awk '{print "    [" NR "] " $s }'
echo
echo -n "Enter number or author: "
read author

if echo "$author" | egrep -q '^[0-9]+$'; then
    author=$(echo "$last_authors" | sed "${author}q;d")
fi

git commit -q --no-edit --amend --author "$author"

看起来像这样:
$ git commit -am "My message"
Select an author from list or enter an author:

    [1] First Author <first_author@domain.com>
    [2] Second Author <second_author@domain.com>
    [3] Third Author <third_author@domain.com>

Enter number or author: 3

(2b) 在提交之前提示作者信息(使用自定义脚本或别名)

您可以创建一个名为git-commit.sh的脚本或者一个名为git commit-author的Git别名,该脚本或别名会提示您输入要使用的作者信息,然后调用git commit --author=<用户选择的信息>。不幸的是,无法覆盖git commit

/usr/local/bin目录下创建一个git-commit.sh脚本,内容如下:

#!/bin/sh

# TODO this doesn't work for repos with <15 commits
NUM_LAST_COMMITS=15  # how many commits in the past to look for authors

last_authors=$(git shortlog -s -e HEAD~${NUM_LAST_COMMITS}..HEAD|sed 's/^\W*[0-9]*\W*//g')
echo "Select an author from list or enter an author:"
echo
echo "$last_authors" | awk '{print "    [" NR "] " $s }'
echo
echo -n "Enter number or author: "
read author

if echo "$author" | egrep -q '^[0-9]+$'; then
    author=$(echo "$last_authors" | sed "${author}q;d")
fi

git commit "$@" --author "$author"

要将脚本添加为别名,请运行:

$ git config alias.commit-author '!git-commit.sh'

(3) 根据用于登录的ssh密钥自动设置作者

这是您自己的答案,为了更好地概述,将其复制到此处:

在共享部署用户的/home/deploy/.ssh/authorized_keys文件中,我为每个密钥添加了一个环境变量,因此我的密钥看起来像这样:

environment="GIT_AUTHOR_EMAIL=ry4an@host.com",environment="GIT_AUTHOR_NAME=Ry4an on Dev" ssh-rsa AAAA...cXBcmHr ry4an@host.com

这还需要添加:

PermitUserEnvironment yes

/etc/ssh/sshd_config 文件。


0

我不是很确定对于你们来说它是如何工作的,但你们可以让开发人员设置一个别名,将--author添加到每个普通提交中。


这是一个共享账户,因此别名对于每个人都是相同的。 - Ry4an Brase
啊,好的,我的错,我没有完全理解。 - edhedges

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接