Optional indented newline in git log format
Asked Answered
S

4

5

I have been tinkering with git aliases for some log commands. I have most of what I'd like (credit here), but I'm having trouble with one piece. When I call…

git log --graph --format=format:'%h - [%ar] %s%+d'

…I get…

* ab123f - [6 hours ago] Fix the references
|  (HEAD, origin/master, master)
* bc123f - [8 hours ago] New build syntax
* cd123f - [10 hours ago] Initial import

…where %+d adds a new line and puts the --decorate tags on it if they exist. I would rather have the tags to be in line with the time stamp instead, like so:

* ab123f - [6 hours ago] Fix the references
|          (HEAD, origin/master, master)
* bc123f - [8 hours ago] New build syntax
* cd123f - [10 hours ago] Initial import

How do I accomplish this? I do not want a bonus newline if there are no --decorate tags. I've been experimenting with various format placeholders: %+d, %-d, %+ d (which doesn't work); permutations of %>(<N>), %>>(<N>); and so on, but I can't get it to do what I want.

Colors and further commit info had been removed for simplicity, but they seem to interfere with torek's answer. The full command is below:

git log --graph --format=format:'%C(bold yellow)%h%C(reset) - %C(green)(%ar)%C(reset) %s %C(white)<%an>%C(reset)%C(auto)%+d%C(reset)'
Shankle answered 14/3, 2014 at 20:41 Comment(0)
P
7

%w as follows seems to do the trick.

git log --graph --format=format:'%h - [%ar] %-s%w(0,0,9)%+d'

Git version 1.8.5.2

Porcelain answered 25/7, 2014 at 18:33 Comment(0)
F
1

Ugh ... using %n%-... almost seems to work, but I still can't get it to do the right thing here.

Aha! '%h - [%ar] %s%n%-w(80,9)%+d' works!

This seems terribly clumsy (add a newline, maybe remove it, wrap lines with indent at 9, add a newline if %d is not empty), but the more obvious versions (with %+w, or %w...%+d without the %n and %- parts) don't work.

Finally answered 14/3, 2014 at 21:35 Comment(2)
+1 I can confirm that this works for the test case provided. Unfortunately, it seems to choke on the full format with colors. I end up with a cute little [m at the end of the first line and no second line for reasons that are unclear to me. Do you know why that is?Shankle
No ... the %-language here is a nice idea, and simple things "just work", but beyond that, it sure gets messy. I can't even explain why the part that works, works. :-)Finally
D
0

You could also use awk—but the columns approach above is cleaner. This might take it all the way to what your looking for.

git log --format=format:'%h - [%ar%] %s %d' | awk -F'(' '{print $1} {if ( $2 != "" ) print "\t  ("$2}'

On the upside, this seems to preserve colors for me.

Deactivate answered 14/3, 2014 at 22:59 Comment(0)
S
0

For curious future visitors, these are what I eventually settled upon:

[alias]
    # Pretty logs
    lg1 = log --graph --date=auto:human --format=tformat:'%C(bold yellow)%h%C(reset) %C(green)%ad%C(reset) %s %C(dim white)%aN%C(reset) %w(0,0,9)%C(auto)%+d%C(reset)'
    lg2 = log --graph --format=tformat:'%C(bold yellow)%h%C(reset) %C(cyan)%aD%C(reset) %C(green)(%ar) %C(dim white)%aN%C(reset)%w(0,0,9)%n %C(brightwhite)%s%C(auto)%+d%C(reset)%w(0,0,10)%+b'
    lg = lg1

    # Monochrome log
    lgs = log --graph --format=format:'%h - [%ar] %s%n%-w(80,9)%+d'
  • git lg or git lg1 for a git log --oneline analogue
  • git lg2 to include the body and full date (but also fuzzy date)
  • git lgs as a simpler fallback in case the colors break

The colors do enough work to distinguish the parts that I no longer wrap most names and times. Also, --date=auto:human wasn't around for the original question, but I like it a lot.

Shankle answered 23/1, 2023 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.