4

How can I specify maximum field lengths with git log? I want the output columns to be aligned.

  • If the specified width is less than the number of characters in the field, the field is not truncated. The columns become mis-aligned. – user2494490 Jun 17 '13 at 20:30

2 Answers2

9

You can specify the column width without using ANY external tool at all:

git log --format="%<(25,trunc)%ad | %<(25,trunc)%s | %<(25,trunc)%an"

There are TONs of other options, e.g.

%<|(25)

to align the output to columns. You can format columns to be left bound, right bound or even centered and so on: https://git-scm.com/docs/pretty-formats

Unfortunately I do not know when this has been added to git, I am using 2.10.1 on Windows…

MarkusL
  • 137
  • 1
  • 5
0

If you are in Windows, you could use a PowerShell script. Format the log with a special character between outputs, create an object from that and pipe into Format-Table, where you can specify Width:

git log --format="%ad|%s|%an" | ForEach-Object {
  New-Object PSObject -Property @{
    Time = $_.Split('|')[0]
    Message = $_.Split('|')[1]
    Author = $_.Split('|')[2]
  }
} | Format-Table -Property @{Expression={$_.Time};width=25;Label="Author date"}, 
                           @{Expression={$_.Message};Width=25;Label="Commit message"}, 
                           @{Expression={$_.Author};Width=11;Label="Author name"}

Sample output:

Author date               Commit message            Author name
-----------               --------------            -----------
Sun Jun 16 12:49:03 20... added rand content 60 ... Bonke
Sun Jun 16 12:46:56 20... added rand content 61 ... Bonke
Sun Jun 16 12:46:37 20... change                    Bonke
Wed Apr 24 22:41:44 20... added rand content 17 ... Klas Mel...
Wed Apr 24 22:40:16 20... added rand content 8 t... Klas Mel...

If you want to do it in bash instead here is the analogous script in bash (inspired by the answer to Git log tabular formatting):

git log --pretty=format:'%ad|%s|%an' | 
  while IFS='|' read time message author
  do 
    printf '%.25s %.25s %.11s\n' "$time" "$message" "$author"
  done

sample output

Sun Jun 16 12:49:03 2013  added rand content 60 to  Bonke
Sun Jun 16 12:46:56 2013  added rand content 61 to  Bonke
Sun Jun 16 12:46:37 2013  change                    Bonke
Wed Apr 24 22:41:44 2013  added rand content 17 to  Klas Mellbo
Wed Apr 24 22:40:16 2013  added rand content 8 to . Klas Mellbo
Community
  • 1
  • 1
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • When I use the bash version the output is not lining up if the subject is less than 25 characters. – user2494490 Jun 17 '13 at 20:58
  • @user2494490 try using `%-25.25` instead of `%.25` – Klas Mellbourn Jun 17 '13 at 21:33
  • Thanks! This works well, but I've lost the paging--the entire log prints now. I piped the output to less, but that scrolls the first line out of view. How to get back the original functionality where I can press the spacebar to page? – user2494490 Jun 18 '13 at 03:25