Git log output to XML, JSON, or YAML?
Solution 1:
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:
- http://git-scm.com/book/en/Git-Basics-Viewing-the-Commit-History
- Git how to save a preset git log --format
- How to parse the output of git log
Solution 2:
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:
This script wraps git log and produces JSON output: https://github.com/paulrademacher/gitjson