有没有一种方法可以在提交时提示用户输入他们的作者信息?他们不会记得使用--author参数。
我尝试过以下几种方法,但都无法实现:
1. 使用客户端钩子来拒绝提交中包含“deploy”字符串的作者字符串。 2. 查找一个始终提示输入作者信息的设置。 3. 尝试根据用于登录到服务器的ssh密钥自动设置作者。
是否有一种方法能使其中之一起作用或者有更好的方式呢?
好的,我找到了一种方法来实现这个,但它仅适用于我们使用基于密钥的身份验证通过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
设置,我更愿意了解一下。
很遗憾,我也没有找到一个真正令人满意的解决方案。 我会列出我迄今为止找到的所有解决方案(我认为你在问题中已经提出了最好的三个解决方案)。
将以下脚本保存为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
钩子修改提交,请参见此处。
这是一种奇怪的黑客方式,具有丑陋的副作用(例如,在提交消息中显示的作者不正确)。
#!/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 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'
这是您自己的答案,为了更好地概述,将其复制到此处:
在共享部署用户的/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
文件。
我不是很确定对于你们来说它是如何工作的,但你们可以让开发人员设置一个别名,将--author
添加到每个普通提交中。