watkinsmatthewp watkinsmatthewp - 3 months ago 24
JSON Question

Git log JSON *with changed files*

I need to get the output of a git log command formatted as JSON. I like the approach suggested in Git log output to XML, JSON, or YAML? to use a custom git log pretty formatter and a light layer of perl processing on top using the command here:

git log \
--pretty=format:'{%n "commit": "%H",%n "author": "%an <%ae>",%n "date": "%ad",%n "message": "%f"%n},' \
$@ | \
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
perl -pe 's/},]/}]/'


This works fantastic for getting those certain fields, but I also need the list of files changed that is returned from using the --name-status flag included in the JSON output.

I've tried various approaches but I'm at a loss at how to approach this. My gut tells me I'm missing something obvious. Any ideas?

Thanks!

Answer

I've added --name-only parameter support to the original Noah Sussman's script so you can get the list of changed files using this command:

git log2json --name-only

The amended script is here.