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

发布于 2024-10-20 22:53:16 字数 283 浏览 8 评论 0 原文

关于代码格式,我是纯粹主义者:)。我经常删除不必要的空格(仅包含 ws 的行、行尾的 ws 等)。我什至将 vim 设置为显示那种红色的线条。

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

-      else{ 
+      else{

即使我有 git-diff 颜色,我也看不到差异(在这种特殊情况下,我在行尾删除了 1 ws)。有什么方法可以告诉 git-diff 显示 ws 颜色为红色吗? (例如与 /\s+$/ 正则表达式匹配的那些)。

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你在看孤独的风景 2024-10-27 22:53:16

使用Git 2.11(2016 年第 4 季度) 及更高版本,您可以执行以下操作:

git config diff.wsErrorHighlight all

请参阅关于 git diff关于git配置


对于早于该版本的版本,您可以设置 color.diff.whitespace 配置设置,例如:(

git config color.diff.whitespace "red reverse"

我假设您已经有 color.diffcolor.ui 设置为 auto 因为你说你无论如何都能看到来自 git diff 的彩色补丁。)

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

一个可能造成混淆的原因是,在 git diff 的输出中,空白错误仅在引入的行中突出显示,而在删除的行中则不突出显示。 (更新:正如 Paul Whittaker 在他的回答中指出的那样,您应该投票: ),您可以通过使用 git diff -R 反转 diff 的含义来查看这些内容。)

您可以在 git config 手册页

如果您不想使用 -R 组合,您可以使用 WhiteSpace diff 手册页中的错误突出显示选项。

--ws-error-highlight=

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

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

git diff --ws-error-highlight=all

对于旧版本从 2.11 开始,除了使用别名之外,没有办法永久打开此功能并将其存储在配置中:

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

现在您可以使用:

git df

查看红色的变化。

With with Git 2.11 (Q4 2016) and after, you can do:

git config diff.wsErrorHighlight all

See doc on git diff and on git config.


For versions older than that, you can set the color.diff.whitespace config setting, e.g. with:

git config color.diff.whitespace "red reverse"

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

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.

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

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

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=

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 <file>

or

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

With versions older than 2.11, there’s no 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'

Now you can use:

git df <file>

To see the changes in red.

弱骨蛰伏 2024-10-27 22:53:16

使用 git diff -R 将删除的行变成添加的行。然后尾随空白将突出显示。

(这假设您已经启用了空白突出显示,根据 Mark 答案中的颜色设置。此方法的功劳来自 Junio 的帖子 http://git.661346.n2.nabble.com/Highlighting-whitespace-on-removal-with-git-diff-td5653205.html .)

例如,当将文件从 DOS 行结尾转换为 Unix 时,git diff -R 清楚地显示了出现在末尾的 ^M 字符(dis)线。如果没有 -R (也没有 -w 等),它会显示整个文件已更改,但不会显示如何更改。

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

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

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

蓬勃野心 2024-10-27 22:53:16

对于懒惰的答案浏览器,只需运行:

git config --global diff.wsErrorHighlight all

然后 git diff 也会突出显示已删除行中的尾随空格。

For the lazy answer skimmers, just run:

git config --global diff.wsErrorHighlight all

Then git diff will highlight trailing whitespaces in removed lines as well.

苏大泽ㄣ 2024-10-27 22:53:16

使用 git diff --color |少-R。 -R 使颜色控制代码变得人性化。

然后就可以使用less的正则表达式搜索,eg

/[[:space:]]+$

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

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

/[[:space:]]+$
冰葑 2024-10-27 22:53:16

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

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

回首观望 2024-10-27 22:53:16

上面提到的命令也可以包含在gitconfig中
而不是将

git config ....

相应的选项添加到例如 ~/.gitconfig 中

[diff]
    wsErrorHighlight = all

[color]
    ui = auto

文件作为选项,即上面的两个选项应该可以解决问题, 。我测试的最早的 git 版本是 1.7,但也应该适用于该版本之后的任何版本。在最新版本的 git 上,“[color]”选项默认设置为“auto”。

The command mentioned above can also be included in the gitconfig
file as options, i.e instead of

git config ....

adding the corresponding options to e.g ~/.gitconfig

[diff]
    wsErrorHighlight = all

[color]
    ui = auto

the two options above should do the trick. The earliest git version I tested this was 1.7 but should work for any version after that too. On recent versions of git the "[color]" options is set to "auto" by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文