使用Objective-Git和libgit2它一直是很容易上演了一出文件准备提交取消阶段文件:与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];
}
}];
然而,这叶损坏状态造成任何混帐混帐指数命令记录一个致命错误:
fatal: Unknown index entry format bfff0000
fatal: Unknown index entry format bfff0000
什么是正确的方式来解除使用libgit2文件?
非常感谢Carlos,我会将致命错误报告为一个问题,但我不确定这是否属于我的错,即使该条目似乎有效。那么使用'git_tree_entry_bypath'应该可以使用'git_tree_entry'的细节来重新构建原始索引条目? – Joshua
无论用户做什么,写出无效索引文件都是一个bug。从树条目中,您可以获取重要信息,即filemode和id。其他任何东西都主要存在于缓存索引中。 –