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.
TypeError: 'str' does not support the buffer interfaceis because yourpwvariable is astrit needs to bebytes. You can convert thestrtobytesbypw.encode()But I dont think you can call a python functionrepo.git.fetch()from inside a shell.gitthe CLI?git fetch <branch>followed bygit reset --hard <commitId>. The big idea is that I want to have a script that will force the local working copy to be exactly like a particular commit