3

I'm trying to clone a repository from stash using an ssh link. I get an error saying authentication is required, This should not require a username and password. How do I fix this error?

from pygit2 import *
repo_name = "ssh://git@stash:TheProject"
clone_repository(self.repo_name, self.repo_dir, credentials=cred)

Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__ return self.func(*args)
File "rio_telem_tool.py", line 143, in build clone_repository(self.repo_name, self.repo_dir)
File "/usr/local/lib/python2.7/dist-packages/pygit2/__init__.py", line 265, in clone_repository check_error(err)
File "/usr/local/lib/python2.7/dist-packages/pygit2/errors.py", line 56, in check_error raise GitError(message)
GitError: authentication required but no callback set
  • Can you successfully clone that repository from the command line using `git clone` without being prompted for a password? – larsks Jun 08 '15 at 19:30
  • ssh always requires a username and password. You're giving the username in the url itself. git spawns the ssh command which will look at the configuration and ssh agents, whereas libgit2 will not, you need to tell it exactly which authentication you want to use. – Carlos Martín Nieto Jun 20 '15 at 09:51

1 Answers1

2

This is an old question, but I recently had two issues to solve:

  1. How to give pygit2 the username and git token to connect to github
  2. Since this was a completely internal application, how to skip the certificate check that was blocking the connection to github

The solution to both involved subclassing pygit2.RemoteCallbacks and calling it like this:

 pygit2.clone_repository(your_args, callbacks=self.MyRemoteCallBacks(user,token)

The class:

class MyRemoteCallbacks(pygit2.RemoteCallbacks):
        def __init__(self, user, token):
            self.user = user
            self.token = token
        def credentials(self, url, username_from_url, allowed_types):
            return pygit2.UserPass(self.user,self.token)
        def certificate_check(self, certificate, valid, host):
            return True