Skip to content

Instantly share code, notes, and snippets.

@zhang-ning
Forked from overengineer/git-json.sh
Created September 17, 2024 02:22
Show Gist options
  • Select an option

  • Save zhang-ning/a8cdb9432b0caa831bc7b039a7e8b64d to your computer and use it in GitHub Desktop.

Select an option

Save zhang-ning/a8cdb9432b0caa831bc7b039a7e8b64d to your computer and use it in GitHub Desktop.
git log json format
#!/bin/bash
# Bash unofficial strict mode
set -euo pipefail
# http://www.dwheeler.com/essays/filenames-in-shell.html
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 strip () {
# strip newlines then strip all whitespace
sed -e ':a' -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' | sed -E 's/^\s+|\s+$//g'
}
function sanitize () {
# escape newline, escape quotes, escape backslash, escape tabs, fix double escapes, delete invalid characters
sed ':a;N;$!ba;s/\n/\\n/g' | sed -e 's/"/\\"/g' -e 's/\\\\/\\/g' -e 's/\t/\\t/g' | sed -E 's/\\([^nt"])/\\\\\1/g' | tr -d '\r\0\t' | sed -E 's/[\x00-\x1f]//g'
}
function field () {
git show -s --format="$1" "$2" | strip | sanitize
}
git log --pretty=format:'%H' | while IFS='' read -r hash; do
TMP="$TMPL"
TMP="${TMP/MSG/$(field "%B" $hash)}"
TMP="${TMP/AUTHOR/$(field "%aN" $hash)}"
TMP="${TMP/COMMITER/$(field "%cN" $hash)}"
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