git-log 中的颜色

发布于 2024-11-05 09:59:21 字数 422 浏览 5 评论 0原文

当您运行 git log --decorate --pretty=oneline 时,输出将包含诸如 (HEAD, refs/published/master, master) 之类的带有颜色的条目。

我的 gitconfig 中还有以下内容:

[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green

在执行如下所示的自定义格式时,如何复制这些颜色?

git log --decorate --stat --graph --pretty=format:"%d %Cgreen%h%Creset (%ar - %Cred%an%Creset), %s%n"

When you run git log --decorate --pretty=oneline the output will have entries like (HEAD, refs/published/master, master) with coloration.

I also have the following in my gitconfig:

[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green

How do you replicate those colors when doing a custom format like the following?

git log --decorate --stat --graph --pretty=format:"%d %Cgreen%h%Creset (%ar - %Cred%an%Creset), %s%n"

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

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

发布评论

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

评论(5

月光色 2024-11-12 09:59:21

从 git 1.8.3(2013 年 5 月 24 日)开始,您可以在 git log< 的格式字符串中使用 %C(auto) 修饰 %d /代码>。

来自发行说明

 * "git log --format" specifier learned %C(auto) token that tells Git
   to use color when interpolating %d (decoration), %h (short commit
   object name), etc. for terminal output.)

As of git 1.8.3 (May 24, 2013), you can use %C(auto) to decorate %d in the format string of git log.

From the release notes:

 * "git log --format" specifier learned %C(auto) token that tells Git
   to use color when interpolating %d (decoration), %h (short commit
   object name), etc. for terminal output.)
南巷近海 2024-11-12 09:59:21

git log --decorate 默认情况下:

  • HEAD 为青色,
  • 远程分支为红色,
  • 标签为绿色

,可以通过 color.decorate< /strong> 配置。

git log --format 没有提供一种专门显示 HEAD 远程 分支的方法:所有三个都通过 %d 显示,其中一种颜色可能。


2013 年 5 月更新,如下文所述,作者:Elad Shahar(已投票),git 1.8.3 提供了另一种选择:

git log –format 现在带有一个 %C(auto) 标记,告诉 Git 在解析 %d 时使用颜色(装饰) 、%h(短提交对象名称)等用于终端输出。

这篇Atlassian 博客文章评论说此功能是其中的一部分 关注格式(git rebasegit count-objects)和颜色(git Branch -vv

其他几个重点 之前的自动,重置 1.8.2,当输出不用于终端时自动禁用颜色1

%C(auto,blue)Hello%C(auto,reset)

注意:git 2.4+(2015 年第 2 季度)将更好地重置分支名称周围的颜色。
请参阅 提交 5ee8758,作者:Junio C Hamano (gitster)

log --decorate:不要将“提交”颜色泄漏到下一个项目

在“git log --decorate”中,您将看到如下提交标头:

commit ... (HEAD, jc/decorate-leaky-separator-color)

其中“commit ... (”用color.diff.commit绘制,“HEAD”用color绘制。 Decorate.head, ", " in color.diff.commit 中的分支名称
color.decorate.branch,然后关闭 color.diff.commit 中的“)”。

如果您想将 HEAD 和本地分支名称绘制为与正文相同的颜色(可能是因为青色和绿色在黑白终端上太淡而难以读取),您不会希望说

[color "decorate"]
    head = black
    branch = black

因为您将无法在黑底白字终端上重复使用相同的配置。你会天真地期望

[color "decorate"]
    head = normal
branch = normal

可以工作,但不幸的是没有。
它将字符串“HEAD”和分支名称绘制为与装饰元素之间的左括号或逗号相同的颜色。
这是因为代码在用自己的颜色打印“前缀”后忘记重置颜色。


请注意,git 2.5(2015 年第 2 季度)修复了一个错误:

请参阅 commit 429ad20 ="https://github.com/gitster" rel="noreferrer">Junio C Hamano (gitster),2015 年 5 月 13 日。
(由 Junio C Hamano -- gitster -- 合并于 提交 fd70780,2015 年 5 月 22 日)

log:不要过早缩短装饰名称

Git 2.4 中的“log --decorate”增强功能显示当前分支顶端的提交,例如“HEAD -> master”,但没有与 --decorate=full 一起使用。


Git 2.9.x+(2016 年第 3 季度)将修复另一个错误并%C 荣誉 color=auto (自动)


Git 2.10.2(2016 年 10 月)使用 提交 82b83da(2016 年 9 月 29 日),以及提交 c99ad27(9 月 17 日) 2016),作者:René Scharfe (``)
(由 Junio C Hamano -- gitster -- 合并于 提交 76796d4,2016 年 10 月 28 日)

pretty:如果输出为空,请避免为 %C(auto) 添加重置

我们发出一个转义序列来重置 %C(auto) 的颜色和属性,以确保自动着色按预期显示。
如果输出 strbuf 为空,即当 %C(auto) 出现在格式字符串的开头时,请停止执行此操作,因为这样就不需要重置,并且我们在输出中保存了几个字节。

pretty:让%C(auto)重置所有属性

%C(auto) 上重置颜色和属性以启用全自动
对他们的控制; 否则粗体或反向等属性可能会
之前的 %C 占位符
仍然有效。

The git log --decorate will put by default:

  • the HEAD in cyan
  • the remote branches in red
  • the tag in green

and can be changed through color.decorate config.

But the git log --format don't offer a way to display specifically the HEAD or remotes or branch: all three are displayed through %d, with one color possible.


Update May 2013, as mentioned below by Elad Shahar (upvoted), git 1.8.3 offers one more option:

git log –format now sports a %C(auto) token that tells Git to use color when resolving %d (decoration), %h (short commit object name), etc. for terminal output.

This Atlassian blog post comments that this feature is part of several others focused on format (git rebase, git count-objects) and colors (git branch -vv)

This comes in addition of the previous auto,reset of 1.8.2, which automatically disables colors when the output is not used for a terminal1

%C(auto,blue)Hello%C(auto,reset)

Note: git 2.4+ (Q2 2015) will do a better job of resetting color around branch names.
See commit 5ee8758 by Junio C Hamano (gitster):

log --decorate: do not leak "commit" color into the next item

In "git log --decorate", you would see the commit header like this:

commit ... (HEAD, jc/decorate-leaky-separator-color)

where "commit ... (" is painted in color.diff.commit, "HEAD" in color.decorate.head, ", " in color.diff.commit, the branch name in
color.decorate.branch and then closing ")" in color.diff.commit.

If you wanted to paint the HEAD and local branch name in the same color as the body text (perhaps because cyan and green are too faint on a black-on-white terminal to be readable), you would not want to have to say

[color "decorate"]
    head = black
    branch = black

because that you would not be able to reuse same configuration on a white-on-black terminal. You would naively expect

[color "decorate"]
    head = normal
branch = normal

to work, but unfortunately it does not.
It paints the string "HEAD" and the branch name in the same color as the opening parenthesis or comma between the decoration elements.
This is because the code forgets to reset the color after printing the "prefix" in its own color.


Note that git 2.5 (Q2 2015) fixes a bug:

See commit 429ad20 by Junio C Hamano (gitster), 13 May 2015.
(Merged by Junio C Hamano -- gitster -- in commit fd70780, 22 May 2015)

log: do not shorten decoration names too early

The "log --decorate" enhancement in Git 2.4 that shows the commit at the tip of the current branch e.g. "HEAD -> master", did not work with --decorate=full.


Git 2.9.x+ (Q3 2016) will fix another bug and honor color=auto for %C(auto)


Git 2.10.2 (Oct. 2016) fixes other bugs with commit 82b83da (29 Sep 2016), and commit c99ad27 (17 Sep 2016) by René Scharfe (``).
(Merged by Junio C Hamano -- gitster -- in commit 76796d4, 28 Oct 2016)

pretty: avoid adding reset for %C(auto) if output is empty

We emit an escape sequence for resetting color and attribute for %C(auto) to make sure automatic coloring is displayed as intended.
Stop doing that if the output strbuf is empty, i.e. when %C(auto) appears at the start of the format string, because then there is no need for a reset and we save a few bytes in the output.

pretty: let %C(auto) reset all attributes

Reset colors and attributes upon %C(auto) to enable full automatic
control over them; otherwise attributes like bold or reverse could
still be in effect from previous %C placeholders
.

好久不见√ 2024-11-12 09:59:21

将它们括起来:

%C(...):颜色规范,如 color.branch.* 配置选项中所述

因此 %C(黄色反转) 可以工作。

(颜色规范文档。)

Parenthesize them:

%C(...): color specification, as described in color.branch.* config option

So %C(yellow reverse) would work.

(Color specification docs.)

撞了怀 2024-11-12 09:59:21

配置选项log.decorate可以启用/禁用日志中的默认装饰。

git config --global log.decorate full

完成后,您可以使用 color.decorate.* 来调整颜色

The config option log.decorate can enable/disable default decorations in logs.

git config --global log.decorate full

Once that is done you can use color.decorate.* to play with the colors

苍白女子 2024-11-12 09:59:21

有些人可能想使用这个:%C(colorname)
这不需要更改颜色配置。

示例:将作者姓名涂成黄色

--pretty=format:"%C(yellow)%an%Creset"

常规 ANSI 颜色应该有效 https://en .wikipedia.org/wiki/ANSI_escape_code

  • 黑色
  • 红色
  • 绿色
  • 黄色
  • 蓝色
  • 品红色
  • 青色
  • 白色

Some may want to use this : %C(colorname)
This doesn't need to change the color config.

Example : Coloring the author name in yellow

--pretty=format:"%C(yellow)%an%Creset"

Regular ANSI colors should work https://en.wikipedia.org/wiki/ANSI_escape_code

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