使用libgit2拉(获取和合并)

时间:2014-03-23 11:18:47

标签: objective-c libgit2 objective-git

我一直在使用objecitive-git和libgit2来尝试实现pull功能。因为git pull只是一个瓷器'命令由git fetch后跟git merge origin/master组成,这就是我实现它的方式。

使用来自github上的fetch分支的objective-git中的方法执行获取。

[remote fetchWithCredentialProvider:nil error:&error progress:nil];

下面的代码是在获取之后所做的事情(我知道成功):

// Get the local branch
GTBranch *localBranch = [repo localBranchesWithError:nil][0];
// Get the remote branch
GTBranch *remoteBranch = [repo remoteBranchesWithError:nil][0];

// Get the local & remote commit
GTCommit *localCommit = [localBranch targetCommitAndReturnError:nil];
GTCommit *remoteCommit = [remoteBranch targetCommitAndReturnError:nil];

// Get the trees of both
GTTree *localTree = localCommit.tree;
GTTree *remoteTree = remoteCommit.tree;

// Get OIDs of both commits too
GTOID *localOID = localCommit.OID;
GTOID *remoteOID = remoteCommit.OID;

// Find a merge base to act as the ancestor between these two commits
GTCommit *ancestor = [repo mergeBaseBetweenFirstOID:localOID secondOID:remoteOID error:&error];
if (error) {
    NSLog(@"Error finding merge base: %@", error);
}
// Get the ancestors tree
GTTree *ancestorTree = ancestor.tree;

// Merge into the local tree
GTIndex *mergedIndex = [localTree merge:remoteTree ancestor:ancestorTree error:&error];
if (error) {
    NSLog(@"Error mergeing: %@", error);
}

// Write the merge to disk and store the new tree
GTTree *newTree = [mergedIndex writeTreeToRepository:repo error:&error];
if (error) {
    NSLog(@"Error writing merge index to disk: %@", error);
}

在内存中开始的mergedIndex被写为磁盘树(writeTreeToRepository使用git_index_write_tree_to)之后,git repos状态没有变化。我假设我错过了制作新树HEAD的最后一步,或者将它与HEAD或类似的东西合并,但我不确定究竟是什么。

任何帮助都有很大的帮助。

1 个答案:

答案 0 :(得分:2)

获得要用于合并提交的树后,需要创建合并提交,您可以使用createCommitWithTree中的GTRepository创建合并提交,方法与创建任何合并提交相同。另外,但祖先都是父母。如果您希望存储库处于安静状态,该函数还允许您要求库更新特定分支。