1

I'm currently using a Python program to make automated commits to a repository after it makes some edits to a file. I am using subprocess.Popen to make a child process that calls the git binary. I though I could just use stdin=PIPE when creating the child process and be able to write my user credentials when prompted by the command line, but for some reason this isn't happening. The code looks like the following:

proc = Popen(["git","push","origin","master"], stdin=PIPE)
proc.communicate("username")
proc.communicate("password")

What's happening right now is that it's calling the binary but still showing me the command line prompt. I've taken a look at some other questions on here, but they are all using essentially the same code I am, so I'm a little stumped as to the problem. I know there are plenty of other ways of achieving this result, like caching my credentials, but I'm curious to see if this way can work for a number of reasons.

8
  • Like I said, I know there are other ways of doing this. I just want to know if this way will work for a number of project specific reasons. Commented Feb 23, 2016 at 22:10
  • 1
    You cannot use communicate twice, write to stdin Commented Feb 23, 2016 at 22:10
  • Using write gives me an I/O error for some reason. Commented Feb 23, 2016 at 22:12
  • 1
    While this does not answer Your question, I could mention that module that handles GIT from Python has already been written here You might want to use it instead. Commented Feb 23, 2016 at 22:17
  • Do you need both username and password or just password? When I push or pull I just need to enter my password. Commented Feb 23, 2016 at 22:22

1 Answer 1

1
proc = Popen(["git","push","origin","master"], stdin=PIPE,stdout=PIPE,stderr=PIPE)
time.sleep(1)
proc.stdin.write("username")
time.sleep(1)
proc.stdin.write("password")
#or maybe
for letter in "password":
    proc.stdin.write(letter)
    time.sleep(0.1)
print proc.communicate()

something like that i guess ...

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

4 Comments

Doesn't seem to be working exactly. It writes the username out to the screen, but git never prompts for the password after that and the program just exits.
hmmm well I would reccomend switching to keyauthentication anyway... at a guess its programmed into git to want one character at a time or something and sending the whole password at once breaks it ... if its too hard you might want to just use pygit2.org/remotes.html#credentials
Yeah, I think you're right. alternatively do you know if it's possible to specify username and password in the same line as "git push origin master". I know git cred store maintains creds as "username:[email protected]".
depends on which git client i think ... afaik tortoise git lets you do that somehow ... you might just want to use pygit pygit2.org/remotes.html#credentials

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.