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.

有帮助吗?

解决方案

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.

其他提示

There are two changes to be made in the code to work . Changes are git commands inside Popen and working directory. After making the changes the code can be as given below which will work without any error

pr = subprocess.Popen(['git', 'diff', 'HEAD~1' ,'HEAD'] , cwd = os.getcwd(), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()
print "Error : " + str(error) 
print "out : " + str(out)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top