2014-03-29 37 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。其他任何東西都主要存在於緩存索引中。 –