Is there a way to customize the output of git blam

2019-03-10 22:56发布

问题:

git log has a nice --format option to specify how the output should be formatted.

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

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 --porcelain but given the horrendous default output of blame I feel that somebody out there must have already done something about it.

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

Edit: Solved it by writing small script.

回答1:

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

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



回答2:

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?



回答3:

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.

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)

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.

git config now reports:

blame.coloring::

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


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.

The documentation now 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.

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

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.


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.


Original answer 2010

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

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

  • git_adapter.rb


回答4:

Since git log provides way more customization options for output, you can combine git blame, awk, xargs and git log to achieve what you want. E.g.

git --no-pager blame <filepath> -L1,+1 --porcelain | awk 'NR==1 {print $1}' | 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 blame and awk does above is get a full commit SHA, and xargs passes it to git log as an argument.



标签: git blame