July 4, 2020

Git log/history in JSON or YAML format

You can use git pretty formats for creating YAML log (YAML instead of JSON, no messing around with trailing comma).

Example:

echo "commits:"
git \
  --no-pager log \
  --since "7 days ago" \
  --pretty=format:"  - id: %h%n    signer_key: '%GK'%n    timestamp: '%at'%n    subject: '%f'%n    body: |%n      %(trailers:separator=\n      )"

This shows all git commits from last 7 days with their signer key, timestamp, (sanitized) subject and the body as YAML. See pretty formats docs for more fields.

If you want to use JSON, you can convert the YAML e.g. using yq:

echo "commits:"
git \
  --no-pager log \
  --since "7 days ago" \
  --pretty=format:"  - id: %h%n    signer_key: '%GK'%n    timestamp: '%at'%n    subject: '%f'%n    body: |%n      %(trailers:separator=\n      )" | yq '.'