Created
November 24, 2014 18:58
-
-
Save demmer/9bd67f23a3d08c616242 to your computer and use it in GitHub Desktop.
Convert a git commit log into JSON
This file contains hidden or 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 node | |
// | |
// Converts a git log into a JSON array including the full commit message. | |
// | |
var cp = require('child_process'); | |
var formats = { | |
author: '%an', | |
email: '%ae', | |
time: '%ad', | |
sha: '%H', | |
message: '%B' | |
}; | |
var field_sep = '--------FIELD----------'; | |
var commit_sep = '============COMMIT============='; | |
var fields = Object.keys(formats); | |
var fmt = ''; | |
fields.forEach(function(f) { | |
fmt += formats[f] + field_sep; | |
}); | |
fmt += commit_sep; | |
var child = cp.exec('git log --reverse --pretty="' + fmt + '"', {maxBuffer: 1024*1024*1024}, function(error, stdout, stderr) { | |
if (error) { | |
console.error(error); | |
throw(error); | |
} | |
var commits = stdout.split(commit_sep + '\n'); | |
commits.pop(); // drop empty line | |
var objs = commits.map(function(c) { | |
var L = c.split(field_sep); | |
var o = {}; | |
for (var i = 0; i < fields.length; ++i) { | |
o[fields[i]] = L[i]; | |
} | |
o.user = o.email.split('@')[0]; | |
return o; | |
}); | |
console.log(JSON.stringify(objs, null, 4)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment