我们的团队有98%的时间在本地开发框中提交并推送提交,在这种情况下,提交的作者信息是正确的。但是,在高压力下,有人会直接在开发服务器上调整某些内容并从那里提交。发生这种情况时,提交中的作者信息默认为共享部署帐户。
有没有办法在提交时提示用户提供作者信息?他们不会记得做--author
。
我看过的一些事情,但无法完成终点线:
有没有办法让其中一个工作或一个好方法去做?
答案 0 :(得分: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
设置。
答案 1 :(得分:0)
我不太确定它对你们有什么用,但是你可以让你的开发者设置一个别名来为每次正常提交添加--author
。
答案 2 :(得分:0)
不幸的是,我也找不到令人满意的解决方案。我将列出我到目前为止找到的所有解决方案(我认为你已经在你的问题中提出了最好的3个)。
将以下脚本保存为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提交。可能也有办法让这项工作
使用钩子设置作者的唯一方法似乎是使用post-commit
hook see also here来修改提交。
这是一个带有丑陋副作用的奇怪黑客(例如提交消息中的作者显示错误)。
#!/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
您可以创建脚本git-commit.sh
或git别名git commit-author
,提示您使用作者,然后致电git commit --author=<user's selection>
。不幸的是,not possible to overwrite git commit
。
git-commit.sh
中的/usr/local/bin
脚本可能如下所示:
#!/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"
将脚本添加为别名run:
$ git config alias.commit-author '!git-commit.sh'
这是您自己的答案,在此处复制以获得更好的概述:
在共享部署用户的/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
文件。