Git 日志输出到 XML、JSON 或 YAML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4600445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:44:36  来源:igfitidea点击:

Git log output to XML, JSON, or YAML?

gitfileloggingversion-controlio

提问by Andrew

This is a pretty simple question: as a Git newbie I was wondering if there's a way for me to output my git log to a file, preferably in some kind of serialized format like XML, JSON, or YAML. Any suggestions?

这是一个非常简单的问题:作为一个 Git 新手,我想知道是否有办法将我的 git 日志输出到文件中,最好是某种序列化格式,如 XML、JSON 或 YAML。有什么建议?

回答by huntar

to output to a file:

输出到文件:

git log > filename.log

To specify a format, like you want everything on one line

指定一种格式,就像您希望在一行中显示所有内容一样

git log --pretty=oneline >filename.log

or you want it a format to be emailed via a program like sendmail

或者您希望它是一种通过 sendmail 等程序通过电子邮件发送的格式

git log --pretty=email |email-sending-script.sh

to generate JSON, YAML or XML it looks like you need to do something like:

要生成 JSON、YAML 或 XML,您似乎需要执行以下操作:

git log --pretty=format:"%h%x09%an%x09%ad%x09%s"

This gist (not mine) perfectly formats output in JSON: https://gist.github.com/1306223

这个要点(不是我的)完美地格式化了 JSON 输出:https: //gist.github.com/1306223

See also:

也可以看看:

回答by Tim Boudreau

I did something like this to create a minimal web api / javascript widget that would show the last 5 commits in any repository.

我做了这样的事情来创建一个最小的 web api/javascript 小部件,它将显示任何存储库中的最后 5 个提交。

If you are doing this from any sort of scripting language, you reallywant to generate your JSON with something other than "for your quote character, so that you can escape real quotes in commit messages. (You willhave them sooner or later, and it's not nice for that to break things.)

如果您使用任何类型的脚本语言执行此操作,您确实希望使用"引号字符以外的其他内容生成 JSON ,以便您可以在提交消息中转义真正的引号。(你迟早拥有它们,破坏东西并不好。)

So I ended up with the terrifying but unlikely delimiter ^@^and this command-line.

所以我最终得到了可怕但不太可能的分隔符^@^和这个命令行。

var cmd = 'git log -n5 --branches=* --pretty=format:\'{%n^@^hash^@^:^@^%h^@^,%n^@^author^@^:^@^%an^@^,%n^@^date^@^:^@^%ad^@^,%n^@^email^@^:^@^%aE^@^,%n^@^message^@^:^@^%s^@^,%n^@^commitDate^@^:^@^%ai^@^,%n^@^age^@^:^@^%cr^@^},\'';

Then (in node.js) my http response body is constructed from stdoutof the call to git logthusly:

然后(在 node.js 中)我的 http 响应主体是从stdout调用中构建的git log

var out = ("" + stdout).replace(/"/gm, '\"').replace(/\^@\^/gm, '"');
if (out[out.length - 1] == ',') {
    out = out.substring (0, out.length - 1);
}

and the result is nice JSON that doesn't break with quotes.

结果是很好的 JSON,不会被引号破坏。

回答by Paul Rademacher

This script wraps git log and produces JSON output: https://github.com/paulrademacher/gitjson

此脚本包装 git log 并生成 JSON 输出:https: //github.com/paulrademacher/gitjson

回答by Patrik Lindstr?m

I wrote this in Powershell to get git logdata and save it as json or other format:

我在 Powershell 中写了这个来获取 git logdata 并将其保存为 json 或其他格式:

$header = @("commit","tree","parent","refs","subject","body","author","commiter") 
[string] $prettyGitLogDump= (git log MyCoolSite.Web-1.4.0.002..HEAD --pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;') 
$gldata = foreach ($commit in $prettyGitLogDump.Replace("; ",';') -split  ";", 0, "multiline") {
          $prop = $commit -split "\|"
          $hash = [ordered]@{}
          for ($i=0;$i -lt $header.count;$i++) {$hash.add($header[$i],$prop[$i])} 
          [pscustomobject]$hash
}
$gldata |  ConvertTo-Json | Set-Content -Path "GitLog.json" 

The headernames:

标题名称:

"commit","tree","parent","refs","subject","body","author","commiter"

“提交”、“树”、“父”、“参考”、“主题”、“正文”、“作者”、“提交者”

have to be in sync with datafields :

必须与数据字段同步:

--pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;'

--pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;'

See prettyformat docs.
I choose pipe |as a separator. I am taking a risc that it is not used in the commit message. I used semicolon ;as a sep for every commit. I should of course chosen something else. You could try to code some clever regular expression to match and check if your separators are used in the commit message. Or you could code more complex regularexpression to match split point or code a powershell scriptblock to define the split.

请参阅prettyformat 文档
我选择管| 作为分隔符。我认为它没有在提交消息中使用。我用了分号作为每次提交的 sep。我当然应该选择别的东西。您可以尝试编写一些巧妙的正则表达式来匹配并检查提交消息中是否使用了分隔符。或者您可以编写更复杂的正则表达式来匹配拆分点或编写一个 powershell 脚本块来定义拆分

The hardest line in the code to figure out was.

代码中最难弄清楚的一行是。

prettyGitLogDump.Replace("; ",';') -split ";", 0, "multiline"

PrettyGitLogDump.Replace("; ",';') -split ";", 0, "multiline"

You have to set option multilinebecuase there can be CR/LF in the messages and then split stops - you can only set multiline if nr of split is given. Therefore second paramvalue 0 which means all.

您必须设置选项 multiline因为消息中可以有 CR/LF 然后拆分停止 - 如果给出拆分的 nr,您只能设置多行。因此第二个参数值 0 表示全部。

(The Replace("; ",';') is just a hack I get a space after the first commit. So I remove space after commit separator. Probably there is a better solution.)

( Replace("; ",';') 只是一个黑客,我在第一次提交后得到一个空格。所以我在提交分隔符后删除空格。可能有更好的解决方案。)

Anyway i think this could be a workable solution for windows users or powershells fans that want the log from git to see who made the commit and why.

无论如何,我认为这对于 Windows 用户或 powershells 粉丝来说可能是一个可行的解决方案,他们希望来自 git 的日志查看谁提交以及为什么提交。

回答by Wallace Sidhrée

Behold https://github.com/dreamyguy/gitlogg, the last git-log => JSONparser you will ever need!

https://github.com/dreamyguy/gitlogggit-log => JSON您将永远需要的最后一个解析器!

Some of Gitlogg's features are:

Gitlogg的一些特性是:

  • Parse the git logof multiple repositories into one JSONfile.
  • Introduced repositorykey/value.
  • Introduced files changed, insertionsand deletionskeys/values.
  • Introduced impactkey/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 JSONby 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).
  • 解析git log多个资源库到一个JSON文件中
  • 引入repository键/值。
  • 引入files changed,insertionsdeletions键/值。
  • 引入impact键/值,表示提交 ( insertions- deletions)的累积更改。
  • "通过'在所有允许或由用户输入创建的值上将双引号转换为单引号来清理双引号,例如subject.
  • 几乎所有pretty=format:占位符都可用。
  • JSON通过注释掉/取消注释可用的键/值,轻松包含/排除哪些键/值将被解析。
  • 易于阅读的代码,经过彻底的注释。
  • 控制台上的脚本执行反馈。
  • 错误处理(因为需要正确设置存储库路径)。

Success, the JSON was parsed and saved.Success, the JSON was parsed and saved.

成功,JSON 被解析并保存。成功,JSON 被解析并保存。

Error 001Error 001: path to repositories does not exist.

错误 001错误 001:存储库路径不存在。

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

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