在 git-diff 的输出中为空白着色 [英] Coloring white space in git-diff's output

查看:39
本文介绍了在 git-diff 的输出中为空白着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

-      else{ 
+      else{

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

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).

推荐答案

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

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

git config color.diff.whitespace "red reverse"

(我假设您已经将 color.diffcolor.ui 设置为 auto 因为您说您看到了彩色来自 git diff 的补丁.)

(I'm assuming that you already have color.diff or color.ui set to auto since you say that you see coloured patches from git diff anyway.)

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

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-eol is enabled by default so you probably won't need to change that for the example you mention.

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

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 配置手册页中找到有关这些配置选项的更多文档

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

如果您不想使用 -R 组合,您可以使用 diff 手册页.

If you don't want to use the -R kludge you can use the WhiteSpace Error Highlight option from the diff man page.

--ws-error-highlight=

--ws-error-highlight=

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

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.

git diff --ws-error-highlight=new,old

git diff --ws-error-highlight=all

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

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 df <文件>

以红色查看变化.

请注意,使用 Git 2.11(2016 年第 4 季度),此别名可能替换为:

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

git config diff.wsErrorHighlight all

请参阅 git 上的文档diffgit config 上.

See doc on git diff and on git config.

这篇关于在 git-diff 的输出中为空白着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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