RD_66
RD_66

Reputation: 11

Python subprocess git commit message only accepting single string

Using python subprocess to commit changes to a git branch, the -m message will only accept single string

I am using python 3 to loop through a directory to commit changes for all xml files. I have tried using double quotes and single quotes for the message, neither have worked. I also tried using f and r and both failed.

add=subprocess.Popen([gitPath,'git add .'],cwd=cd,shell=True,stdout=subprocess.PIPE)
subprocess.Popen.wait(add)

commit=subprocess.Popen([gitPath,f"git commit -m 'no message spacing works'"],cwd=cd,shell=True,stdout=subprocess.PIPE)

subprocess.Popen.wait(commit)

push=subprocess.Popen([gitPath,'git push origin DEV:PyTesting'],cwd=cd,shell=True,stdout=subprocess.PIPE)
subprocess.Popen.wait(push)

I would like to be able to use regular text strings, including spaces, to put in the input commit message. When I use "no message spacing works" in the script the errors are error: pathspec 'message' did not match any file(s) known to git error: pathspec 'spacing' did not match any file(s) known to git error: pathspec 'works'' did not match any file(s) known to git

If I use git commit -m 'no message spacing works' in bash, it is accepted. What am I missing in the script to allow multiple strings?

Upvotes: 1

Views: 1348

Answers (1)

Daniel Junglas
Daniel Junglas

Reputation: 5940

I think you are passing arguments to Popen the wrong way. If you pass a list then each argument should be a separate list element. That way you don't even have to bother with quotes. So git commit -m 'no message spacing works' should be passed as ['git', 'commit', '-m', 'no message spacing works']. See also the examples in the reference documentation of Popen.

Upvotes: 3

Related Questions