gpt4 book ai didi

regex - git word diff regex 奇怪的行为

转载 作者:行者123 更新时间:2023-12-03 14:55:30 26 4
gpt4 key购买 nike

我正在使用 Git 编写散文版本并一直在尝试 git diff --word-diff查看行内的变化。我想使用脚本中生成的结果。

但是默认方式是--word-diff识别一个词似乎有缺陷。所以我一直在试验 --word-diff-regex=选项。

问题

以下是我试图解决的两个主要缺陷:

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

    例如,从 Markdown (.md) 文件中获取此 header :
    # Test file

    现在,让我们在它的末尾添加一些文本:
    # Test file in Markdown

    如果我运行 git diff --word-diff对此:
    # Test file {+in Markdown+}

    但是“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.

    但这里是 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 分享一些关于幕后发生的事情的知识,我将不胜感激。 .

    最佳答案

    您遇到的主要问题是阻止您拥有想要的东西,来自 https://git-scm.com/docs/diff-options , 是:

    A match that contains a newline is silently truncated(!) at the newline.


    这意味着单词差异总是会忽略行差异。我认为如果没有自定义差异生成器,您将无法获得想要的结果。

    关于regex - git word diff regex 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249437/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com