如何将git log映射到json?



运行git log --all --pretty=format:"%h%x09%an%x09%ad%x09%s" --date=short --no-merges后,我的git输出如下所示:

84hf6f3 Some Author 2022-07-13 some commit message

以上输出顺序始终为sha|author|date|commit message

在这里我有一个问题,如何将这个输出映射到json文件,像上面的字段?

谢谢你的帮助!

Usingjq:

git log --all --pretty=format:"%h%x09%an%x09%ad%x09%s" --date=short --no-merges | 
jq -R '[ inputs | split("t") | { hash: .[0], author: .[1], date: .[2], message: .[3] }]'

-R(--raw-input)选项告诉jq输入不是JSON。相反,输入的每一行都作为字符串传递给过滤器。

jq脚本(过滤器)解释

[                     # wrap everything in a list (array)  <---------------------+
inputs              # process the list of all inputs (a list of strings)       |
| split("t")       # each input is split into a list of pieces                |
| {                 # for each list of pieces created above, create an object  |
hash: .[0],     # put the first piece as `hash`                      ^     |
author: .[1],   # the second piece as `author`                       |     |
date: .[2],     # the third as `date`                                |     |
message: .[3]   # and so on...                                       |     |
}                 # this is where the object ends ---------------------+     |
]                     # remember the wrapper on the first line? it ends here ----+

输出如下:

[
{
"hash": "199a8e4a",
"author": "me",
"date": "2018-07-05",
"message": "Implemented frobulator"
},
{
"hash": "85e76d6e",
"author": "Someone else",
"date": "2018-07-03",
"message": "Added a valuable contribution"
},
{
"hash": "ba91c1dc",
"author": "John Doe",
"date": "2018-05-24",
"message": "Initial Commit"
}
]
使用Node.js:
import { simpleGit } from 'simple-git';
simpleGit().log()

最新更新