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

标签 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.
}

我希望能够 check out 一个分支或一个提交或一个标签。

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

我可以执行以下操作,但它只会更改文件

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

最佳答案

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

checkout 一个分支:

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

checkout 一个提交:

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

}

要 checkout 标签,请尝试这样的操作:

use git2::*;

fn main() {
    // No relation to the example project below.
    // It looks interesting and it has tags!
    let repo = Repository::clone(
        "https://github.com/narke/colorForth.git",
        "colorforth"
    ).expect("failed to clone repository");

    let tag_name = "v2012";

    let references = repo.references().unwrap();

    for reference in references {
        let _reference = reference.unwrap();

        if _reference.is_tag() == true {
            let tag = _reference.peel_to_tag().unwrap();
            if tag.name().unwrap() == tag_name {
                let target_oid = tag.target().unwrap().id();
                let commit = repo.find_commit(target_oid).unwrap();

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

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

                repo.checkout_tree(
                    &obj,
                    None,
                );

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

    }
}

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

关于git - 如何在 Rust git2 中获取 `git checkout ...` 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55141013/

相关文章:

rust - 如何展平使用引用的迭代器

rust - 将 Rust 箱用于 STM32 微 Controller 板

arrays - 如何在未排序的数组或其段中找到第 k 个最小的元素?

Github-Jenkins 触发奴隶问题

git - 如何查看 git binary 使用的是 openssl 还是 gnutls?

Paypal 错误 10002 : Authentication/Authorization Failed

PHP 文件不会从 HTML PayPal 表单中提取 'AMOUNT'

php - 如何在 magento 结帐过程之前设置自定义总计?

Gitosis post-receive hook 用于部署存储库获取公钥错误

git svn rebase 总是与我自己的提交冲突