如何"git pull"?用git2-rs Rust板条箱完成? [英] How is "git pull" done with the git2-rs Rust crate?

查看:73
本文介绍了如何"git pull"?用git2-rs Rust板条箱完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 git2-rs 在Rust应用程序中实现一些标准git功能.我一直在阅读git的内部知识,并了解从高层次来看,"git pull"是"git fetch",然后是"git merge",但是在理解如何使其与git2-rs结合使用方面仍然有困难.在此处上讨论了一个问题,在该问题上,同意使用git2-rs"git pull"示例很好,但从未创建过.在该讨论中有一个进行硬重置的示例,但我想避免覆盖本地更改(因此合并).我也无法在其他任何使用git2-rs的代码库中找到示例.

I'm using git2-rs to implement some standard git functionality in a Rust application. 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. 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. 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.

"git reset"示例此处显示了如何我想在获取后获取OID,但合并功能采用AnnotatedCommit ,而且我不确定如何在两者之间进行转换,即使这是正确的选择.

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.

推荐答案

尝试类似的方法:

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

使用

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

这篇关于如何"git pull"?用git2-rs Rust板条箱完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆