Git日志表格格式

时间:2010-09-02 20:33:29

标签: git bash scripting

我有一个简单的别名来显示最后的提交:

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

如何将结果显示在列中,如下所示:

898e8789 Author1             Commit message here
803e8759 Other Author name   Commit message here

6 个答案:

答案 0 :(得分:51)

在Git 1.8.3及更高版本中,pretty format中有本机支持,使用%<(N)语法将下一个占位符格式化为 N 列:

$ git log --pretty=format:'%h %<(20)%an %s' -10

对于1.8.3之前的版本,此答案的上一版本(如下所示)应该有效。

这是Bash中的一个解决方案,使用read将日志行分开解析,并printf将它们打印出来,并为作者姓名提供固定宽度字段,以便列保持排列。它假定|永远不会出现在作者的名字中;如果您认为可能存在问题,可以选择另一个分隔符。

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

您可以使用以下命令将其创建为别名:

[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"

我确信您可以使用awk以更少的字符执行此操作,但正如the saying所做的那样,

  

每当遇到问题时,有人会说“让我们使用AWK。”现在,他们有两个问题。

当然,话虽如此,我必须弄清楚如何在awk中做一些更短的事情:

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

答案 1 :(得分:35)

你也可以这样做:

git log --pretty=format:'%h %<(20)%an %s' -10

不需要使用awkcolumn等进行shell魔术或后处理。

答案 2 :(得分:15)

以下命令将以表格形式打印日志

git log --pretty=format:'%h|%an|%s' -10 | column -t -s '|'

column命令“列出”输出,使用“|”作为字段分隔符,它将在给定输入的情况下找出最佳列宽,因此即使您有更多字段也能正常工作。

在Linux上,只要你不使用颜色就可以正常工作,Linux实现不能很好地处理ANSI escape codes

但是在Mac OS X中,它会处理颜色,你可以使用任何unicode字符作为字段分隔符。我使用Σ,因为我很确定它不会偶然发生在提交文本上。 |是一个糟糕的选择,因为它可能出现在提交说明中,如果您使用--graph --decorate --all,它将显示为用于绘制图形的符号的一部分

答案 3 :(得分:10)

In order to get truly tabular format you need to also truncate usernames that are longer than e.g. 20 characters:

git log --pretty=format:'%h %<(20,trunc)%an %s' -10

Additionally, if you're outputting to the terminal, you may want to prevent line overflows by either truncating the commit message field:

git log --pretty=format:'%h %<(20,trunc)%an %<(39,trunc)%s' -10

or wrapping the lines and indenting any overflowing lines to align:

git log --pretty=format:'%w(79, 0, 29)%h %<(20,trunc)%an %s' -10

EDIT: The above example hard-codes the terminal width to be 79 characters. On POSIX systems you can use tput cols to return the width:

git log --pretty=format:'%w(`tput cols`, 0, 29)%h %<(20,trunc)%an %s' -10

or

git log --pretty=format:'%w($((`tput cols`-1)), 0, 29)%h %<(20,trunc)%an %s' -10

Unfortunately these last two break git alias so it will require a terminal alias (unless you really love typing).

答案 4 :(得分:0)

表格格式的问题在于你可能用完空间......

以下是我要展示的基本内容之间的个人最佳折衷:

  • 做了什么
  • 和何时
  • 能够通过how
  • 的提交哈希来挖掘更多内容

这样:

  git log --pretty --format='%h %aI %<(15)%an ::: %s'

 git log --pretty --format='%h %cI %<(15)%an ::: %s'

if you want to get the commiter time and not the author time

答案 5 :(得分:0)

如何添加标签?

您正在寻找的是:

%x09

这里是一个例子

git log --pretty=format:"%C(magenta)%h %C(cyan)%C(bold)%ad%Creset %C(cyan)%cr%Creset%x09 | %s %C(green)%Creset" --date=short

我希望将相对日期作为选项卡,因为它不是固定长度,所以所有列都排成一行(少于一年以前的任何内容)。想要一直保持排列,但这是我用尽时间(具有讽刺意味的)之前所获得的。

希望这会有所帮助!

如果您有相应的建议可以改进相关年份的答案(而不仅仅是略述年份),也很高兴听到他们的建议,如果您知道如何做,可以随时编辑此答案以添加答案。