C# - 统一的差异/补丁创建工具

6

我在一个C# WPF应用程序中有两个字符串,想要生成一个统一的差异文件(格式为GNU diff符号,类似于Subversion和TortoiseSVN中使用的补丁)。

是否有一个库可以帮助我完成这个任务,而不是重新发明轮子?

我已经在谷歌上搜索了很久,但没有成功。 :(

3个回答

4

它们是很棒的链接!!!非常感谢!我不知道为什么它们没有在 Google 中显示。 - Russell
昨天我在搜索替代方案时,在stackoverflow上找到了第一个方案(因为它没在谷歌上显示出来)。通常我用谷歌搜索SO:”searchwords site:stackoverflow.com“。 - jgauffin
1
这是一个古老的问题,但我也在寻找这个功能。和楼主一样,我更喜欢生成GNU diff格式文件的工具,但据我所知,你提到的两个程序都不支持该格式。 - RenniePet
那么你希望我在一个七年前的问题上做些什么呢? - jgauffin

2

是的,我知道这是一个古老的问题,但它仍然与我的当前项目相关。因此,为了帮助他人,我提供了一个博客文章链接,展示了我编写的用于调用GNU diff.exe程序的程序。

https://renniestechblog.com/information/37-generate-diff-and-fuzzy-patch-programs

以下是最重要的部分,其中调用了GNU diff.exe程序。(此代码引用了程序其他部分的一些内容,但应该可以猜出它的工作原理。)

  /// <summary>
  /// Method to use the GNU diff.exe program to create a .diff file for a Roslyn file which has 
  /// been modified.
  /// 
  /// This currently uses the GNU diff.exe program that happens to have been installed on my 
  /// developer PC as part of the GitHub Desktop for Windows program installation.
  /// </summary>
  private static void CreateDiffFile(string archiveFileName, string currentFileName)
  {
     const string CGnuDiffExe = 
        @"C:\Users\rp\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\usr\bin\diff.exe";

     string diffFileName = GetDiffFileName(currentFileName) + ".diff";
     Directory.CreateDirectory(Path.GetDirectoryName(diffFileName));

     using (Process windowsProcess = new Process())
     {
        windowsProcess.StartInfo.UseShellExecute = false;
        windowsProcess.StartInfo.FileName = CGnuDiffExe;
        windowsProcess.StartInfo.Arguments = 
                                "-u -r \"" + archiveFileName + "\" \"" + currentFileName + "\"";
        windowsProcess.StartInfo.RedirectStandardOutput = true;
        if (!windowsProcess.Start())
           DisplayErrorOrInfo("Unexpected result for Process.Start()");
        File.WriteAllText(diffFileName, windowsProcess.StandardOutput.ReadToEnd());
        windowsProcess.WaitForExit();
     }

     Console.WriteLine("Diff file created: " + diffFileName);
  }

0
找到了一个解决方案。请注意,汇编语言不受支持且据说存在错误,但我用它来比较复杂的XHTML,效果非常好!

http://razor.occams.info/code/diff/

请注意,它是根据PERL许可证发布的(请参见网站)。
下载Diff.dll并将其添加到您的解决方案中。
网站上有一个不错的示例。

4
链接已损坏。 - RenniePet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接