如何让 git-diff 忽略所有空格更改但领先一个? [英] How to have git-diff ignore all whitespace-change but leading one?

查看:35
本文介绍了如何让 git-diff 忽略所有空格更改但领先一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一篇文章中,我发现非常整洁

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

在将 git-diff 的输出压缩到基本的同时保持清晰(特别是在添加 --word-diff=plain 以获取额外的 --word-diff=plaincode>[-/-]{+/+} 周围的删除/添加).虽然这确实包括空格更改,但输出不会以任何明显的方式突出显示它们,例如更改一行 代码的缩进时(这是一个严重的变化)将显示为带有更长缩进(之前或之后)的那条线,但没有任何突出显示.

which does a great job at compressing git-diff's output to the essential while remaining legible (especially when adding --word-diff=plain for additional [-/-] and {+/+} surrounding deletions/additions). While this does include whitespace changes, the output does not highlight them in any noticeable way, e.g. when changing the indentation of a line of python code (which is a severe change) will show up as that line with the longer indentation (before or after), but there is no highlighting whatsoever.

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

How can whitespace changes be hightlighted correctly, maybe by replacing whitespace by some unicode characters such as ·, and , or something more close to git diff --word-diff-regex=.'s {+ +} etc but with the smarter word separation?

推荐答案

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

Here's an alternative using the substitution suggested at the question's end:

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

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

such that unicode symbols are displayed correctly instead of some weird <c3>ish output. Add the following to your git configuration:

[diff "txt"]
    textconv = unwhite.sh

并且,缺乏全局解决方案.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 以区别它们文字 \n s.这可能无法复制粘贴,在这种情况下,您可能必须手动插入它们.或者试试你的运气,使用 unicode 符号,例如 .

Be advised there are raw escape (awk fails to support \e) characters before the [s, I display the newline-indicating \n in inverted colors to differ them from literal \ns. This may fail to copy paste, in which case you may have to manually insert them. Or try your luck with a unicode symbol such as instead.

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

I deviated from the original unicode symbols since they failed to display correctly on msysgit.

这篇关于如何让 git-diff 忽略所有空格更改但领先一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆