1

TreeChanges在 LibGit2Sharp中迭代的最佳(如高性能、简单)方法是什么?

如果我访问该.Patch属性,我会检索更改的全文。这对我来说还不够……理想情况下,我希望能够遍历 diff 行,并且每行检索行的状态(修改、添加、删除)并从中构建我自己的输出。

更新:

假设我想建立自己的差异输出。我想做的是迭代更改的行,在迭代期间我会检查更改的类型(添加、删除),并构造我的输出。

例如:

var diff = "";
foreach (LineChange line in changes) // Bogus class "LineChange"
{
    if (line.Type == LineChange.TYPE_ADDED)
        diff += "+";
    else
        diff += "-";

    diff += line.Content;
    diff += "\n";
}

以上只是一个简单的例子,我正在寻找什么样的灵活性。能够通过更改,并根据行更改类型运行一些逻辑。该Patch属性已经“构建”,一种方法是解析它,但库首先构建输出然后我解析它似乎很愚蠢......我宁愿直接使用构建成分。

我需要这种功能,以便我可以显示更改的视觉差异,这涉及比我上面给出的简单示例更多的代码和逻辑。

4

2 回答 2

2

@svick is right. It's not exposed.

It might be useful to open an issue/feature request to further discuss this topic. Indeed, exposing a full blown line based diffgram might not fit the current "grain" of the library. However, provided you can come up with a scenario/use case that would benefit most of the users, some research may be invested in order to widen the API.

Beside this option, there might be other solutions: post-process the current produced patch against the previous version of the file

  • See this SO question for potential leads
  • Neil Fraser's "Diff Strategies" paper is also a great source of strategies and potential caveats regarding what a diff tool might aim at
  • DiffPlex, as a working visualization tool, might be inspirational as well
  • With some more work, one might even achieve something similar to the following kind of visualization (from Perforce 4 viewer)

p4merge
(source: macworld.com)

Note: In order to ease this, it might be useful to expose in C# the libgit2 diffing options.

于 2012-06-24T17:07:08.520 回答
2

据我所知,libgit2sharp 并未公开此信息,但在 blob 差异(但不适用于树差异)的情况下,它是由 libgit2 提供的。相关代码在 中ContentChanges.cs,特别是在构造函数和LineCallback()方法中(树差异的代码在 中TreeChanges.cs)。

因此,我认为您有两种选择:

  1. 调用您自己git_diff_blobs()内部使用的方法ContentChanges,或者使用反射(它是 中的内部方法NativeMethods),或者通过将 PInvoke 签名复制到您的项目。您很可能还需要Utf8Marshaler.
  2. 修改 的代码ContentChanges,使其符合您的需要。如果您这样做,为该更改创建一个拉取请求可能是有意义的,以便其他人也可以使用它。
于 2012-06-12T14:52:04.437 回答