哪个用户运行 git hook?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2626964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:13:54  来源:igfitidea点击:

What user runs the git hook?

githook

提问by atp

I have a post-update hook on my server, such that when I

我的服务器上有一个更新后挂钩,这样当我

git push

it does a pull on the live web directory. However, while the push always succeeds, the post-update hook sometimes fails.

它会拉动实时 Web 目录。然而,虽然推送总是成功,但更新后挂钩有时会失败。

The hook is pretty simple:

钩子很简单:

#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
cd /var/www
env -i git pull

I'm pushing updates from a variety of places, but sometimes I have to login as root on the server and manuall do a

我正在从多个地方推送更新,但有时我必须在服务器上以 root 身份登录并手动执行

env -i git pull

I only have to do it 20% of the time though. Any ideas why it would fail randomly? Also, how would I get it to log error messages, since it might be running as someone who can't write to the file system?

不过,我只需要在 20% 的时间里这样做。任何想法为什么它会随机失败?另外,我如何让它记录错误消息,因为它可能作为无法写入文件系统的人运行?

采纳答案by Cascabel

The hooks are run as the user doing the push. If you have some kind of pre-made setup, that may be a user like gitor gitosis, or it may be you. Just look at how you have the remote configured. (git remote show <remote-name>or just examine .git/config if you don't know) Presumably you're pushing via SSH, and there's a username@hostname in the URL.

钩子在用户执行推送时运行。如果您有某种预制设置,那可能是喜欢git或的用户gitosis,也可能是您。看看你是如何配置遥控器的。(git remote show <remote-name>或者,如果您不知道,请检查 .git/config)大概您是通过 SSH 推送的,并且 URL 中有一个 username@hostname。

P.S. It's pretty quick to demonstrate this - just clone a repo locally, throw a post-update hook in with an echo $USERor something similar, and try pushing as yourself or a different user (directly or through ssh).

PS 演示这一点非常快——只需在本地克隆一个 repo,使用echo $USER或类似的东西抛出一个更新后的钩子,然后尝试以你自己或其他用户的身份推送(直接或通过 ssh)。

回答by spuder

I decided to test this on my gitlab 6 server by creating a pre-receive hook and echoing out the user information

我决定在我的 gitlab 6 服务器上通过创建一个预接收钩子并回显用户信息来测试这个

$ cat /home/git/repositories/foo/foo.git/hooks/pre-recieve
#!/bin/bash
set -x
echo -e "The user the hook is run as is $USER"
echo -e "Just to doublecheck, the user is $(whoami)"
exit 1

It looks like it is run as the git user

看起来它是作为 git 用户运行的

$ git push 
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: + echo -e 'The user the hook is run as is'
remote: The user the hook is run as is
remote: ++ whoami
remote: + echo -e 'Just to doublecheck, the user is git'
remote: Just to doublecheck, the user is git
remote: + exit 1