I've been using objecitive-git and libgit2 to try to implement pull functionality. As git pull
is just a 'porcelain' command and is made up of a git fetch
followed by a git merge origin/master
then that would be how I implemented it.
The fetch is performed using the method in objective-git from the fetch branch on github.
[remote fetchWithCredentialProvider:nil error:&error progress:nil];
The code below is what is done after the fetch (which I know succeeds):
// 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);
}
After the mergedIndex
which starts out in memory has been written as a tree to disk (writeTreeToRepository
uses git_index_write_tree_to
) there is no change in the git repos status. I'm assuming I'm missing a last step to make the new tree HEAD or merge it with HEAD or something similar but I'm not sure exactly what.
Any help would be much obliged.