views:

32

answers:

1

Hello,

I have a simple alias to display few last commits:

log --pretty=format:'%h %an %s' -10

How can I make the results to be displayed in columns, like this:

898e8789 Author1             Commit message here
803e8759 Other Author name   Commit message here
+1  A: 

Here's a solution in Bash using read to parse the log lines back apart, and printf to print them out, with a fixed width field for the authors name so the columns will stay lined up. It assumes that | will never appear in the author's name; you can choose another delimiter if you think that might be a problem.

git log --pretty=format:'%h|%an|%s' -10 | 
  while IFS='|' read hash author message
  do 
    printf '%s %-20s %s\n' "$hash" "$author" "$message"
  done

You can create this as an alias using:

[alias]
    mylog = "!git log --pretty=format:'%h|%an|%s' -10 | while IFS='|' read hash author message; do printf '%s %-20s %s\n' \"$hash\" \"$author\" \"$message\"; done"

I'm sure you could do this in fewer characters using awk but as the saying goes,

Whenever faced with a problem, some people say “Lets use AWK.” Now, they have two problems.

Of course, having said that, I had to figure out how to do it in awk which is a bit shorter:

git ... | awk -F '|' '{ printf "%s %-20s %s\n", $1, $2, $3 }'
Brian Campbell