2017-02-19 34 views
0

我想在這種情況下構建一個git鉤子post-receive,但它只在直接從shell調用時才起作用。gitPython不能從git鉤子工作

小例子腳本:

#! /bin/env python3 
import git 

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

導致以下錯誤時混帳叫:

$ 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倉庫另有說明。

回答

0

碰巧我剛剛找到了/ 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())