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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With