5

I am trying to execute a simple git command using following python script.

#!/usr/bin/python

import commands
import subprocess
import os
import sys

pr = subprocess.Popen( "/usr/bin/git log" , cwd = os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()


print "Error : " + str(error) 
print "out : " + str(out)

but I am getting the following error even though I am running the python script in the same directory where git reposetory is.

Error : fatal: Not a git repository (or any of the parent directories): .git

I suspected the git might got correputed, but git files are fine and git commands work if I execute on normal command prompt.

I tried to search on net but couldn't get useful information. Please help it will be greatly appreciated.

1
  • What's wrong with code that wraps at 79 chars ? Commented Apr 8, 2014 at 13:36

2 Answers 2

5

The problem is your use of os.path.dirname():

os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' )

will give you:

>>> os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' )
'/ext/home/rakesh.kumar/workspace'

which, I bet, is not what you want.

Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

#!/usr/bin/python

import commands
import subprocess
import os
import sys

pr = subprocess.Popen( "/usr/bin/git log" , cwd = os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject/' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()


print "Error : " + str(error) 
print "out : " + str(out)

Directory path should have '/' at the end.

1 Comment

Note that this error can also occur if you have the GIT_DIR environment variable set (particularly to a relative path, as can happen in git hooks)

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.