简体   繁体   English

如何获取 git log -p as json

[英]How to get git log -p as json

I want to run git log -p and get the results as JSON.我想运行git log -p并得到 JSON 的结果。 I couldn't find a way to do it from the pretty format documentation , but I am probably missing something.我无法从漂亮的格式文档中找到一种方法,但我可能遗漏了一些东西。

The desired result I have in mind will be something like:我想到的预期结果将类似于:

[{
  "commit": SHA,
  "author": AUTHOR,
  "date": DATE,
  "commit_message": COMMIT_MSG,
  "full_diff": FULL_DIFF
}]

It's impossible to implement with git log because there is no format for diff.使用git log是不可能实现的,因为没有 diff 格式。 It's possible to script using plumbing commands:可以使用管道命令编写脚本:

echo '['
git rev-list HEAD | while read sha1; do
    full_diff="$(git show --format='' $sha1 | sed 's/\"/\\\"/g')"
    git --no-pager show --format="{%n  \"commit\": \"%H\",%n  \"author\": \"%an\",%n  \"date\": \"%ad\",%n  \"commit_message\": \"%s\",%n  \"full_diff\": \"$full_diff\"%n}," -s $sha1
    done
echo ']'

A few notes:几点注意事项:

git rev-list HEAD | while read sha1; do…done

Means "run through all commits, read every hash into variable sha1 ".表示“运行所有提交,将每个 hash 读取到变量sha1中”。

full_diff="$(…)"

Extract the full diff for the commit.提取提交的完整差异。 Replace " with \" to avoid generating broken JSON."替换为\"以避免生成损坏的 JSON。

git show --format="…" -s $sha1

Print information about the commit in the given format.以给定格式打印有关提交的信息。 Add the full diff separately.单独添加完整的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM