Skip to content

Instantly share code, notes, and snippets.

@jinjor
Last active August 29, 2015 14:08
Show Gist options
  • Save jinjor/da7e3cad333f572fd023 to your computer and use it in GitHub Desktop.
Save jinjor/da7e3cad333f572fd023 to your computer and use it in GitHub Desktop.
NodeでGitのグラフをゴリゴリ作る
var childProcess = require('child_process');
var nedb = require('nedb');
db = {};
db.commits = new nedb('db/commits.db');
db.commits.loadDatabase();
var update = function(lines, db, cb, latestId) {
var head = lines.shift();
var splitted = head.split(',,');
var parents = splitted[1].split(' ');
var id = splitted[0];
var author = splitted[3];
var date = splitted[4];
var comment = splitted[5];
var commit = {
_id: id,
author: author,
date: date,
comment: comment,
parents: parents
};
if (!latestId) {
latestId = commit._id;
}
db.commits.insert(commit, function(err, commit) {
if (lines.length) {
update(lines, db, cb, latestId);
} else{
cb(latestId);
}
});
};
var recent = function(callback, db) {
var log = childProcess.exec('git log --date=iso --pretty=format:"%h,,%p,,%t,,%an,,%ad,,%s"', function(error, stdout, stderr) {
var lines = stdout.split('\n');
update(lines, db, callback);
});
};
var log = function(latestId, db) {
if (!latestId) {
return;
}
db.commits.findOne({
_id: latestId
}, function(error, commit) {
console.log(commit);
var parentId = commit.parents[0]; // とりあえずmasterのみ
log(parentId, db);
});
};
recent(function(latest) {
log(latest, db);
}, db);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment