【问题标题】:How to have git-diff ignore all whitespace-change but leading one?如何让 git-diff 忽略所有空格更改但领先一个?
【发布时间】:2016-03-24 20:12:42
【问题描述】:

another post我发现很整洁

git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'

它在将git-diff 的输出压缩到基本内容同时保持清晰易读方面做得很好(尤其是在添加--word-diff=plain 以获取额外的[-/-]{+/+} 周围的删除/添加时)。虽然这确实包括空白更改,但输出不会以任何明显的方式突出显示它们,例如当更改 代码行的缩进时(这是一个严重的更改)将显示为缩进较长的行(之前或之后),但没有任何突出显示。

如何正确突出显示空白更改,可能通过将空白替换为一些 unicode 字符,例如 ·,或者更接近 git diff --word-diff-regex=.{+ +} 等,但使用更智能的分词?

【问题讨论】:

  • 如果这很重要,我目前正在使用 msys-git 的 git 2.6.2.windows.1

标签: python git whitespace git-diff


【解决方案1】:

到目前为止我能做到的最好的是

git diff --color-words='[[:space:]]|([[:alnum:]]|UTF_8_GUARD)+' --word-diff=plain

注意[:space:]前面删除的^

【讨论】:

    【解决方案2】:

    我无法解决您的问题,但我担心 Git 可能会在这里对您不利。回想一下--color-words=<regex>--word-diff=color--word-diff-regex=<regex> 的组合。 man git diff 文档说:

       --word-diff-regex=<regex>
           Use <regex> to decide what a word is, instead of considering runs
           of non-whitespace to be a word. Also implies --word-diff unless it
           was already enabled.
    
           Every non-overlapping match of the <regex> is considered a word.
           Anything between these matches is considered whitespace and
           ignored(!) for the purposes of finding differences. You may want
           to append |[^[:space:]] to your regular expression to make sure
           that it matches all non-whitespace characters. A match that
           contains a newline is silently truncated(!) at the newline.
    
           The regex can also be set via a diff driver or configuration
           option, see gitattributes(1) or git-config(1). Giving it
           explicitly overrides any diff driver or configuration setting.
           Diff drivers override configuration settings.
    

    请注意中间段落的这一部分:“这些匹配项之间的任何内容都被视为空格并被忽略(!)以便发现差异。”所以,听起来就像 Git 试图在这里特别对待空白一样,这可能是个问题。

    【讨论】:

    【解决方案3】:

    这是使用问题末尾建议的替换的替代方法:

    git config --global core.pager 'less --raw-control-chars'
    

    这样可以正确显示 unicode 符号,而不是一些奇怪的 &lt;c3&gt;ish 输出。将以下内容添加到您的 git 配置中:

    [diff "txt"]
        textconv = unwhite.sh
    

    并且,缺少global solution.gitattributes 之类的东西

    *.py diff=txt
    

    最后,unwhite.sh

    #!/bin/bash
    awk 1 ORS='[7m\\n[27m\n' $1 | sed -e 's/ /␣/g' -e 's/\t/→/g'
    

    请注意,[s 之前有原始转义字符(awk 不支持\e),我在inverted colors 中显示换行符\n 以区别于文字@ 987654334@s。这可能无法复制粘贴,在这种情况下您可能需要手动插入它们。或者使用 unicode 符号(例如 )试试运气。

    我偏离了原始的 unicode 符号,因为它们无法在 msysgit 上正确显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2017-11-09
      • 2011-03-18
      • 2016-01-14
      • 2010-09-08
      • 2022-12-10
      • 1970-01-01
      相关资源
      最近更新 更多