2015-10-07 39 views
1

我使用Libgit2sharp,我想解決衝突。 在「衝突」類中,我有3個IndexEntry屬性(祖先,我們,他們的) 他們每個人都有指向同一文件的屬性「路徑」。Libgit2sharp衝突獲取他們的/我們的/我們的基礎文件內容

我想lanch的TortoiseGitMerge的工具,我想生成基本/我們/他們/文件...

我怎麼能這樣做?

在此先感謝!

回答

2

你可以得到ConflictCollectionIndex

var conflicts = repository.Index.Conflicts; 

然後得到衝突特定文件:

var conflict = conflicts["Foo.cs"]; 

然後你就可以得到IndexEntry爲衝突的各個方面:

var ancestor = conflict.Ancestor; 
var ours = conflict.Ours; 
var theirs = conflict.Theirs; 

隨着索引條目,喲ü可以得到的對象:

var ancestorBlob = (ancestor != null) ? repository.Lookup(ancestor.Id) : null; 
var ourBlob = (ours != null) ? repository.Lookup(ours.Id) : null; 
var theirBlob = (theirs != null) ? repository.Lookup(theirs.Id) : null; 

,你可以得到一個流的每一面的內容:

var ancestorStream = (ancestor != null) ? ancestorBlob.GetContentStream(new FilteringOptions(ancestor.Name)); 
var ourStream = (ours != null) ? ourBlob.GetContentStream(new FilteringOptions(ours.Name)); 
var theirStream = (theirs != null) ? theirBlob.GetContentStream(new FilteringOptions(theirs.Name)); 

然後,你可以寫的每個文件 - 記住,衝突可能有三種不同的路徑,如果該文件在每一側都重新命名,您應該檢查Conflict.Name。例如,要將其中一面寫入磁盤:

using (var ancestorOutputStream = File.Create(ancestor.Name + ".orig")) 
{ 
    ancestorStream.CopyTo(ancestorOutputStream); 
} 
+0

非常感謝!解決了我的問題! – Sergiu

+0

唯一的區別是我使用foreach來處理衝突,然後對於每個衝突,我從衝突的Ours.Path中獲取文件名,因爲此路徑在所有屬性(Ours,Theirs,Base)中都是相同的 – Sergiu

+0

@ Sergiu不能保證 - 如果其中一個文件被重命名,路徑可能會不同。 –

相關問題