git2_credentials icon indicating copy to clipboard operation
git2_credentials copied to clipboard

OSX Credentials Helper

Open manuschillerdev opened this issue 2 years ago • 1 comments

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)
}

manuschillerdev avatar Mar 25 '23 17:03 manuschillerdev

Good idea, but TBH

  • currently I don't have idea how to do it (and test it with CI if possible).
  • I don't know when I could work on it

Anyway PR is welcome.

davidB avatar Mar 29 '23 19:03 davidB