获取差异以正确识别包含相似内容的代码块
Getting a diff to properly recognize code blocks that contain similar content
我正在比较包含具有相似内容的代码块的文件。问题是这会导致差异混淆。我将从一个例子开始,因为这很难用语言来解释。
file1.txt:
text
(
contents
)
block "block1"
(
contents
)
block "block2"
(
contents
)
file2.txt:
block "block1"
(
contents
)
text
(
contents
)
block "block2"
(
contents
)
当我比较这两个文件时,我得到以下输出:
-text
+block "block1"
(
contents
)
-block "block1"
+text
(
contents
)
block "block2"
(
contents
)
问题是,diff 程序无法识别 "block" 类型的代码块完全独立于 "text" 类型的代码块,应将其视为单独的实体。 (Perl 的 Text::Diff
在这种情况下,但我也有 git-diff 可用,它做同样的事情。)
我怎样才能使 diff 将这些不同类型的代码块识别为单独的实体,以便这两个文件的 diff 会产生以下结果?
-text
-(
- contents
-)
block "block1"
(
contents
)
+text
+(
+ contents
+)
block "block2"
(
contents
)
Note that this is a drastically simplified example compared to the code I am actually trying to diff, I understand that it is easy enough to figure out what this example is doing, but when you are dealing with hundreds of similar elements the diff output becomes completely unreadable.
我想让 diff 意识到在此修改中只编辑了 "text" 个代码块,没有触及 "block" 个代码块。
如果您可以直接使用 git,请尝试 git diff --patience
$ git diff --patience
diff --git a/foo1.txt b/foo1.txt
index b474449..30a91bb 100644
--- a/foo1.txt
+++ b/foo1.txt
@@ -1,11 +1,11 @@
-text
-(
- contents
-)
block "block1"
(
contents
)
+text
+(
+ contents
+)
block "block2"
(
contents
处理此问题的最佳方法是使用 manual diff alignments in KDiff3。重复使用一些示例图像,但它与您的示例文件的结果相同:
我正在比较包含具有相似内容的代码块的文件。问题是这会导致差异混淆。我将从一个例子开始,因为这很难用语言来解释。
file1.txt:
text
(
contents
)
block "block1"
(
contents
)
block "block2"
(
contents
)
file2.txt:
block "block1"
(
contents
)
text
(
contents
)
block "block2"
(
contents
)
当我比较这两个文件时,我得到以下输出:
-text
+block "block1"
(
contents
)
-block "block1"
+text
(
contents
)
block "block2"
(
contents
)
问题是,diff 程序无法识别 "block" 类型的代码块完全独立于 "text" 类型的代码块,应将其视为单独的实体。 (Perl 的 Text::Diff
在这种情况下,但我也有 git-diff 可用,它做同样的事情。)
我怎样才能使 diff 将这些不同类型的代码块识别为单独的实体,以便这两个文件的 diff 会产生以下结果?
-text
-(
- contents
-)
block "block1"
(
contents
)
+text
+(
+ contents
+)
block "block2"
(
contents
)
Note that this is a drastically simplified example compared to the code I am actually trying to diff, I understand that it is easy enough to figure out what this example is doing, but when you are dealing with hundreds of similar elements the diff output becomes completely unreadable.
我想让 diff 意识到在此修改中只编辑了 "text" 个代码块,没有触及 "block" 个代码块。
如果您可以直接使用 git,请尝试 git diff --patience
$ git diff --patience
diff --git a/foo1.txt b/foo1.txt
index b474449..30a91bb 100644
--- a/foo1.txt
+++ b/foo1.txt
@@ -1,11 +1,11 @@
-text
-(
- contents
-)
block "block1"
(
contents
)
+text
+(
+ contents
+)
block "block2"
(
contents
处理此问题的最佳方法是使用 manual diff alignments in KDiff3。重复使用一些示例图像,但它与您的示例文件的结果相同: