guy*_*sey 6 regex git word-diff
我正在使用 Git 对散文进行版本化,并且一直在尝试git diff --word-diff
查看行内的更改。我想使用脚本中生成的结果。
但是--word-diff
识别单词的默认方式似乎有缺陷。所以我一直在尝试各种--word-diff-regex=
选择。
以下是我试图解决的两个主要缺陷:
添加的空格似乎被忽略了。但是如果尝试以编程方式使用结果,空格可能非常重要。
例如,从 Markdown (.md) 文件中获取此标头:
# Test file
Run Code Online (Sandbox Code Playgroud)
现在,让我们在它的末尾添加一些文本:
# Test file in Markdown
Run Code Online (Sandbox Code Playgroud)
如果我运行git diff --word-diff
这个:
# Test file {+in Markdown+}
Run Code Online (Sandbox Code Playgroud)
但是“in”一词之前的空格并未作为差异的一部分包含在内。
空行被完全忽略。
这git diff
是文件内容的标准,我删除了一行并添加了几行新行——一个是空的,另一个带有文本“这是一个新行”。
This is a test file to see how word diff responds in certain situations.
-
I'll try removing lines and adding them to see what happens.
Here's another line so we can see what happens with line removals and additions. I want to see how `git diff --word-diff` handles it all!
+
+Here's a new line.
Run Code Online (Sandbox Code Playgroud)
但这里是git diff --word-diff
相同的内容:
This is a test file to see how word diff responds in certain situations.
I'll try removing lines and adding them to see what happens.
Here's another line so we can see what happens with line removals and additions. I want to see how `git diff --word-diff` handles it all!
{+Here's a new line.+}
Run Code Online (Sandbox Code Playgroud)
删除和添加的空行将被完全忽略。
把上面的两个例子放在一起。这是我想看到的:
# Test file{+ in Markdown+}
This is a test file to see how word diff responds in certain situations.
{--}
I'll try removing lines and adding them to see what happens.
Here's another line so we can see what happens with line removals and additions. I want to see how `git diff --word-diff` handles it all!
{++}
{+Here's a new line.+}
Run Code Online (Sandbox Code Playgroud)
git diff --word-diff-regex='.'
当新词与现有词共享字符时似乎太细化了git diff --word-diff-regex='[^ ]+|[ ]'
似乎解决了第一个问题,但老实说,我实际上不确定为什么。git diff --word-diff-regex='[^ ]+|[ ]|^$'
-- 我希望^$
最后能帮助捕获空行 -- 但它没有,更糟糕的是,它似乎忽略了随后的变化。git diff --word-diff-regex='[^ ]+|[ ]|.{0}'
产生与前一个相同的问题。如果有人能阐明如何做到这一点,或者至少分享一些关于--word-diff-regex
.
您遇到的阻止您获得所需内容的主要问题来自https://git-scm.com/docs/diff-options,是:
包含换行符的匹配项会在换行符处被静默截断(!)。
这意味着单词差异总是会忽略行差异。我认为如果没有自定义差异生成器,您将无法获得所需的结果。