I'm running Gitolite over the Git repository and I have post-receive hook there written in Python. I need to execute "git" command at git repository directory. There are few lines of code:
proc = subprocess.Popen(['git', 'log', '-n1'], cwd='/home/git/repos/testing.git' stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()
After I make new commit and push to repository, scripts executes and says
fatal: Not a git repository: '.'
If I run
proc = subprocess.Popen(['pwd'], cwd='/home/git/repos/testing.git' stdout=subprocess.PIPE, stderr=subprocess.PIPE)
it says, as expected, correct path to git repository (/home/git/repos/testing.git)
If I run this script manually from bash, it works correct and show correct output of "git log". What I'm doing wrong?
There is a comma missing after the cwd argument:
You could try to set the git repository using a command-line switch:
--git-dir
needs to point to an actual git directory (.git
in a working tree). Note that for some commands you also need to set a--work-tree
option too.Another way to set the directory is using the
GIT_DIR
environment variable:Apparently hooks already set
GIT_DIR
but obviously this is incorrect for your case (it could be relative); the above code sets it to a full, explicit path.See the
git
manpage.Edit: Apparently it only works for the OP when specifying both a cwd and overriding the
GIT_DIR
var: