2

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.

3
  • 1
    The error TypeError: 'str' does not support the buffer interface is because your pw variable is a str it needs to be bytes. You can convert the str to bytes by pw.encode() But I dont think you can call a python function repo.git.fetch() from inside a shell. Commented May 26, 2015 at 21:03
  • Are you actually trying to use a Git+Python library here or just shelling out to git the CLI? Commented May 26, 2015 at 21:07
  • 1
    @JamesMills, I'll take either one, honestly. A little more digging says I should probably use git fetch <branch> followed by git 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 Commented May 27, 2015 at 20:57

1 Answer 1

1

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.

Generating ssh keys:

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

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

1 Comment

I think this is probably the answer, I've generated the key and am waiting for our repo administrator to add the key to the central server. Do you know if gitPython will allow something like repo.fetch(sshUrl)?

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.