Created
July 16, 2023 02:51
-
-
Save dylanbstorey/7e1d43eb84b30ead1e17d68852351804 to your computer and use it in GitHub Desktop.
angreal/private cloning gist + test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use git2::{Cred, RemoteCallbacks, Repository}; | |
use git2; | |
use git2_credentials::CredentialHandler; | |
use tempfile; | |
use std::path::Path; | |
/// works after running ╰─❯ ssh-keygen -t ed25519 -C "dylan.storey@gmail.com.com" | |
/// and uploading to github/gitlab | |
fn main() { | |
// let repo_url = "git@github.com:angreal/private_test_template.git"; | |
// let repo_url = "https://gitlab.com/angreal/test_clone"; | |
let repo_url = "git@gitlab.com:angreal/test_clone.git"; | |
// May have to add to known hosts with a manual git clone the first time you use it | |
let target_path = "here"; | |
let mut cb = git2::RemoteCallbacks::new(); | |
let git_config = git2::Config::open_default().unwrap(); | |
// let mut entries = git_config.entries(None).unwrap(); | |
// while let Some(entry) = entries.next() { | |
// let entry = entry.unwrap(); | |
// println!("{} => {}", entry.name().unwrap(), entry.value().unwrap()); | |
// } | |
let mut ch = CredentialHandler::new(git_config); | |
cb.credentials(move |url, username, allowed| ch.try_next_credential(url, username, allowed)); | |
// clone a repository | |
let mut fo = git2::FetchOptions::new(); | |
fo.remote_callbacks(cb) | |
.download_tags(git2::AutotagOption::All) | |
.update_fetchhead(true); | |
let dst = tempfile::tempdir().unwrap(); | |
std::fs::create_dir_all(&dst.as_ref()).unwrap(); | |
git2::build::RepoBuilder::new() | |
.branch("main") | |
.fetch_options(fo) | |
.clone(repo_url, Path::new("here")).unwrap(); | |
println!("{:#?}",dst); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment