I am trying to write a script (probably python) that needs to fetch a remote repo (housed with Stash via git), and checkout a specific commit (based on the hash). The problem is that this needs to happen 'blindly' to the user, but it pauses for a password. I need to figure out a way to pipe (or proc.communicate()
(or something) the password to the repo.fetch()
or origin.update()
proc.
Currently, I have code that's something like this:
remoteUrl = 'http://uname@build:7990'
commitId = '9a5af460615'
pw = 'MyPassword'
repo = git.Repo('C:\\Myfolder\\MyRepo')
proc = subprocess.Popen('echo pw | repo.git.fetch()', shell=True, stdin = PIPE)
but it doesn't seem to work
if I done try the echo/pipe, but follow repo.git.fetch()
with proc.communicate(pw)
, I get error:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Program Files (x86)\Python34\lib\subprocess.py", line 941, in communicate
self.stdin.write(input)
TypeError: 'str' does not support the buffer interface
Finally, I've also tried adding:
o = repo.remotes.origin
proc = subprocess.Popen('o.update()', shell=True, stdin = PIPE)
proc.communicate(pw)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Program Files (x86)\Python34\lib\subprocess.py", line 941, in communicate
self.stdin.write(input)
TypeError: 'str' does not support the buffer interface
But to no avail, as you can see from the error.
I think I'm over-complicating this, as it seems gitpython probably has a good way to send the pw to o.update()
or repo.git.fetch()
without using the subprocess
?
EDIT: I'm hoping for code something along these lines:
remoteUrl = 'http://uname@build:7990'
commitId = '9a5af460615'
pw = 'MyPassword'
repo = git.Repo('C:\\Myfolder\\MyRepo')
repo.fetch(remoteUrl)
repo.checkout(commitId) # or maybe repo.pull, or something?
That is more pseudocode than anything else, but it may help you see what I'm hoping for. Also, I want to force through any 'hiccups' I don't want detached head warnings or anything, I want to completely replace the local working copy with the remote at the specified commit.
Instead of using http use ssh.
Once you set up your ssh keys the authentication is done "silently" with the ssh keys and there will be no need to enter password ever again.
ssh-keygen -t rsa -C 'email@address'
Once you create your keys add them to the central machine or to your central git software and update your fetch url to clone using ssh instead of http