retrography
3/25/2015 - 4:45 PM

Compiles most of the useful information in a git log into a chronologically-ordered JSON array using pretty formatting and a combination of

Compiles most of the useful information in a git log into a chronologically-ordered JSON array using pretty formatting and a combination of shell commands.

# Requires GNU's implementation of sed, which is provided by default on Linux. 
# If you are on a Mac try installing gnu-sed using Homebrew and replace
# all instances of `sed` with `gsed` in the script.

# Sets the date format to iso8601. Change it if you need to.

# Note that several fields may contain invalid JSON strings and the script 
# does not check for that. You may wan to run the output through a linter
# in order to make sure the output is usable.

# Git log reference: http://git-scm.com/docs/git-log

git --no-pager log --all --no-merges --ignore-missing --shortstat --date=iso --pretty=format:\
'{ "commit_hash": "%H", "tree_hash": "%T", "parent_hash": "%P", "author": { "name": "%an", "mailmap_name": "%aN", "email": "%ae", "mailmap_email": "%aE", "date": "%ad" }, "committer": { "name": "%cn", "mailmap_name": "%cN", "email": "%ce", "mailmap_email": "%cE", "date": "%cd" }, "subject": "%s", "ref_name": "%d", "notes": "%N",' |\
sed -r -e 's/^ ([0-9]*) files? changed(, ([0-9]*) insertions?\(\+\))?(, ([0-9]*) deletions?\(-\))?/"files_changed": "\1", "insertions": "\3", "deletions": "\5" }/' -e '1i ]\n\n' | \
paste -d " " - - - | \
tail -r | \
tr "\n" , | \
sed -r -e '1i [' -e 's/ ,\]  ,$/\n]\n/' -e 's/\}\s*,\s*\{/},\n{/g'