使用Rust/git2将remote拉入本地分支

使用Rust/git2将remote拉入本地分支,rust,libgit2,Rust,Libgit2,我用这个答案作为指导: 这似乎是可行的,但我总是以分离状态结束(尽管引用在某些情况下匹配)。该存储库中没有分支master。你想去哪个分行结账? use git2::Repository; fn main() { let repo = Repository::clone("https://github.com/rossmacarthur/dotfiles", "/tmp/dots") .expect("Failed to

我用这个答案作为指导:


这似乎是可行的,但我总是以分离状态结束(尽管引用在某些情况下匹配)。

该存储库中没有分支
master
。你想去哪个分行结账?
use git2::Repository;

fn main() {
    let repo = Repository::clone("https://github.com/rossmacarthur/dotfiles", "/tmp/dots")
        .expect("Failed to clone repo");

    let refname = "master"; // or a tag (v0.1.1) or a commit (8e8128)
    let (object, reference) = repo.revparse_ext(refname).expect("Object not found");
    
    repo.checkout_tree(&object, None)
        .expect("Failed to checkout");

    match reference {
        // gref is an actual reference like branches or tags
        Some(gref) => repo.set_head(gref.name().unwrap()),
        // this is a commit, not a reference
        None => repo.set_head_detached(object.id()),
    }
    .expect("Failed to set HEAD");
}