git diff 没有将单引号更改为双引号

xax*_*axa 4 git

我的一位同事应用了 js-linting 的结果,所以现在有很大一部分差异,其中所有实际更改的行只是单引号'到双引号"

- var mod = require('mod');
+ var mod = require("mod");
Run Code Online (Sandbox Code Playgroud)

有什么办法可以过滤掉git diff吗?如果一行上的其他内容发生了变化,它仍应显示:

- var mod = require('mod1');
+ var mod = require("mod2");
Run Code Online (Sandbox Code Playgroud)

Mar*_*ger 5

这里没有一个伟大的方式来做到这一点,尤其是当你考虑之间的变化"' 可能是bug。

"hello" => 'hello'
Run Code Online (Sandbox Code Playgroud)

可能没问题,但是

"what's up" => 'what's up'
Run Code Online (Sandbox Code Playgroud)

将是令人关注的(关注程度可能因语言而异)。

您可以--word-diff-regex'and"视为“not part of words”;在这种情况下,在计算差异时将忽略所有 '"字符。但是,这会执行“单词”差异而不是典型的面向行的差异。例如

$ git diff file1

diff --git a/file1 b/file1
index 64315fd..cd80abf 100644
--- a/file1
+++ b/file1
@@ -1,4 +1,4 @@
-"this is a test"
-"this is a changing test"
-"this couldn't cause a problem, could it?"
-"it's possible some edits could be trouble"
+'this is a test'
+'this is a changing line of test'
+'this couldn't cause a problem, could it?'
+'it"s possible some edits could be trouble'"'"''
Run Code Online (Sandbox Code Playgroud)

嗯,这没什么用;它只是说一切都变了。好的,怎么样

$ git diff --word-diff-regex="[^'\"]" file1
diff --git a/file1 b/file1
index 64315fd..cd80abf 100644
--- a/file1
+++ b/file1
@@ -1,4 +1,4 @@
'this is a test'
'this is a changing {+line of +}test'
'this couldn't cause a problem, could it?'
'it"s possible some edits could be trouble'"'"''
Run Code Online (Sandbox Code Playgroud)

嗯,它检测到并突出显示了第二行的变化,所以很好。(这不会在复制的文本中出现,但默认情况下,在控制台上,添加的内容会突出显示为绿色,因此它确实可以很好地调用更改。)它没有注意到我已经更改了第 3 行潜在的破坏性方式,它没有注意到第 4 行的一堆虚假报价更改(和添加)。

所以它并不完美。

通过预处理文件,您可以得到稍微好一点的结果,强制两个版本都使用规范化的引号表示法;这将捕获上面的第 4 行,但仍然不是第 3 行。而且您将不再直接区分源代码控制的版本,因此可以说您所做的并不完全是可审计的。