Last active
September 17, 2024 02:22
-
-
Save overengineer/b69e578f5cf7457dc7d4ff8c3b7850bc to your computer and use it in GitHub Desktop.
git log json format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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