Skip to content

Instantly share code, notes, and snippets.

@overengineer
Last active July 20, 2022 14:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overengineer/b69e578f5cf7457dc7d4ff8c3b7850bc to your computer and use it in GitHub Desktop.
Save overengineer/b69e578f5cf7457dc7d4ff8c3b7850bc to your computer and use it in GitHub Desktop.
git log json format
#!/usr/bin/env bash
# Caution: It can break
# Bash unofficial strict mode
set -euo pipefail
IFS=$'\n\t'
LANG=''
function define() { IFS='\n' read -r -d '' ${1} || true; }
define TMPL << 'EOF'
{
"commit": "%H",
"tree": "%T",
"parent": "%P",
"refs": "%D",
"encoding": "%e",
"message": "MSG",
"commit_notes": "%N",
"verification_flag": "%G?",
"signer": "%GS",
"signer_key": "%GK",
"author": {
"name": "AUTHOR",
"email": "%aE",
"date": "%ai"
},
"commiter": {
"name": "COMMITER",
"email": "%cE",
"date": "%ci"
}
}
EOF
function sanitize () {
# strip newlines, strip all whitespace, escape newline
sed -E -e ':a' -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' -e 's/^\s+|\s+$//g' -e ':a;N;$!ba;s/\n/\\n/g' \
| sed -E -e 's/%/%%/g' -e 's/"/\\"/g' -e 's/\\\\/\\/g' -e 's/\t/\\t/g' -e 's/\\([^nt"])/\\\\\1/g' -e 's/[\x00-\x1f]//g' | tr -d '\r\0\t'
# escape percent sign for template, escape quotes, escape backslash, escape tabs, fix double escapes, delete control characters
}
function field () {
git show -s --format="$1" "$2" | sanitize
}
git log --pretty=format:'%H' | while IFS='' read -r hash; do
TMP="$TMPL"
msg=$(field "%B" $hash)
author=$(field "%aN" $hash)
commiter=$(field "%cN" $hash)
TMP="${TMP/MSG/$msg}"
TMP="${TMP/AUTHOR/$author}"
TMP="${TMP/COMMITER/$commiter}"
git show $hash -s --format="$TMP"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment