git_api/
fetch.rs

1use git2::{Error, FetchOptions, Oid, Repository};
2
3use crate::auth::auth_callbacks;
4
5/// 获取远程分支(fetch)
6///
7/// 返回值:远程分支Oid
8pub fn repo_fetch(
9    repo: &Repository,
10    remote_name: &str,
11    branch_name: &str,
12    username: &str,
13    password: &str,
14) -> Result<Oid, Error> {
15    let mut remote = repo.find_remote(remote_name)?;
16
17    let mut fetch_options = FetchOptions::new();
18    let callbacks = auth_callbacks(username, password);
19    fetch_options.remote_callbacks(callbacks);
20
21    // Update remote-tracking branches
22    // remote.fetch(&[branch_name], Some(&mut fetch_options), None)?;
23    remote.fetch(
24        &[&format!("refs/heads/{branch_name}")],
25        Some(&mut fetch_options),
26        None,
27    )?;
28
29    // Lookup remote-tracking branch
30    // let refs = repo.find_reference(&format!("refs/remotes/{remote_name}/{branch_name}"))?;
31    let obj = repo.revparse_single(&format!("refs/remotes/{remote_name}/{branch_name}"))?;
32
33    Ok(obj.id())
34}