【发布时间】:2026-02-06 13:45:01
【问题描述】:
我正在构建一个预接收挂钩来检查作者的姓名和电子邮件地址。
我可以轻松检查提交的名称/电子邮件地址是否使用,
while read oldsha newsha refname; do
authorEmail=$(git log -1 --pretty=format:%ae $newsha)
if [[ $(grep -w $authorEmail ~/.ssh/authorized_keys | wc -w) -gt 1 ]]; then
echo "Author Email: $authorEmail"
exit 0
else
echo "Unauthorized Email"
exit 1
fi
done
但不是简单地拒绝它,我想用正确的电子邮件替换它。 我已经这样设置了authorized_keys,
environment="UserEmail=user@hostA" ssh-rsa AAAAAA... user@hostA
所以我想做一些类似的事情
if [[ ... ]];
then
echo "Author Email: $authorEmail"
exit 0
else
echo "Unauthorized Email detected"
echo "Replacing email with: $UserEmail"
git commit --amend --author "something <$UserEmail>"
exit 0
fi
但正如预期的那样, git commit --amend .. 会抛出一个错误,这应该在工作树中使用。 有什么方法可以在预接收或更新挂钩中完成此操作?
【问题讨论】:
标签: git bash githooks git-bash