使用预先提交钩子停止特定作者的git提交 [英] Stop a git commit by a specific author using pre-commit hook

查看:154
本文介绍了使用预先提交钩子停止特定作者的git提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划是使用git跟踪/ etc中的更改,但是当提交时,我希望让更改的人通过在命令行中添加--author选项来指定自己为作者。

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.

所以我想停止以root身份进行意外提交。

So I would like to stop accidental commits as root.

我试图创建这个预先提交的钩子,但它不工作 - git即使我在提交行指定了作者,var仍然会返回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.

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不要通过环境变量,命令行参数或stdin使Git钩子可用的 - author 信息。但是,不要求使用 - author 命令行,您可以指示用户设置 GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL 环境变量:

The current version of Git does not make --author information available to Git hooks via environment variables, command-line arguments, or stdin. 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:

#!/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 which使用它们就像 git commit 一样。

Like the --author argument, these environment variables control the commit's author. Because these environment variables are in Git's environment, they're also in the environment of the pre-commit hook. 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.

不幸的是,设置这些变量比使用 - 作者。我建议联系Git开发者,并在启动 pre-commit 之前请求他们设置这些环境变量(使用通过 - author 传递的值) / code>钩子。

Unfortunately, setting these variables is much less convenient than using --author. I suggest contacting the Git developers and requesting that they set these environment variables (using the value passed via --author) before launching the pre-commit hook.

这篇关于使用预先提交钩子停止特定作者的git提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆