Вытащить (извлечь и объединить) с помощью libgit2
Я использовал objecitive-git и libgit2, чтобы попытаться реализовать функциональность pull. Как git pull
это просто "фарфоровая" команда и состоит из git fetch
с последующим git merge origin/master
тогда это будет, как я это реализовал.
Извлечение выполняется с помощью метода в target-git из ветки fetch на github.
[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 ответ
Когда у вас есть дерево, которое вы хотите использовать для фиксации слияния, вам нужно создать коммит слияния, который вы можете создать с помощью createCommitWithTree
в GTRepository
Точно так же, как вы создали бы любой другой, но с обоими предками в качестве родителей. Эта функция также позволяет вам попросить библиотеку обновить конкретную ветку, если вы ожидаете, что хранилище будет в тихом состоянии.