简体   繁体   English

Git post-receive hook无效

[英]Git post-receive hook not working

We're using git with a central repo (using Gitosis). 我们使用带有中央仓库的git(使用Gitosis)。 I've created a post-receive hook to generate an email to the dev mailing list whenever changes are pushed to the central repo, and to generate documentation from the documentation folder in the git repo. 我已经创建了一个post-receive挂钩,只要将更改推送到中央存储库,就会生成一封发送到dev邮件列表的电子邮件,并从git repo中的文档文件夹生成文档。

Therefore, in ~git/ I've got a directory, we'll call it 'a' that contains a clone of the git repo. 因此,在~git /我有一个目录,我们称之为'a',其中包含git repo的克隆。 The post-receive hook looks like: 后接收挂钩看起来像:

#!/bin/bash
cd ~git/repositories/a.git
. ~git/post-receive-email &> /dev/null
( cd ~git/a && git pull &> ~git/pull_log.log && php ~git/a/scripts/generate_markdown_documentation.php &> ~git/doc_log.log )

The email script is working, but the documentation generation is not. 电子邮件脚本正在运行,但文档生成却没有。 The content of pull_log.log is: pull_log.log的内容是:

fatal: Not a git repository: '.'

Which makes me think that it's not changing to the correct directory in line 5 of the above script. 这让我觉得它没有改变到上面脚本第5行的正确目录。 Am I wrong? 我错了吗? How can I get this to work? 我怎样才能让它发挥作用?

Edit: I've updated the post-receive hook as suggested in the responses. 编辑:我已按照回复中的建议更新了post-receive挂钩。 The script is now: 该脚本现在是:

#!/bin/bash
function die {
  echo "$*" >&2; exit 1
}

function checkgit {
  [ -d "$1/.git" ] || die "$1 could not possibly be a git repo; $1/.git is not a dir"
}


cd ~git/repositories/a.git
. ~git/post-receive-email &> /dev/null
( set -x
checkgit ~git/a
cd ~git/a
checkgit .
pwd
git pull
php ~git/a/scripts/generate_markdown_documentation.php )

And I get the following output from git push: 我从git push得到以下输出:

+ checkgit /var/git/a
+ '[' -d /var/git/a/.git ']'
+ cd /var/git/a
+ checkgit .
+ '[' -d ./.git ']'
+ pwd
/var/git/a
+ git pull
fatal: Not a git repository: '.'
+ php /var/git/a/scripts/generate_markdown_documentation.php

Any more help? 还有什么帮助吗?

Oh, and if I run the script myself, it works (I run it by saying hooks/post-receive) 哦,如果我自己运行脚本,它可以工作(我通过说钩子/后接收来运行它)

Discovered the problem, thanks to serverfault -- basically, environment variables GIT_DIR and GIT_WORK_TREE are set when the hook runs, and these affect git pull adversely. 发现问题,多亏了serverfault - 基本上,当钩子运行时设置环境变量GIT_DIRGIT_WORK_TREE ,这些会对git pull造成不利影响。 Unsetting the variables fixes the problem. 取消设置变量可以解决问题。

You need more diagnostics, eg, 您需要更多诊断,例如,

function die {
  echo "$*" >&2; exit 1
}

function checkgit {
  [ -d "$1/.git" ] || die "$1 could not possibly be a git repo; $1/.git is not a dir"
}

At this point, in the subshell right after the parenthesis, you can try stuff like 此时,在括号后面的子shell中,您可以尝试类似的东西

set -x # show exactly what's executed (writes to stderr)
checkgit ~git/a
cd ~git/a && checkgit . && git pull ...

You might also consider redirecting the whole stderr of the subshell, eg, 您也可以考虑重定向子shell的整个stderr,例如,

( ... ) 2>/tmp/mydiagnosis$$.log

(This is a temporary measure and is OK only if there's no confidential info in the logs.) (这是一种临时措施,只有在日志中没有机密信息时才可以。)


OK Silas, your additional info rules out a lot of awkward possibilities. OK Silas,您的其他信息排除了许多尴尬的可能性。 I'm nearing the end of my git fu, but here are some more things to try: 我即将结束我的git fu,但还有一些事情需要尝试:

  1. Go into ~git/a and see if you can make it git pull by hand. 进入~git/a ,看看你是否可以手动git pull This should fail. 这应该失败。
  2. Got into ~git/a and run git status . 进入~git/a并运行git status This should also fail. 这也应该失败。 If it doesn't, then git is giving you a very bad error message. 如果没有,那么git会给你一个非常糟糕的错误信息。

If both steps fail, ~git/a is not the clone you thought it was. 如果两个步骤都失败了, ~git/a就不是您认为的克隆。 Rename it, make a new clone, and see if you can get the problem to persist. 重命名它,创建一个新的克隆,看看是否可以让问题持续存在。

If the first step succeeds by hand, then something strange is going on and I'm baffled. 如果第一步是手工成功,那么一些奇怪的事情正在发生,我感到困惑。

If the first step fails but the second succeeds, you may have a problem with branches: 如果第一步失败但第二步成功,则分支可能有问题:

  • Perhaps repo ~git/a is set to the wrong branch, and your repo needs a branch it doesn't have. 也许repo ~git/a设置为错误的分支,你的repo需要一个它没有的分支。 Try git branch -a and see if you see something unexpected. 试试git branch -a ,看看你是否看到了意想不到的东西。

  • Perhaps you have the branch, but it's not properly associated with a remote repository. 也许您有分支,但它与远程存储库没有正确关联。 At this point you have to dive into ~git/a/.git/config , and I don't really know how to explain what you should expect to find there. 此时你必须深入~git/a/.git/config ,我真的不知道如何解释你应该在那里找到什么。 At that point you will need a real git expert; 那时你需要一个真正的 git专家; I just play one on TV. 我只是在电视上播放一个。

unset GIT_DIR is a solution which works for the fatal error you are seeing. unset GIT_DIR是一个适用于您所看到的致命错误的解决方案。

This applies to all scripts in hooks (post-update is another common one), which uses the git command inside it. 这适用于挂钩中的所有脚本(更新后是另一个常见脚本),它在其中使用git命令。 git command uses the GIT_DIR from env instead of pwd. git命令使用env中的GIT_DIR而不是pwd。

See https://stackoverflow.com/a/4100577 for further explanation. 有关详细说明,请参阅https://stackoverflow.com/a/4100577

I recently ran into a similar problem and I think it is related to the environment variables that git sets, specifically the $GIT_DIR variable. 我最近遇到了类似的问题,我认为它与git设置的环境变量有关,特别是$ GIT_DIR变量。 If you have that set, all git commands on other repos start to act weirdly. 如果你有这个设置,其他repos上的所有git命令都会开始变得非常奇怪。 Basically I think that running your git pull inside the hook must be invoked in a neutral shell environment that does not have those odd variables and leads to git confusion, though I haven't figured out how to do that just yet. 基本上我认为在钩子里运行你的git pull必须在没有那些奇怪变量的中性shell环境中调用并导致git混乱,尽管我还没想出如何做到这一点。

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

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