davidB / git2_credentials

Provide credentials function to used with git2::RemoteCallbacks.credentials
Apache License 2.0
21 stars 4 forks source link

OSX Credentials Helper #39

Open manuschillerdev opened 1 year ago

manuschillerdev commented 1 year ago

There is one git host I need to conect to, which does not allow ssh connections. I use https + username + access tokens instead.

My local git-cli is configured to use OSXKeychain:

git config credential.helper
> osxkeychain

when I use a regular git clone git asks me for the credentials on the very first connection, but stores the credentials in OSXKeychain for any subsequent commands (survives restarts as well).

When using git2_credentials + git2 I can clone repos, but it always asks for the credentials.

Would it be possible that git2_credentials uses OSXKeychain as well?

My code is very close to your official example :)

use std::path::PathBuf;

use git2::Repository;
use git2_credentials::CredentialHandler;

pub fn clone_with_auth(repo_url: &str, target_path: &PathBuf) -> Result<Repository, git2::Error> {
    let mut remote_callback = git2::RemoteCallbacks::new();
    let git_config = git2::Config::open_default()?;

    let mut credential_handler = CredentialHandler::new(git_config);
    remote_callback.credentials(move |url, username, allowed| {
        credential_handler.try_next_credential(url, username, allowed)
    });

    let mut fetch_options = git2::FetchOptions::new();
    fetch_options.remote_callbacks(remote_callback);

    git2::build::RepoBuilder::new()
        .fetch_options(fetch_options)
        .clone(&repo_url, &target_path)
}
davidB commented 1 year ago

Good idea, but TBH

Anyway PR is welcome.