我一直在使用 objecitive-git 和 libgit2 来尝试实现拉取功能。Asgit pull
只是一个“瓷器”命令,由 agit fetch
后跟 a git merge origin/master
then 组成,这就是我实现它的方式。
使用来自 github 上的 fetch 分支的 Objective-git 中的方法执行获取。
[remote fetchWithCredentialProvider:nil error:&error progress:nil];
下面的代码是在 fetch 之后完成的(我知道它成功了):
// 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
which 已作为树写入磁盘(writeTreeToRepository
使用git_index_write_tree_to
)之后,git repos 状态没有变化。我假设我错过了制作新树 HEAD 或将其与 HEAD 或类似内容合并的最后一步,但我不确定到底是什么。
任何帮助将不胜感激。