Skip to content

Commit

Permalink
use git2_credentials for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunarequest committed Jan 27, 2024
1 parent c358013 commit 08af739
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use super::{
};
use anyhow::{anyhow, Context, Result};
use dirs::{config_dir, home_dir};
use git2::{build::RepoBuilder, Cred, FetchOptions, RemoteCallbacks, Repository, StatusOptions};
use git2::{build::RepoBuilder, FetchOptions, RemoteCallbacks, Repository, StatusOptions};
use git2_credentials::CredentialHandler;
use owo_colors::{OwoColorize, Stream::Stdout, Style};
use serde_json::from_reader;
use std::{
Expand Down Expand Up @@ -97,18 +98,13 @@ pub fn clone(url: String, path: &Path) -> Result<()> {
let mut builder = RepoBuilder::new();
let mut callbacks = RemoteCallbacks::new();
let mut fetch_options = FetchOptions::new();
let config = git2::Config::open_default().context("unable to open gitconfig")?;
let mut ch = CredentialHandler::new(config);

// ssh
if url.starts_with("git@") {
callbacks.credentials(|_, _, _| {
let creds =
Cred::ssh_key_from_agent("git").expect("Could not create credentials object");
Ok(creds)
});
fetch_options.remote_callbacks(callbacks);
} else {
fetch_options.remote_callbacks(callbacks);
}
callbacks.credentials(move |url, username, allowed_types| {
ch.try_next_credential(url, username, allowed_types)
});
fetch_options.remote_callbacks(callbacks);

builder.fetch_options(fetch_options);
builder
Expand Down

0 comments on commit 08af739

Please sign in to comment.