git-api 0.1.1

git2-rs based utility library
Documentation
mod common;

use git2::{build::RepoBuilder, Cred, Error, FetchOptions, RemoteCallbacks, Repository};
use ttycolor::ColorTrait;

use common::*;

#[test]
fn clone_url() -> Result<(), Error> {
    let url = format!("http://{USERNAME}:{PASSWORD}@{DOMAIN}/{REPO}.git");
    let path = format!("{DIR}/{REPO}");
    macro_log::d!("Clone {} to {}", url.red(), path.bg_green());
    std::fs::remove_dir_all(&path).unwrap_or_default();
    Repository::clone(&url, path)?;
    Ok(())
}

#[test]
fn clone_by_auth() -> Result<(), Error> {
    let repo_url = format!("http://{DOMAIN}/{REPO}.git");
    let path = format!("{DIR}/{REPO}");
    std::fs::remove_dir_all(&path).unwrap_or_default();

    let mut callbacks = RemoteCallbacks::new();
    callbacks.credentials(|_url, _username_from_url, _allowed_types| {
        Cred::userpass_plaintext(USERNAME, PASSWORD)
    });

    let mut opts: FetchOptions<'_> = FetchOptions::new();
    opts.remote_callbacks(callbacks);

    let mut builder = RepoBuilder::new();
    builder.fetch_options(opts);

    let _repo = builder.clone(&repo_url, std::path::Path::new(&path))?;
    Ok(())
}