4

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?

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • 1
    have you checked http://stackoverflow.com/q/7853332/11343 ? – CharlesB Mar 04 '13 at 15:20
  • 1
    Just a side note. You do realize that `--graph` will break the alignment when there are different branches, right? Also tags have this effect. And pointers (e.g. `HEAD`) – Shahbaz Mar 04 '13 at 17:33
  • @Shahbaz: I use a very linear history, so the circumstance will be rare. In any case, I am ready to drop graph if necessary. – Stefano Borini Mar 04 '13 at 20:55

1 Answers1

4

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

Community
  • 1
  • 1
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I'm surprised git doesn't support flags, width and precision as in C's `printf`. By the way, if `%an` is very long and `%s` is short, wouldn't you see the rest of the name after the commit message? Could you make it cleaner by making it clear the rest of the line after `%s`? – Shahbaz Mar 04 '13 at 17:40
  • While true, it might serve as a reminder that your commit messages aren't very good :) However, try adding "%x1bK" after the "%s"; I think this should clear from the current cursor position to the end of the line. – chepner Mar 04 '13 at 18:37
  • not that it would ever happen to me, but I just said it since you know, as a programmer you're used to finding bugs ;) – Shahbaz Mar 05 '13 at 09:07