gitPython 无法通过 git 钩子工作

gitPython not working from git hook

在这种情况下,我正在尝试构建一个 git 挂钩 post-receive,但它仅在直接从 shell.

调用时有效

一个最小的示例脚本:

#! /bin/env python3
import git

rep = git.Repo("/var/www/cgi-bin/deployed.everland")
print(rep.git.status())

在 git 调用时导致以下错误:

$ git push --tags
Total 0 (delta 0), reused 0 (delta 0)
remote: Traceback (most recent call last):
remote:   File "hooks/post-receive", line 6, in <module>
remote:     print(rep.git.status())
remote:   File "/usr/lib/python3.5/site-packages/git/cmd.py", line 424, in <lambda>
remote:     return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
remote:   File "/usr/lib/python3.5/site-packages/git/cmd.py", line 873, in _call_process
remote:     return self.execute(call, **_kwargs)
remote:   File "/usr/lib/python3.5/site-packages/git/cmd.py", line 687, in execute
remote:     raise GitCommandError(command, status, stderr_value, stdout_value)
remote: git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
remote:   cmdline: git status
remote:   stderr: 'fatal: Not a git repository: '.''
To host:testrepos
 * [new tag]         newtag -> newtag

我不明白为什么它在 '.' 打开 git 存储库,因为我已经明确说明了。

碰巧我刚找到 the/a 解决方案。

gitPython 包装器不会忽略环境变量。因此,即使我打开了带有指定目录的 git 存储库,它仍然以某种方式尝试使用 GIT_DIR='.' 中指定的目录。所以在我的例子中,我不得不用 del os.environ['GIT_DIR'] 删除那个变量,然后像以前一样做这些事情。完整的示例脚本位于:

#! /bin/env python3
import os
if 'GIT_DIR' in os.environ:
    del os.environ['GIT_DIR']

import git

rep = git.Repo("/var/www/cgi-bin/deployed.everland")
print(rep.git.status())