2016-09-23 55 views
1

我已经看过这篇文章中的答案(libgit2 (fetch & merge & commit)),但我正努力让Git Pull工作。我没有收到错误消息。该提取似乎工作,但合并不会发生。做一个Git状态显示,我的分支背后是1提交...使用libgit2做Git Pull

On branch Branch_1_1. 
Your branch is behind 'origin/Branch_1_1' by 1 commit, and can be fast-forwarded. 
    (use "git pull" to update your local branch) 
nothing to commit, working directory clean 

我的代码如下...

static int fetchhead_ref_cb(const char *name, const char *url, 
    const git_oid *oid, unsigned int is_merge, void *payload) 

{ 
    if (is_merge) 
    { 
     strcpy_s(branchToMerge, 100, name); 
     memcpy(&branchOidToMerge, oid, sizeof(git_oid)); 
    } 
    return 0; 
} 

void GitPull() 
{ 
    git_libgit2_init(); 

    git_repository *repo = NULL; 
    git_remote *remote; 

    int error = git_repository_open(&repo, "C:\\work\\GitTestRepos\\Repo1"); 
    CHECK_FOR_ERROR 

    error = git_remote_lookup(&remote, repo, "origin"); 
    CHECK_FOR_ERROR 

    git_fetch_options options = GIT_FETCH_OPTIONS_INIT; 
    error = git_remote_fetch(remote, NULL, &options, NULL); 
    CHECK_FOR_ERROR 

    git_repository_fetchhead_foreach(repo, fetchhead_ref_cb, NULL); 

    git_merge_options merge_options = GIT_MERGE_OPTIONS_INIT; 
    git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT; 
    git_annotated_commit *heads[ 1 ]; 
    git_reference *ref; 

    error = git_annotated_commit_lookup(&heads[ 0 ], repo, &branchOidToMerge); 
    CHECK_FOR_ERROR 
    error = git_merge(repo, (const git_annotated_commit **)heads, 1, &merge_options, &checkout_options); 
    CHECK_FOR_ERROR 

    git_annotated_commit_free(heads[ 0 ]); 
    git_repository_state_cleanup(repo); 
    git_libgit2_shutdown(); 
} 

我在做什么是造成合并不工作?

回答

1

首先,git_checkout_options.checkout_strategy有一个相当惊人的默认值。它默认为空运行。所以你可能想把它改成GIT_CHECKOUT_SAFE之类的东西。

要理解的第二件事是,git_merge实际上没有提交合并。它只设置你的工作目录和索引进行合并。您仍然需要检查冲突并致电git_commit_create完成合并。

但是,看起来你并不需要在这个特定情况下合并。您的分支可以快速转发。致电git_merge_analysis以确定要执行哪种合并。使用新提取头快速转发当前分支上的呼叫git_reference_set_target

+0

谢谢杰森。我试过你的建议来做一个提交。我指定了本地和远程分支作为父母,但是它使得本地回购一个提交在遥控器之前,这不会发生“git pull”。 – Anthony

+0

提交合并后,本地分支*应该*提前一次提交。然而,我没有意识到你并不需要合并,因为你的本地历史没有偏离远程历史。你可以快进。我会相应地更新我的答案。 –