有没有办法自定义 git blame 的输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3958977/
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-19 04:39:13  来源:igfitidea点击:

Is there a way to customize the output of git blame?

gitblame

提问by Rene Saarsoo

git loghas a nice --formatoption to specify how the output should be formatted.

git log有一个很好的--format选项来指定输出的格式。

But git blamedoesn't seem to have an equivalent, although default output of blameis not quite human-friendly. I would like to see much less.

git blame似乎没有等价物,尽管默认输出blame不是很人性化。我希望看到的少得多。

For example, instead of:

例如,而不是:

5600cab7 js/sidebar/VehicleGrid.js        (Rene Saarsoo    2009-10-08 18:55:24 +0000 127)    if (x > y) {
b5f1040c js/map/monitoring/VehicleGrid.js (Mihkel Muhkel   2010-05-31 07:20:13 +0000 128)        return x;

I would like to have:

我想拥有:

5600cab7 Rene Saarsoo (1 year ago)     127:    if (x > y) {
b5f1040c Mihkel Muhkel (5 months ago)  128:        return x;

I figure that I could write a script to parse the output of git blame --porcelainbut given the horrendous default output of blameI feel that somebody out there must have already done something about it.

我想我可以编写一个脚本来解析 的输出,git blame --porcelain但考虑到可怕的默认输出,blame我觉得那里的某个人一定已经对此做了一些事情。

Any ideas? Or any tips for implementing such a script?

有任何想法吗?或者任何实现此类脚本的提示?

Edit:Solved it by writing small script.

编辑:通过编写小脚本解决了它。

回答by Jakub Nar?bski

You can use alternate output format: git annotateor git blame -c.

您可以使用备用输出格式:git annotategit blame -c.

You can change formatting of dates with --date=<format>option (or blame.dateconfig variable), where <format>is one of relative, local, default, iso, rfc, short. See git-blameand git-logmanpages for details.

您可以使用--date=<format>选项(或blame.date配置变量)更改日期格式,其中<format>relative、local、default、iso、rfc、short 之一。有关详细信息,请参阅git-blamegit-log联机帮助页。

回答by Gdalya

For me, the -s flag works, because all I need is the SHA1 ID, after which I look up the commit for details. A script definitely seems like overkill. Is it possible the -s flag was unavailable a year ago?

对我来说,-s 标志有效,因为我需要的只是 SHA1 ID,之后我会查找提交以获取详细信息。脚本绝对看起来有点矫枉过正。一年前 -s 标志是否可能不可用?

回答by VonC

Update Git 2.18 (Q2 2018): "git blame" learns to unhighlight uninteresting metadata from the originating commit on lines that are the same as the previous one, and also paint lines in different colors depending on the age of the commit.

更新 Git 2.18(2018 年第 2 季度):“ git blame”学会了在与前一个相同的行上从原始提交中取消突出显示无趣的元数据,并根据提交的时间用不同的颜色绘制行。

See commit 0dc95a4, commit 25d5f52, commit cdc2d5f(24 Apr 2018) by Stefan Beller (stefanbeller).
(Merged by Junio C Hamano -- gitster--in commit 3d24129, 30 May 2018)

请参阅Stefan Beller ( ) 的commit 0dc95a4commit 25d5f52commit cdc2d5f(2018 年 4 月 24 日(由Junio C Hamano合并-- --commit 3d24129,2018 年 5 月 30 日)stefanbeller
gitster

builtin/blame: add new coloring scheme config

Add a config option that allows selecting the default color scheme for blame. The command line still takes precedence over the configuration.

builtin/blame:添加新的配色方案配置

添加允许选择默认配色方案的配置选项。命令行仍然优先于配置。

git confignow reports:

git config现在报道:

blame.coloring::

This determines the coloring scheme to be applied to blame output.
It can be 'repeatedLines', 'highlightRecent', or 'none' which is the default.

blame.coloring::

这决定了要应用于blame 输出的配色方案。
它可以是“ repeatedLines”、“ highlightRecent”或“ none”,这是默认值。



builtin/blame: highlight recently changed lines

Choose a different color for dates and imitate a 'temperature cool down' depending upon age.

Similarly to the previous patch, this offers the command line option '--color-by-age' to enable this mode and the config option 'color.blame.highlightrecent' to select colors.

内置/责备:突出显示最近更改的行

为日期选择不同的颜色,并根据年龄模仿“降温”。

与之前的补丁类似,这提供了命令行选项“ --color-by-age”来启用此模式和配置选项“ color.blame.highlightrecent”来选择颜色。

The documentationnow adds:

文档现在添加:

color.blame.highlightRecent

This can be used to color the metadata of a blame line depending on age of the line.

This setting should be set to a comma-separated list of color and date settings, starting and ending with a color, the dates should be set from oldest to newest.
The metadata will be colored given the colors if the the line was introduced before the given timestamp, overwriting older timestamped colors.

color.blame.highlightRecent

这可用于根据责任线的年龄为责任线的元数据着色。

此设置应设置为以逗号分隔的颜色和日期设置列表,以颜色开始和结束,日期应设置为从最旧到最新。
如果该行在给定时间戳之前引入,元数据将根据颜色着色,覆盖旧的时间戳颜色。

Instead of an absolute timestamp relative timestamps work as well, e.g. 2.weeks.ago is valid to address anything older than 2 weeks.

相对时间戳也可以工作,而不是绝对时间戳,例如 2.weeks.ago 可以有效地解决超过 2 周的任何事情。

It defaults to 'blue,12 month ago,white,1 month ago,red', which colors everything older than one year blue, recent changes between one month and one year old are kept white, and lines introduced within the last month are colored red.

它默认为“ blue,12 month ago,white,1 month ago,red”,将超过一年的所有内容着色为蓝色,最近一个月到一年之间的变化保持白色,上个月内引入的线条为红色。



builtin/blame: dim uninteresting metadata lines

When using git-blame, lots of lines contain redundant information, for example in hunks that consist of multiple lines, the metadata (commit name, author, date) are repeated.
A reader may not be interested in those, so offer an option to color the information that is repeated from the previous line differently.
Traditionally, we use CYAN for lines that are less interesting than others (e.g. hunk header), so go with that.

The command line option '--color-lines' will trigger the coloring of repeated lines, and the config option 'color.blame.colorLines' is provided to select the color.
Setting the config option doesn't imply that repeated lines are colored.

builtin/blame: 暗淡无趣的元数据行

使用时git-blame,很多行包含冗余信息,例如在由多行组成的大块中,元数据(提交名称、作者、日期)会重复。
读者可能对这些不感兴趣,因此提供一个选项来为上一行重复的信息着色。
传统上,我们将 CYAN 用于比其他线条更不有趣的线条(例如大块头),所以就这样吧。

命令行选项 ' --color-lines' 将触发重复行的着色,并color.blame.colorLines提供配置选项 ' ' 来选择颜色。
设置 config 选项并不意味着重复的行是彩色的。



Original answer 2010

原始答案 2010

Considering web interface like Trac or Redmine integrate git blameresults, I suppose such a parsing has already been done.

考虑到像 Trac 或 Redmine 这样的 Web 界面集成git blame结果,我想这样的解析已经完成了。

You can see in this Redmine Defect 3832an example with this ruby script:

您可以在Redmine Defect 3832 中看到这个 ruby​​ 脚本的示例:

回答by elquimista

Since git logprovides way more customization options for output, you can combine git blame, awk, xargsand git logto achieve what you want. E.g.

由于git log输出方式提供更多自定义选项,你可以结合git blameawkxargsgit log达到你想要的。例如

git --no-pager blame <filepath> -L1,+1 --porcelain | awk 'NR==1 {print }' | xargs git --no-pager log -1 --pretty=format:"%h - (%cd) %s - %an" --date=relative

This outputs something like this:

这会输出如下内容:

f8a66e80c - (5 months ago) Add gem: devise - elquimista

Basically what git blameand awkdoes above is get a full commit SHA, and xargspasses it to git logas an argument.

基本上git blameawk上面所做的是获取完整的提交 SHA,xargs并将其git log作为参数传递给。

回答by imaliazhar

You can add this to your .gitconfig to get relative time (n days ago etc)

您可以将此添加到您的 .gitconfig 以获取相对时间(n 天前等)

[blame]
    date = human