如何在一行中获取短数据的Git日志? [英] How to get Git log with short stat in one line?

查看:112
本文介绍了如何在一行中获取短数据的Git日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下命令在控制台上输出以下几行文本:

  git log --pretty = format:%h;%ai ;%s--shortstat 
ed6e0ab; 2014-01-07 16:32:39 +0530; Foo
3个文件已更改,14个插入(+),13个删除( - )

cdfbb10; 2014-01-07 14:59:48 +0530; Bar
1个文件已更改,21个插入(+)

5fde3e1; 2014-01-06 17: 26:40 +0530; Merge Baz
772b277; 2014-01-06 17:09:42 +0530; Qux
7个文件已更改,72个插入(+),7个删除( - )

我有兴趣在上面显示这种格式

  ed6e0ab; 2014-01-07 16:32:39 +0530; Foo; 3; 14; 13 
cdfbb10; 2014-01-07 14:59: 48 +0530; Bar; 1; 21; 0
5fde3e1; 2014-01-06 17:26:40 +0530; Merge Baz; 0; 0; 0
772b277; 2014-01-06 17 :09:42 +0530; Qux; 7; 72; 7

它可以解析分号分隔值。
这是文本\ n 3个文件已更改,14个插入(+),13个删除( - )(包含新行)转换为 3; 14; 13 (不换行)
一个可能的转角情况是类似于5fde3e1; 2014-01-06 17: 26:40 +0530;合并巴兹它没有这样的线。在这种情况下,我希望; 0; 0; 0



总的来说,一段的时间。
我阅读了git log文档,但找不到能够帮助我以这种格式呈现的格式。我发现的最好的是上面提到的命令。



因此,任何可以生成预期格式的命令或shell脚本都会非常有用。



谢谢!

解决方案

不幸的是, c> git log 。人们必须使用其他脚本来弥补大多数人不知道的事情:某些提交没有统计 ,即使它们不是合并。

>

我一直在研究一个将 git log 转换为 JSON 并完成它我必须做你所需要的:将每个提交和统计数据集合在一行中。该项目名为 Gitlogg ,欢迎您根据自己的需求进行调整:
错误001:存储库路径不存在。




错误002:存储库路径存在,但为空。


Following command outputs following lines of text on console

git log --pretty=format:"%h;%ai;%s" --shortstat
ed6e0ab;2014-01-07 16:32:39 +0530;Foo
 3 files changed, 14 insertions(+), 13 deletions(-)

cdfbb10;2014-01-07 14:59:48 +0530;Bar
 1 file changed, 21 insertions(+)

5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz
772b277;2014-01-06 17:09:42 +0530;Qux
 7 files changed, 72 insertions(+), 7 deletions(-)

I'm interested in having above format to be displayed like this

ed6e0ab;2014-01-07 16:32:39 +0530;Foo;3;14;13
cdfbb10;2014-01-07 14:59:48 +0530;Bar;1;21;0
5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz;0;0;0
772b277;2014-01-06 17:09:42 +0530;Qux;7;72;7

This will be consumed in some report which can parse semicolon separated values. The thing is the text "\n 3 files changed, 14 insertions(+), 13 deletions(-)" (new line included) gets converted to 3;14;13 (without new line) One possible corner case is text like "5fde3e1;2014-01-06 17:26:40 +0530;Merge Baz" which doesn't have such line. In that case I want ;0;0;0

Overall the goal is to analyze file change stats over a period of time. I read the git log documentation but couldn't find any format which will help me to render in this format. The best I came up was the above command mentioned.

So any command or shell script which can generate the expected format would be of great help.

Thanks!

解决方案

This is, unfortunately, impossible to achieve using only git log. One has to use other scripts to compensate for something most people aren't aware of: some commits don't have stats, even if they are not merges.

I have been working on a project that converts git log to JSON and to get it done I had to do what you need: get each commit, with stats, in one line. The project is called Gitlogg and you're welcome to tweak it to your needs: https://github.com/dreamyguy/gitlogg

Below is the relevant part of Gitlogg, that will get you close to what you'd like:

git log --all --no-merges --shortstat --reverse --pretty=format:'commits\tcommit_hash\t%H\tcommit_hash_abbreviated\t%h\ttree_hash\t%T\ttree_hash_abbreviated\t%t\tparent_hashes\t%P\tparent_hashes_abbreviated\t%p\tauthor_name\t%an\tauthor_name_mailmap\t%aN\tauthor_email\t%ae\tauthor_email_mailmap\t%aE\tauthor_date\t%ad\tauthor_date_RFC2822\t%aD\tauthor_date_relative\t%ar\tauthor_date_unix_timestamp\t%at\tauthor_date_iso_8601\t%ai\tauthor_date_iso_8601_strict\t%aI\tcommitter_name\t%cn\tcommitter_name_mailmap\t%cN\tcommitter_email\t%ce\tcommitter_email_mailmap\t%cE\tcommitter_date\t%cd\tcommitter_date_RFC2822\t%cD\tcommitter_date_relative\t%cr\tcommitter_date_unix_timestamp\t%ct\tcommitter_date_iso_8601\t%ci\tcommitter_date_iso_8601_strict\t%cI\tref_names\t%d\tref_names_no_wrapping\t%D\tencoding\t%e\tsubject\t%s\tsubject_sanitized\t%f\tcommit_notes\t%N\tstats\t' |
  sed '/^[ \t]*$/d' |               # remove all newlines/line-breaks, including those with empty spaces
  tr '\n' 'ò' |                     # convert newlines/line-breaks to a character, so we can manipulate it without much trouble
  tr '\r' 'ò' |                     # convert carriage returns to a character, so we can manipulate it without much trouble
  sed 's/tòcommits/tòòcommits/g' |  # because some commits have no stats, we have to create an extra line-break to make `paste -d ' ' - -` consistent
  tr 'ò' '\n' |                     # bring back all line-breaks
  sed '{
      N
      s/[)]\n\ncommits/)\
  commits/g
  }' |                              # some rogue mystical line-breaks need to go down to their knees and beg for mercy, which they're not getting
  paste -d ' ' - -                  # collapse lines so that the `shortstat` is merged with the rest of the commit data, on a single line

Note that I've used the tab character ( \t ) to separate fields as ; could have been used on the commit message.

Another important part of this script is that each line must begin with an unique string (in this case it's commits). That's because our script needs to know where the line begins. In fact, whatever comes after the git log command is there to compensate for the fact that some commits might not have stats.

But it strikes me that what you want to achieve is to have commits neatly outputted in a format you can reliably consume. Gitlogg is perfect for that! Some of its features are:

  • Parse the git log of multiple repositories into one JSON file.
  • Introduced repository key/value.
  • Introduced files changed, insertions and deletions keys/values.
  • Introduced impact key/value, which represents the cumulative changes for the commit (insertions - deletions).
  • Sanitise double quotes " by converting them to single quotes ' on all values that allow or are created by user input, like subject.
  • Nearly all the pretty=format: placeholders are available.
  • Easily include / exclude which keys/values will be parsed to JSON by commenting out/uncommenting the available ones.
  • Easy to read code that's thoroughly commented.
  • Script execution feedback on console.
  • Error handling (since path to repositories needs to be set correctly).

Success, the JSON was parsed and saved.

Error 001: path to repositories does not exist.

Error 002: path to repositories exists, but is empty.

这篇关于如何在一行中获取短数据的Git日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆