简体   繁体   English

“git pull”是如何使用 git2-rs Rust crate 完成的?

[英]How is "git pull" done with the git2-rs Rust crate?

I'm using git2-rs to implement some standard git functionality in a Rust application.我正在使用git2-rs在 Rust 应用程序中实现一些标准的 git 功能。 I've been reading up on git internals and understand that at a high level "git pull" is a "git fetch" followed by a "git merge", but am still having trouble understanding how to make it work with git2-rs.我一直在阅读 git 内部结构,并了解到“git pull”在高层次上是“git fetch”,然后是“git merge”,但我仍然无法理解如何使其与 git2-rs 一起工作。 There is a discussion on an issue here where it's agreed that a git2-rs "git pull" example would be nice, but one was never created.有对一个问题的讨论在这里在那里的同意,GIT2-RS“混帐拉”的例子将是很好的,但一个是从未创建。 There is an example of doing a hard reset in that discussion, but I want to avoid overwriting local changes (thus the merge).在该讨论中有一个进行硬重置的示例,但我想避免覆盖本地更改(因此是合并)。 I have been unable to find an example in any other codebases that use git2-rs as well.我一直无法在任何其他使用 git2-rs 的代码库中找到示例。

The "git reset" example here shows how to get an OID after a fetch I think, but the merge function takes an AnnotatedCommit , and I'm not sure how to convert between the two, or even if that's the right direction to go. 此处的“git reset”示例显示了我认为如何在获取后获取 OID,但合并函数采用AnnotatedCommit ,我不确定如何在两者之间进行转换,或者即使这是正确的方向。

Try something like:尝试类似:

        let our_commit = find_last_commit()?;
        let reference = repo.find_reference("FETCH_HEAD")?;
        let their_commit = reference.peel_to_commit()?;
        let _index = repo
                .merge_commits(&our_commit, &their_commit, Some(&MergeOptions::new()))?;

using使用

   pub fn find_last_commit(repo: &Repository) -> Result<Commit, RepoError> {
        let obj = repo.head()?.resolve()?.peel(ObjectType::Commit)?;
        match obj.into_commit() {
            Ok(c) => Ok(c),
            _ => Err(RepoError::new("commit error")),
        }
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM