如何在Rust git2中获得`git checkout ...`的行为

时间:2019-03-13 11:38:11

标签: git rust checkout libgit2

我正在使用Rust git2板条箱来克隆这样的Git存储库

use git2::Repository;

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

     repo.checkout("mybranch");  // need something like this.
}

我希望能够签出分支机构,提交或标签。

我查看了以下文档,但仍不确定使用哪种方法

我能够执行以下操作,但只会更改文件

let object = repo
    .revparse_single("mybranch")
    .expect("failed to find identifier");
repo.checkout_tree(&object, None)
    .expect(&format!("failed to checkout '{:?}'", object));

如果我进行重置,它将更改HEAD,但不会更改当前分支

repo.reset(&object, git2::ResetType::Soft, None)
    .expect(&format!("failed to checkout '{:?}'", object));

3 个答案:

答案 0 :(得分:1)

下面的示例是Rust v1.34和git2 v0.8。

要结帐分支机构:

PartictionScheme

要检出提交:

use git2::*;

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

    let branch_name = "my_branch";

    let head = repo.head().unwrap();
    let oid = head.target().unwrap();
    let commit = repo.find_commit(oid).unwrap();

    let branch = repo.branch(
        branch_name,
        &commit,
        false,
    );

    let obj = repo.revparse_single(&("refs/heads/".to_owned() + 
        branch_name)).unwrap();

    repo.checkout_tree(
        &obj,
        None
    );

    repo.set_head(&("refs/heads/".to_owned() + branch_name));
}

要签出标签,请尝试以下操作:

use git2::*;

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

    let my_oid_str = "9411953f92d100f767e6de6325b17afae5231779";

    let oid = Oid::from_str(my_oid_str).unwrap();
    let commit = repo.find_commit(oid).unwrap();

    let branch = repo.branch(
        my_oid_str,
        &commit,
        false,
    );

    let obj = repo.revparse_single(&("refs/heads/".to_owned() + my_oid_str)).unwrap(); 

    repo.checkout_tree(
        &obj,
        None,
    );

    repo.set_head(&("refs/heads/".to_owned() + my_oid_str));

}

我敢打赌,通常会有更好的方法,但是这个问题已经几个月没有得到答案了,这就是我刚才想出的方式。

答案 1 :(得分:1)

使用更新版本的 git2 (v0.13.18):

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");
}

注意checkout_tree只设置工作树的内容,set_head只设置HEAD。仅运行其中之一将使目录处于脏状态。

答案 2 :(得分:0)

我认为您正在寻找repo.set_head("mybranch")here中提供了更多信息。