2

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?

2 Answers 2

4

You could try to set the git repository using a command-line switch:

proc = subprocess.Popen(['git', '--git-dir', '/home/git/repos/testing.git', 'log', '-n1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

--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:

import os
env = os.environ.copy()
env['GIT_DIR'] = '/home/git/repos/testing.git'
proc = subprocess.Popen((['git', 'log', '-n1', stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)

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:

import os
repo = '/home/git/repos/testing.git'
env = os.environ.copy()
env['GIT_DIR'] = repo
proc = subprocess.Popen((['git', 'log', '-n1', stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, cwd=repo)
Sign up to request clarification or add additional context in comments.

5 Comments

Hooks are run with the environment variable $GIT_DIR set already, so if you want to point git at a different directory, you have to override that. Note that in general the hook is run with GIT_DIR=. as well (i.e., it's relative, which was a surprise to me). Best to either clear it out entirely or set it to what you want though.
@torek: interesting, this may be what is wrong for the OP. Updated the answer, as env.update(os.environ) would have wiped the custom GIT_DIR var we are trying to set.
Oh, hah, I didn't read your second part of your answer closely enough to realize that you were using .update that way. Anyway, yes, I suspect that's the OP's real issue. (Experimentation tells me that --git-dir overrides $GIT_DIR so your first answer should work.)
Thank you very much, I didn't even expect any env vars here. Now, it only works with redefined 'GIT_DIR' variable AND specified 'cwd' option in Popen method. Please add it to your answer.
Thanks you very much. I've been trying some solutions but yours (in the edit section ) is the best and simplest that I've seen.
0

There is a comma missing after the cwd argument:

proc = subprocess.Popen(['git', 'log', '-n1'], cwd='/home/git/repos/testing.git', stdout=subprocess.PIPE, stderr=subprocess.PIPE)

1 Comment

That can hardly be it; that would give a SyntaxError, not the error the user saw. Prolly a transciption error when posting on SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.