Libgit2sharp 冲突获取 theirs/ours/base 个文件内容

Libgit2sharp conflict get theirs/ours/base files content

我使用 Libgit2sharp,我想解决一个冲突。 在 class "Conflict" 我有 3 个 IndexEntry 属性(祖先,我们的,他们的) 他们每个人都有 属性 "Path" 指向同一个文件。

我想启动 TortoiseGitMerge 工具并生成 base/ours/theirs/ 文件...

我该怎么做?

提前致谢!

您可以从 Index:

得到 ConflictCollection
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);
}