在 git-diff 的输出中为空白着色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5257553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:19:33  来源:igfitidea点击:

Coloring white space in git-diff's output

gitcolorsdiffwhitespace

提问by radarek

Regarding code formatting I'm kind of purist :). I very often remove unnecessary white spaces (lines with only ws, ws at the end of lines etc). I even have set vim to show that kind of lines colored to red.

关于代码格式,我有点纯粹:)。我经常删除不必要的空格(只有 ws 的行,行尾的 ws 等)。我什至设置了 vim 来显示那种颜色为红色的线条。

My problem is that using git-diff I often see something like this:

我的问题是使用 git-diff 我经常看到这样的事情:

-      else{ 
+      else{

Even if I have git-diff colored I can't see difference (in that particular situation I removed 1 ws at the end of line). Is there any way to tell git-diff to show that ws colored to red? (for example those matched with /\s+$/ regexp).

即使我有 git-diff 颜色,我也看不到差异(在那种特殊情况下,我在行尾删除了 1 ws)。有没有办法告诉 git-diff 显示 ws 颜色为红色?(例如与 /\s+$/ regexp 匹配的那些)。

回答by Mark Longair

You may need to set the color.diff.whitespace config setting, e.g. with:

您可能需要设置 color.diff.whitespace 配置设置,例如:

 git config color.diff.whitespace "red reverse"

(I'm assuming that you already have color.diffor color.uiset to autosince you say that you see coloured patches from git diffanyway.)

(我假设您已经拥有color.diffcolor.ui设置了,auto因为您说git diff无论如何您都会看到彩色补丁。)

If you want to fine tune the type of whitespace errors that are highlighted in red, you can then change core.whitespace, but blank-at-eolis enabled by default so you probably won't need to change that for the example you mention.

如果您想微调以红色突出显示的空白错误类型,您可以更改core.whitespace,但blank-at-eol默认情况下已启用,因此对于您提到的示例,您可能不需要更改它。

A possible source of confusion is that in the output of git diff, whitespace errors are only highlighted in the lines that are introduced, not those that are removed. (Update:as Paul Whittaker points out in his answer, which you should up-vote :), you can see these by reversing the sense of the diff with git diff -R.)

一个可能的混淆来源是,在 的输出中git diff,空白错误仅在引入的行中突出显示,而不是在删除的行中突出显示。(更新:正如 Paul Whittaker在他的回答中指出的,你应该投赞成票 :),你可以通过用 反转 diff 的意义来看到这些git diff -R。)

You can find more documentation on these config options in the git config man page

您可以在git config 手册页中找到有关这些配置选项的更多文档

If you don't want to use the -Rkludge you can use the WhiteSpace Error Highlightoption from the diff man page.

如果您不想-R使用kludge,您可以使用diff 手册页中WhiteSpace Error Highlight选项。

--ws-error-highlight=

Highlight whitespace errors on lines specified by in the color specified by color.diff.whitespace. is a comma separated list of old, new, context. When this option is not given, only whitespace errors in new lines are highlighted. E.g. --ws-error-highlight=new,old highlights whitespace errors on both deleted and added lines. all can be used as a short-hand for old,new,context.

--ws-error-highlight=

以 color.diff.whitespace 指定的颜色突出显示由 指定的行上的空白错误。是旧的、新的、上下文的逗号分隔列表。如果未给出此选项,则仅突出显示新行中的空白错误。例如 --ws-error-highlight=new,old 突出显示删除和添加行上的空白错误。all 可以用作旧的、新的、上下文的简写。

git diff --ws-error-highlight=new,old <file>

git diff --ws-error-highlight=new,old <file>

or

或者

git diff --ws-error-highlight=all <file>

git diff --ws-error-highlight=all <file>

I don't know of a way to permanently turn this on and store this in config aside from using an alias:

除了使用别名之外,我不知道有什么方法可以永久打开它并将其存储在配置中:

git config alias.df 'diff --ws-error-highlight=all'

git config alias.df 'diff --ws-error-highlight=all'

Now you can use:

现在您可以使用:

git df <file>

git df <file>

To see the changes in red.

看到红色的变化。

Note that with Git 2.11 (Q4 2016), this alias might be replaced with:

请注意,在 Git 2.11 (Q4 2016) 中,此别名可能会替换为:

git config diff.wsErrorHighlight all

See doc on git diffand on git config.

请参见上的文档git diffgit config

回答by Paul Whittaker

Use git diff -Rto turn removed lines into added lines. Then trailing whitespace will be highlighted.

使用git diff -R把删除的行为添加的行。然后将突出显示尾随空格。

(This assumes you already have whitespace hightlighting enabled, as per the colour settings from Mark's answer. Credit for this method goes to Junio's post at http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html.)

(这假设您已经根据 Mark 回答中的颜色设置启用了空白高亮显示。此方法的功劳归功于 Junio 在http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal- with-git-diff-td5653205.html。)

For example, when converting a file from DOS line endings to Unix, git diff -Rclearly shows me the ^Mcharacters (dis)appearing at the ends of lines. Without -R(and also without -wetc.) it shows that the entire file has changed, but doesn't show how.

例如,将文件从 DOS 行尾转换为 Unix 时,会git diff -R清楚地向我^M显示出现在行尾的字符 (dis)。没有-R(也没有-w等)它表明整个文件已经改变,但没有显示如何。

回答by Kelvin

Use git diff --color | less -R. The -Rmakes the color control codes human-friendly.

使用git diff --color | less -R. 这-R使得颜色控制代码人性化。

Then you can use less's regular expression search, e.g.

然后你可以使用less's 的正则表达式搜索,例如

/[[:space:]]+$

回答by nickgrim

My version of git diffalready seems to do this - I have git 1.7.4.1 and have set color.ui = auto.

我的版本git diff似乎已经这样做了 - 我有 git 1.7.4.1 并且已经设置了color.ui = auto.