Stefano Borini
Stefano Borini

Reputation: 143755

Better control of git log formatting

I have a git log alias like this

log --graph --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %ci %an - %s''

producing

* 123456 2013-03-01 09:45:11 +0100 Name Surname - commit message 1
* 123457 2013-03-01 09:45:11 +0100 Name LongerSurname - commit message 2
* 123458 2013-03-01 09:45:11 +0100 Name Sho - commit message 3

I would like to obtain a different format, namely

* 123456 2013-03-01 09:45:11 Name Surname - commit message 1
* 123457 2013-03-01 09:45:11 Name LongerS - commit message 2
* 123458 2013-03-01 09:45:11 Name Sho     - commit message 3

Notice how the iso8601 is lacking the GMT+1 specification, and how long names are cut and short names are padded to keep the log message aligned.

Is it possible to do this with plain git log? if not, what is the best way to achieve it?

Upvotes: 4

Views: 1144

Answers (1)

chepner
chepner

Reputation: 530823

You can use ANSI escape codes for cursor movement. You'll have to adjust your pager settings as well.

export LESS+=' -r'  # Make sure your pager will accept ANSI escape codes
git log --graph \
  --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %ci %x1b[s%an%x1b[u%x1b[3C - %s'

The escape codes used are as follows:

  1. %x1b[s - save the current cursor position
  2. %x1b[u - restore the cursor position, i.e., move the cursor to where it was when you used %x1b[s
  3. %x1b[3C - move the cursor forward 3 positions (you can change the number to match the number of characters you wnat to display.

After repositioning the cursor using these escape characters, the following text will overwrite the trailing portion of the author name, giving the effect you want.

As for the date, check out the link in the comments: How to change git log date formats

Upvotes: 4

Related Questions