How is “git pull” done with the git2-rs Rust crate?

回眸只為那壹抹淺笑 提交于 2021-02-08 15:13:05

问题


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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/54100789/how-is-git-pull-done-with-the-git2-rs-rust-crate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!