如何让git-diff忽略所有的空白变化而忽略一个?

时间:2015-12-18 07:09:21

标签: git whitespace git-diff

another post我找到了非常整洁的

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

可以很好地将git-diff的输出压缩到基本输出,同时保持清晰(特别是在为--word-diff=plain / [-和{{1}添加-]时} / {+周围的删除/添加)。虽然此 包含空格更改,但输出不会以任何明显的方式突出显示它们,例如更改代码行的缩进(这是一个严重的更改)将显示为具有较长缩进(前或后)的行,但没有任何突出显示。

如何正确地突出空白变化,可能是通过某些unicode字符替换空格,例如+}·,或更接近'的字符s git diff --word-diff-regex=.等,但更聪明的单词分离?

3 个答案:

答案 0 :(得分: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试图在这里专门处理空白,这可能是一个问题。

答案 1 :(得分:1)

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

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

请注意^前面已移除的[:space:]

答案 2 :(得分:1)

这是使用问题结尾处建议的替代的替代方案:

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

这样可以正确显示unicode符号,而不是一些奇怪的<c3> 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'

请注意awk之前有原始转义(\e无法支持[)个字符,我会在{{}显示换行符\n {3}}与文字\n区别开来。此可能无法复制粘贴,在这种情况下,您可能需要手动插入它们。或者尝试使用unicode符号,例如

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