1

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?

2
  • Can you provide a minimal reproducible example? Odds are you have quoting for the intermediate shell wrong and that you'll find the problem yourself. As a new user here, please also take the tour and read How to Ask. Commented Oct 16, 2019 at 18:22
  • @UlrichEckhardt I shortened the code in my example.Thank you for the links for tour and how to ask! Commented Oct 16, 2019 at 21:14

1 Answer 1

3

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.

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

Comments

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.