-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-log-json.sh
More file actions
executable file
·59 lines (55 loc) · 2.39 KB
/
git-log-json.sh
File metadata and controls
executable file
·59 lines (55 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# attempting to be the most robust solution for outputting git log as JSON,
# using only `git` and the standard shell functions, without requiring
# additional software.
# - uses traditional JSON camelCase
# - includes every major field that git log can output, including the body
# - proper sections for author, committer, and signature
# - multiple date formats (one for reading, ISO for parsing)
# - should properly handle (most? all?) body values, even those that contain
# quotation marks and escaped characters
# - outputs as minimized JSON, can be piped to `jq` for pretty printing
# - can run against the current directory as `git-log-json` or against a file
# or folder with `git-log-json foo`
# - easily piped into `jq`, e.g. this will get all the commit subjects:
# $ git-log-json foo | jq -r '.[] | .subject'
# credit to @nsisodiya, @varemenos, @overengineer, and others for the
# original working code:
# https://gist.github.com/varemenos/e95c2e098e657c7688fd
git_log_json() {
IFS='' read -r -d '' FORMAT << 'EOF'
{
^^^^author^^^^: { ^^^^name^^^^: ^^^^%aN^^^^,
^^^^email^^^^: ^^^^%aE^^^^,
^^^^date^^^^: ^^^^%aD^^^^,
^^^^dateISO8601^^^^: ^^^^%aI^^^^},
^^^^body^^^^: ^^^^%b^^^^,
^^^^commitHash^^^^: ^^^^%H^^^^,
^^^^commitHashAbbreviated^^^^: ^^^^%h^^^^,
^^^^committer^^^^: {
^^^^name^^^^: ^^^^%cN^^^^,
^^^^email^^^^: ^^^^%cE^^^^,
^^^^date^^^^: ^^^^%cD^^^^,
^^^^dateISO8601^^^^: ^^^^%cI^^^^},
^^^^encoding^^^^: ^^^^%e^^^^,
^^^^notes^^^^: ^^^^%N^^^^,
^^^^parent^^^^: ^^^^%P^^^^,
^^^^parentAbbreviated^^^^: ^^^^%p^^^^,
^^^^refs^^^^: ^^^^%D^^^^,
^^^^signature^^^^: {
^^^^key^^^^: ^^^^%GK^^^^,
^^^^signer^^^^: ^^^^%GS^^^^,
^^^^verificationFlag^^^^: ^^^^%G?^^^^},
^^^^subject^^^^: ^^^^%s^^^^,
^^^^subjectSanitized^^^^: ^^^^%f^^^^,
^^^^tree^^^^: ^^^^%T^^^^,
^^^^treeAbbreviated^^^^: ^^^^%t^^^^
},
EOF
FORMAT=$(echo "$FORMAT"|tr -d '\r\n ')
git log --pretty=format:"$FORMAT" "$1" | \
sed -e ':a' -e 'N' -e '$!ba' -e s'/\^^^^},\n{\^^^^/^^^^},{^^^^/g' \
-e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\^^^^/"/g' -e '$ s/,$//' | \
sed -e ':a' -e 'N' -e '$!ba' -e 's/\r//g' -e 's/\n/\\n/g' -e 's/\t/\\t/g' | \
awk 'BEGIN { ORS=""; printf("[") } { print($0) } END { printf("]\n") }'
}