Get Git log in JSON format
git log --pretty=format:'{%n "commit": "%H",%n "abbreviated_commit": "%h",%n "tree": "%T",%n "abbreviated_tree": "%t",%n "parent": "%P",%n "abbreviated_parent": "%p",%n "refs": "%D",%n "encoding": "%e",%n "subject": "%s",%n "sanitized_subject_line": "%f",%n "body": "%b",%n "commit_notes": "%N",%n "verification_flag": "%G?",%n "signer": "%GS",%n "signer_key": "%GK",%n "author": {%n "name": "%aN",%n "email": "%aE",%n "date": "%aD"%n },%n "commiter": {%n "name": "%cN",%n "email": "%cE",%n "date": "%cD"%n }%n},'
The only information that aren't fetched are:
%B
: raw body (unwrapped subject and body)%GG
: raw verification message from GPG for a signed commit
The format is applied to each line, so once you get all the lines, you need to remove the trailing ,
and wrap them around an Array.
git log pretty format source: http://git-scm.com/docs/pretty-formats
A very interesting javascript take you took there, I didn't consider it before! 🏆
For me it was very important to parse the commit stats as well, but the most straight-forward way to output stats (through
--shortstat
) is cumbersome as it gets printed on a new line. As some commits don't have any stats, the new line pattern breaks, making it difficult to port the data reliably.It was also important for me to output the
git log
of many repositories into a singleJSON
, so the script got a bit more complex than originally planned, as I had to deal with some error handling. After many failed attempts I got it sorted with the help of two scripts, one to spit out the commits output in one line each, and another to parse the chewed output toJSON
.It's all here https://github.com/dreamyguy/gitlogg
Some of Gitlogg's features are:
git log
of multiple repositories into oneJSON
file. ✨repository
key/value.files changed
,insertions
anddeletions
keys/values.impact
key/value, which represents the cumulative changes for the commit (insertions
-deletions
)."
by converting them to single quotes'
on values that allow user input, likesubject
.pretty=format:
placeholders are featured.JSON
by commenting out/uncommenting the available ones.