git-api 0.1.1

git2-rs based utility library
Documentation
use git2::{Error, FetchOptions, Oid, Repository};

use crate::auth::auth_callbacks;

/// 获取远程分支(fetch)
///
/// 返回值:远程分支Oid
pub fn repo_fetch(
    repo: &Repository,
    remote_name: &str,
    branch_name: &str,
    username: &str,
    password: &str,
) -> Result<Oid, Error> {
    let mut remote = repo.find_remote(remote_name)?;

    let mut fetch_options = FetchOptions::new();
    let callbacks = auth_callbacks(username, password);
    fetch_options.remote_callbacks(callbacks);

    // Update remote-tracking branches
    // remote.fetch(&[branch_name], Some(&mut fetch_options), None)?;
    remote.fetch(
        &[&format!("refs/heads/{branch_name}")],
        Some(&mut fetch_options),
        None,
    )?;

    // Lookup remote-tracking branch
    // let refs = repo.find_reference(&format!("refs/remotes/{remote_name}/{branch_name}"))?;
    let obj = repo.revparse_single(&format!("refs/remotes/{remote_name}/{branch_name}"))?;

    Ok(obj.id())
}