Git log output to XML, JSON, or YAML?
GitFileLoggingVersion ControlIoGit Problem Overview
This is a pretty simple question: as a Git newbie I was wondering if there's a way for me to output my git log to a file, preferably in some kind of serialized format like XML, JSON, or YAML. Any suggestions?
Git Solutions
Solution 1 - Git
to output to a file:
git log > filename.log
To specify a format, like you want everything on one line
git log --pretty=oneline >filename.log
or you want it a format to be emailed via a program like sendmail
git log --pretty=email |email-sending-script.sh
to generate JSON, YAML or XML it looks like you need to do something like:
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
This gist (not mine) perfectly formats output in JSON: https://gist.github.com/1306223
See also:
Solution 2 - Git
I did something like this to create a minimal web api / javascript widget that would show the last 5 commits in any repository.
If you are doing this from any sort of scripting language, you really want to generate your JSON with something other than "
for your quote character, so that you can escape real quotes in commit messages. (You will have them sooner or later, and it's not nice for that to break things.)
So I ended up with the terrifying but unlikely delimiter ^@^
and this command-line.
var cmd = 'git log -n5 --branches=* --pretty=format:\'{%n^@^hash^@^:^@^%h^@^,%n^@^author^@^:^@^%an^@^,%n^@^date^@^:^@^%ad^@^,%n^@^email^@^:^@^%aE^@^,%n^@^message^@^:^@^%s^@^,%n^@^commitDate^@^:^@^%ai^@^,%n^@^age^@^:^@^%cr^@^},\'';
Then (in node.js) my http response body is constructed from stdout
of the call to git log
thusly:
var out = ("" + stdout).replace(/"/gm, '\\"').replace(/\^@\^/gm, '"');
if (out[out.length - 1] == ',') {
out = out.substring (0, out.length - 1);
}
and the result is nice JSON that doesn't break with quotes.
Solution 3 - Git
This script wraps git log and produces JSON output: https://github.com/paulrademacher/gitjson
Solution 4 - Git
I wrote this in Powershell to get git logdata and save it as json or other format:
$header = @("commit","tree","parent","refs","subject","body","author","commiter")
[string] $prettyGitLogDump= (git log MyCoolSite.Web-1.4.0.002..HEAD --pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;')
$gldata = foreach ($commit in $prettyGitLogDump.Replace("; ",';') -split ";", 0, "multiline") {
$prop = $commit -split "\|"
$hash = [ordered]@{}
for ($i=0;$i -lt $header.count;$i++) {$hash.add($header[$i],$prop[$i])}
[pscustomobject]$hash
}
$gldata | ConvertTo-Json | Set-Content -Path "GitLog.json"
The headernames:
> "commit","tree","parent","refs","subject","body","author","commiter"
have to be in sync with datafields :
> --pretty=format:'%H|%T|%P|%D|%s|%b|%an|%cn;'
See prettyformat docs.
I choose pipe | as a separator. I am taking a risc that it is not used in the commit message. I used semicolon ; as a sep for every commit. I should of course chosen something else. You could try to code some clever regular expression to match and check if your separators are used in the commit message. Or you could code more complex regularexpression to match split point or code a powershell scriptblock to define the split.
The hardest line in the code to figure out was.
> prettyGitLogDump.Replace("; ",';') -split ";", 0, "multiline"
You have to set option multiline becuase there can be CR/LF in the messages and then split stops - you can only set multiline if nr of split is given. Therefore second paramvalue 0 which means all.
(The Replace("; ",';') is just a hack I get a space after the first commit. So I remove space after commit separator. Probably there is a better solution.)
Anyway i think this could be a workable solution for windows users or powershells fans that want the log from git to see who made the commit and why.
Solution 5 - Git
Behold https://github.com/dreamyguy/gitlogg, the last git-log => JSON
parser you will ever need!
Some of Gitlogg's features are:
- Parse the
git log
of multiple repositories into oneJSON
file. - Introduced
repository
key/value. - Introduced
files changed
,insertions
anddeletions
keys/values. - Introduced
impact
key/value, which represents the cumulative changes for the commit (insertions
-deletions
). - Sanitise double quotes
"
by converting them to single quotes'
on all values that allow or are created by user input, likesubject
. - Nearly all the
pretty=format:
placeholders are available. - Easily include / exclude which keys/values will be parsed to
JSON
by commenting out/uncommenting the available ones. - Easy to read code that's thoroughly commented.
- Script execution feedback on console.
- Error handling (since path to repositories needs to be set correctly).
Success, the JSON was parsed and saved.