Search code examples
pythongitgit-diff

how do I output git diff to a text file using python


I am trying to make a command for git to directly email the diff,

what I have thought is to get the diff to a text file to email,

but not sure why

pr = subprocess.Popen( "git diff HEAD^ HEAD" , cwd = os.path.dirname( os.getcwd() ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()
print "Error : " + str(error) 
print "out : " + str(out)

gives error saying

Error : error: Could not access 'HEAD^'

out : 

whereas I want diff in out variable to email.


Solution

  • You are probably not issuing the command in correct folder, where git is initialized.

    Your actual problem is, that os.path.dirname command is striping out the folder from the command os.getcwd and you are actually running the command in parent folder. If you get rid of os.path.dirname and just use os.getcwd your code should work.