enaut
enaut

Reputation: 441

gitPython not working from git hook

I am trying to build a git hook in this case post-receive however it only works when called directly from shell.

A minimal example script:

#! /bin/env python3
import git

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

results in the following error when called by 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

I don't understand why it is opening the git repository at '.' as I clearly stated otherwise.

Upvotes: 1

Views: 555

Answers (1)

enaut
enaut

Reputation: 441

As it happens I just found the/a solution.

The gitPython wrapper does not ignore environment variables. So even though I opened the git repository with a specified directory it is somehow still trying to use the directory specified in GIT_DIR='.'. So in my case I had to delete that variable with del os.environ['GIT_DIR'] and then do the stuff as before. The full sample script is at:

#! /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())

Upvotes: 2

Related Questions