pythongitauthenticationdulwich

Dulwich Remote Repo Authentication


Are there any good examples out there for accessing and fetching a remote repository over HTTPS? I have a Git repository that I can clone from the command line with my username and password, but I want to be able to do this using Dulwich and just provide my username and password when I make my HTTPS client.

Another requirement is that this be done using the MemoryRepo option rather than writing to the file system.


Solution

  • Dulwich 0.16.0 and later support usernames/paswords in URLs.

    In older versions, you can do this by specifying a custom HTTP Handler:

        import urllib2
    
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    
        # Add the username and password.
        password_mgr.add_password(realm, top_level_url, username, password)
    
        handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    
        opener = urllib2.build_opener([handler])
    
        client, path = get_transport_and_path(remote_location)
        client.opener = opener
        remote_refs = client.fetch(path, target_repo)