С помощью target-git и libgit2 было довольно легко создать файл, готовый к фиксации:
GTIndex *repoIndex = [self.repository indexWithError:&error];
[repoIndex removeFile:path error:&error];
if (status != GTFileStatusIgnored && status != GTFileStatusWorkingDeleted) {
// Now add the file to the index
[repoIndex addFile:path error:&error];
}
[repoIndex write:&error];
Однако деинсталляция файла оказывается немного более сложной. Простое удаление его из индекса репозитория не работает, так как git думает, что файл был удален, что имеет смысл. Кажется, что мне нужно сделать, это изменить запись индекса в индексе на ту, которая была до того, как она была поставлена.
Я попытался сделать следующее, используя diff, чтобы получить старую дельту diff, построить из нее git_index_entry
и вставить ее:
GTIndex *repoIndex = [self.repository indexWithError:&error];
GTBranch *localBranch = [self.repository currentBranchWithError:&error];
GTCommit *localCommit = [localBranch targetCommitAndReturnError:&error];
GTDiff *indexCommitDiff = [GTDiff diffIndexFromTree:localCommit.tree inRepository:self.repository options:nil error:&error];
// Enumerate through the diff deltas until we get to the file we want to unstage
[indexCommitDiff enumerateDeltasUsingBlock:^(GTDiffDelta *delta, BOOL *stop) {
NSString *deltaPath = delta.newFile.path;
// Check if it matches the path we want to usntage
if ([deltaPath isEqualToString:path]) {
GTDiffFile *oldFile = delta.oldFile;
NSString *oldFileSHA = oldFile.OID.SHA;
git_oid oldFileOID;
int status = git_oid_fromstr(&oldFileOID, oldFileSHA.fileSystemRepresentation);
git_index_entry entry;
entry.mode = oldFile.mode;
entry.oid = oldFileOID;
entry.path = oldFile.path.fileSystemRepresentation;
[repoIndex removeFile:path error:&error];
status = git_index_add(repoIndex.git_index, &entry);
[repoIndex write:&error];
}
}];
Однако это оставляет индекс git в поврежденном состоянии, что приводит к тому, что любая команда git регистрирует фатальную ошибку:
fatal: Unknown index entry format bfff0000
fatal: Unknown index entry format bfff0000
Как правильно удалить файл с помощью libgit2?