git-api 0.1.1

git2-rs based utility library
Documentation
mod common;

use common::*;

use git2::{build::RepoBuilder, Error, FetchOptions, Repository};
use git_api::*;

fn clone(clean: bool) -> Result<(), Error> {
    let repo_url = format!("http://{DOMAIN}/{REPO}.git");
    let path = format!("{DIR}/{REPO}");
    if clean {
        std::fs::remove_dir_all(&path).unwrap_or_default();
    }

    let callbacks = auth_callbacks(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(())
}

#[test]
fn test() -> Result<(), Error> {
    clone(false).unwrap_or_default();
    let path = format!("{DIR}/{REPO}");
    let repo = Repository::open(&path)?;

    let remotes = repo_remotes(&repo)?;
    println!("remotes: {:?}", remotes);

    let repo_url = remotes[0].1.as_str(); // 远程仓库地址
    println!("repo_url: {:?}", repo_url);

    let refs = remote_refs(repo_url, USERNAME, PASSWORD)?;
    println!("refs: {:?}", refs);

    let default_branch = remote_default_branch(repo_url, USERNAME, PASSWORD)?;
    println!("default_branch: {:?}", default_branch);

    let branch_name = "main";
    let oid = repo_fetch(&repo, remotes[0].0.as_str(), branch_name, USERNAME, PASSWORD)?;
    println!("branch {} oid: {:?}", branch_name, oid);

    // repo_switch(&repo, remotes[0].0.as_str(), branch_name, true)?;
    repo_checkout(&repo, remotes[0].0.as_str(), branch_name, true)?;

    let branch = repo_current_branch(&repo)?;
    println!("branch name: {:?}", branch);

    Ok(())
}