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

16

我正在使用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个回答

18
以下示例使用的是Rust v1.34和git2 v0.8。
要检出一个分支:
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));

}

要检出一个标签,请尝试以下方法:

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

    }
}

我敢打赌通常有更好的方法,但这个问题已经无人回答几个月了,这就是我刚刚想到的解决方法。


1
我的更新帖子完全解决了原帖提出的问题。点赞是受欢迎的。我甚至没有足够的积分来评论其他人的帖子,比如原帖,所以我可以让他们知道我成功回答了这个问题。如果对这个问题有兴趣,我会努力使答案更符合习惯用语。 - szignal
在检出提交的示例中,正确的方法是使用 set_head_detached() - set_head() 期望一个引用名称,如 refs/heads/masterHEAD(但提交哈希不是引用)。因此,正确的方式是 repo.set_head_detached(oid); - sudormrfbin

17

使用更新版本的git2v0.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
谢谢,这样更干净了! - Ross MacArthur
我在使用这段代码检出远程分支时遇到了一些问题,它会出现一个错误信息:“找不到对象:未找到'revspec 'test'”。使用origin/test时一切正常(但会进入分离头状态)... 在我提出一个新问题之前,你有什么想法吗? - undefined

1
我认为repo.set_head("mybranch")是您正在寻找的内容。更多信息可在此处获得。

1
  1. repo.set_head("refs/heads/mybranch"):设置仓库的 HEAD 为 "refs/heads/mybranch"。
  2. 这不会改变工作目录中的文件。
- Shimon S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接