6

我正在使用 Git 对散文进行版本化,并且一直在尝试git diff --word-diff查看行内的更改。我想使用脚本中生成的结果。

--word-diff但识别单词的默认方式似乎存在缺陷。所以我一直在尝试各种--word-diff-regex=选项。

问题

以下是我试图处理的两个主要缺陷:

  1. 添加的空格似乎被忽略了。但是,如果尝试以编程方式使用结果,则空格可能非常重要。

    例如,从 Markdown (.md) 文件中获取此标头:

    # Test file
    

    现在,让我们在它的末尾添加一些文本:

    # Test file in Markdown
    

    如果我运行git diff --word-diff这个:

    # Test file {+in Markdown+}
    

    但是“in”一词之前的空格并未包含在差异中。

  2. 空行被完全忽略。

    这是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.
    

    但这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.+}
    

    删除和添加的空行将被完全忽略。

期望的结果

把上面的两个例子放在一起。这是我想看到的:

# 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.+}

我尝试过的事情:

  • git diff --word-diff-regex='.'当新词与现有词共享字符时,似乎过于细化
  • git diff --word-diff-regex='[^ ]+|[ ]'似乎解决了第一个问题,但老实说,我不确定为什么。
  • git diff --word-diff-regex='[^ ]+|[ ]|^$'-- 我希望^$最后的 能帮助捕获空行 -- 但它没有,更糟糕的是,它似乎忽略了随后的变化。
  • git diff --word-diff-regex='[^ ]+|[ ]|.{0}'产生与以前相同的问题。

如果有人能阐明如何做到这一点,或者至少与--word-diff-regex.

4

1 回答 1

1

你遇到的主要问题是阻止你从https://git-scm.com/docs/diff-options获得你想要的东西,是:

包含换行符的匹配项在换行符处被静默截断(!)。

这意味着单词差异总是会忽略行差异。我认为缺少自定义差异生成器,您不会获得想要的结果。

于 2020-09-03T22:08:16.210 回答