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)'
Michael
  • 8,362
  • 6
  • 61
  • 88

4 Answers4

6

%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

Michael
  • 8,362
  • 6
  • 61
  • 88
macksold
  • 76
  • 1
  • 3
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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • +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? – Michael Mar 14 '14 at 22:11
  • 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. :-) – torek Mar 14 '14 at 22:24
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.

Michael
  • 8,362
  • 6
  • 61
  • 88
eddiemoya
  • 6,713
  • 1
  • 24
  • 34
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.

Michael
  • 8,362
  • 6
  • 61
  • 88