Skip to content

Instantly share code, notes, and snippets.

@ericvicenti
Created April 18, 2015 15:49
Show Gist options
  • Save ericvicenti/8f8b791e55218b8c8625 to your computer and use it in GitHub Desktop.
Save ericvicenti/8f8b791e55218b8c8625 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var db = require('level')(__dirname + '/.hig');
var diff = require('diff');
var fs = require('fs');
var isBinary = require('is-binary');
var md5_file = require('md5-file').async;
var md5 = require('MD5');
var path = require('path');
var program = require('commander');
function _isBufferBinary(buffer) {
return isBinary(buffer.toString('utf8'));
}
function _getAuthor() {
return process.env.HIG_AUTHOR || 'anonymous';
}
function _createFileCommit(file, cb) {
md5_file(file, function(dataId) {
var time = +new Date();
var fileName = path.basename(file);
var id = md5(fileName + dataId + time + _getAuthor());
fs.readFile(file, function(err, fileData) {
if (err) return cb(err);
var commit = {
data: fileData,
isBinary: _isBufferBinary(fileData),
name: fileName,
id: id,
dataId: dataId,
time: time,
author: _getAuthor(),
};
cb(null, commit);
});
});
}
function commitNewFile(file, cb) {
_createFileCommit(file, function(err, commit) {
if (err) return cb(err);
db.put(commit.id, JSON.stringify(commit), function(err) {
if (err) return cb(err);
return cb(null, commit.id);
});
});
}
function commitUpdatedFile(file, fromId, cb) {
get(fromId, function(err, fromCommit) {
if (err) return cb(err);
_createFileCommit(file, function(err, commit) {
if (err) return cb(err);
if (fromCommit.fromBase) {
commit.fromBase = fromCommit.fromBase;
} else {
commit.fromBase = fromCommit.fromId;
}
commit.from = fromId;
if (!fromCommit.isBinary) {
commit.patch = diff.createPatch('',
fromCommit.data.toString('utf8'),
commit.data.toString('utf8')
);
delete commit.data;
}
db.put(commit.sign, JSON.stringify(commit), function(err) {
if (err) return cb(err);
return cb(null, sign);
});
});
});
}
function commit(file, lastId, cb) {
if (lastId) {
commitUpdatedFile(file, lastId, cb);
} else {
commitNewFile(file, cb);
}
}
function _getPatched(destSign, destCommit, cb, _patched) {
db.get(destCommit.fromBase, function (err, baseCommitData) {
if (err) return cb(err);
var baseCommit = JSON.parse(baseCommitData);
baseCommit.data = new Buffer(baseCommit.data);
baseCommit
} else if (commitData.patch) {
return _getPatched(destSign, fromSign)
}
});
}
function get(sign, cb) {
db.get(sign, function (err, commitData) {
if (err) return cb(err);
var commit = JSON.parse(commitData);
if (commit.data) {
commit.data = new Buffer(commit.data);
} else if (commitData.fromBase) {
_getPatched(sign, commit, cb);
}
cb(null, commit);
});
}
program
.version('0.0.1')
.option('-c, --commit [file]', 'Commit a file or folder')
.option('-f, --from [id]', 'ID of the source for a new commit')
.option('-g, --get [id]', 'Get a commit by ID')
.parse(process.argv);
if (program.commit) {
commit(program.commit, program.from, function(err, sign) {
if (err) {
console.error(err);
} else {
console.log('Committed ' + sign);
}
});
} else if (program.get) {
get(program.get, function(err, commit) {
if (err) {
console.error(err);
} else {
console.log('Name: ' + commit.name);
console.log('Time: ' + new Date(commit.time));
// console.log('Content: ' + commit.data.toString('utf8'));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment