2014-03-29 26 views
0

使用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文件?

回答

0

请记住,Git是基于快照的,因此在提交时索引中的任何内容都将是提交的内容。

本身没有任何未停止的动作,因为它是依赖于上下文的。如果这个文件是新的,那么删除它就是你做什么来取消它。否则,暂存和暂存操作是相同的操作,区别在于您在条目中使用的blob是该文件的旧版本。

由于您想要将文件移回其在HEAD中的状态,因此不应该需要使用diff,而是需要使用HEAD树并在其中查找所需的条目(尽管快速浏览一下,我没有看到目标git包装git_tree_entry_bypath()

如果我们写出一个错误版本的索引,那肯定是库中的一个bug,你能否打开一个问题,我们可以尝试重现它?

+0

非常感谢Carlos,我会将致命错误报告为一个问题,但我不确定这是否属于我的错,即使该条目似乎有效。那么使用'git_tree_entry_bypath'应该可以使用'git_tree_entry'的细节来重新构建原始索引条目? – Joshua

+0

无论用户做什么,写出无效索引文件都是一个bug。从树条目中,您可以获取重要信息,即filemode和id。其他任何东西都主要存在于缓存索引中。 –