我的计划是使用 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
有帮助吗?

解决方案

当前版本的 Git 不支持 --author 通过环境变量、命令行参数或标准输入可用于 Git 挂钩的信息。但是,不需要使用 --author 命令行,您可以指示用户设置 GIT_AUTHOR_NAMEGIT_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
}

--author 参数,这些环境变量控制提交的作者。因为这些环境变量在Git的环境中,所以它们也在 pre-commit 钩。而且因为他们处于这样的环境中 pre-commit 钩子,它们被传递到 git var GIT_AUTHOR_IDENT 它使用它们就像 git commit 做。

不幸的是,设置这些变量远不如使用方便 --author. 。我建议联系 Git 开发人员并请求他们设置这些环境变量(使用通过 --author)在启动之前 pre-commit 钩。

其他提示

我使用了以下内容,将其添加到系统中.bashrc。

它不会抓住实际上才能root并生活在那个贝壳中的民间,(坏!) 但是,当民间使用sudo时,它确实让我的日志很有用。 我也在试图用git保留一个/ etc changeLog - 所以我可以看到一个月的时间已经完成了什么。

#I want everyone to check in changes to /etc files, but also want their names even when they use sudo.
export GIT_COMMITTER_EMAIL=${USER}@ourcompany.co.nz
export GIT_AUTHOR_EMAIL=${USER}@ourcompany.co.nz
.

https://serverfault.com/questions/256754/correct-user-names-when-tracking-etc-in-git-repository-commiting-as-root

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top