简体   繁体   English

使用预提交钩子停止特定作者的 git 提交

[英]Stop a git commit by a specific author using pre-commit hook

My plan is to use git to keep track of changes in /etc but when committing I want to have the person making the change specify themselves as author by adding the --author option on the commandline.我的计划是使用 git 来跟踪 /etc 中的更改,但是在提交时,我想让进行更改的人通过在命令行上添加 --author 选项来将自己指定为作者。

So I would like to stop accidental commits as root.所以我想停止作为 root 的意外提交。

I tried creating this pre-commit hook but it is not working - git var is still returning root even if I specify the author on commit line.我尝试创建这个预提交钩子,但它不起作用 - 即使我在提交行指定了作者,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

Until v1.7.10.1 (released 2012-05-01), Git did not make --author information available to Git hooks via environment variables, command-line arguments, or stdin.在 v1.7.10.1(2012-05-01 发布)之前,Git 没有通过环境变量、命令行参数或标准输入将--author信息提供给 Git 钩子。 However, instead of requiring the use of the --author command line, you can instruct users to set the GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables:然而,而不需要使用的--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
}

Like the --author argument, these environment variables control the commit's author.--author参数一样,这些环境变量控制提交的作者。 Because these environment variables are in Git's environment, they're also in the environment of the pre-commit hook.因为这些环境变量在 Git 的环境中,所以它们也在pre-commit钩子的环境中。 And because they're in the environment of the pre-commit hook, they're passed to git var GIT_AUTHOR_IDENT which uses them like git commit does.并且因为它们处于pre-commit钩子的环境中,所以它们被传递给git var GIT_AUTHOR_IDENT ,它像git commit一样使用它们。

Unfortunately, setting these variables is much less convenient than using --author .不幸的是,设置这些变量远不如使用--author方便。 If you can, upgrade to v1.7.10.1 or later.如果可以,请升级到 v1.7.10.1 或更高版本。

I used the following, adding this to the systems .bashrc.我使用了以下内容,将其添加到系统 .bashrc 中。

It won't catch folk who actually su to root and live in that shell, (bad!) But it does keep my logs useful when folk just use sudo.它不会抓住那些实际上 su 扎根并生活在那个 shell 中的人,(糟糕!)但是当人们只使用 sudo 时,它确实让我的日志有用。 I too am trying to keep a /etc changelog with git - so I can see what's been done month-by-month.我也试图用 git 保留一个 /etc 变更日志 - 这样我就可以看到每个月都做了什么。

#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-and-committing-as-root https://serverfault.com/questions/256754/correct-user-names-when-tracking-etc-in-git-repository-and-committing-as-root

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM