如何获取git log -p as json [英] How to get git log -p as json

查看:61
本文介绍了如何获取git log -p as json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行 git log -p 并以JSON格式获取结果.我从漂亮格式文档中找不到解决方法,但是我可能会丢失一些东西.

I want to run git log -p and get the results as 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
}]

推荐答案

由于diff没有格式,因此无法使用 git log 来实现.可以使用管道命令编写脚本:

It's impossible to implement with git log because there is no format for 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 ']'

一些注意事项:

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

意味着遍历所有提交,将每个哈希读入变量 sha1 ".

Means "run through all commits, read every hash into variable sha1".

full_diff="$(…)"

提取提交的完整差异.将" 替换为 \" ,以避免生成损坏的JSON.

Extract the full diff for the commit. Replace " with \" to avoid generating broken JSON.

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

以给定格式打印有关提交的信息.分别添加完整的差异.

Print information about the commit in the given format. Add the full diff separately.

这篇关于如何获取git log -p as json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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