Adr*_*ish 13 git hook pre-commit
我的计划是使用git来跟踪/ etc中的更改但是在提交时我希望让进行更改的人通过在命令行上添加--author选项将自己指定为作者.
所以我想以root身份停止意外提交.
我尝试创建这个预提交钩子但它不起作用 - 即使我在提交行上指定了作者,git var仍然会返回root.
AUTHOR=`git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/\1/p'`
if [ "$AUTHOR" == "root <root@localhost>" ];
then
echo "Please commit under your own user name instead of \"$AUTHOR\":"
echo 'git commit --author="Adrian"'
echo "or if your name is not already in logs use full ident"
echo 'git commit --author="Adrian Cornish <a@localhost>"'
exit 1
fi
exit 0
Run Code Online (Sandbox Code Playgroud)
Ric*_*sen 11
当前版本的Git不会--author通过环境变量,命令行参数或stdin 将信息提供给Git钩子.但是,--author您可以指示用户设置GIT_AUTHOR_NAME和GIT_AUTHOR_EMAIL环境变量,而不是要求使用命令行:
#!/bin/sh
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1
NAME=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p')
EMAIL=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p')
[ "${NAME}" != root ] && [ "${EMAIL}" != "root@localhost" ] || {
cat <<EOF >&2
Please commit under your own name and email instead of "${NAME} <${EMAIL}>":
GIT_AUTHOR_NAME="Your Name" GIT_AUTHOR_EMAIL="your@email.com" git commit
EOF
exit 1
}
Run Code Online (Sandbox Code Playgroud)
与--author参数一样,这些环境变量控制提交的作者.因为这些环境变量在Git的环境中,所以它们也在pre-commit钩子的环境中.而且因为它们处于pre-commit钩子的环境中,所以它们被传递到git var GIT_AUTHOR_IDENT使用它们的状态git commit.
不幸的是,设置这些变量比使用它更不方便--author.我建议联系Git开发人员,并--author在启动pre-commit钩子之前请求他们设置这些环境变量(使用传递的值).