Add repo.js
This is our git wrapper. I tried using gift but it's painful.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
var exec = require('child_process').exec,
|
||||
debug = require('debug')('repo.js');
|
||||
|
||||
exports = module.exports = {
|
||||
initialize: initialize,
|
||||
head: null,
|
||||
tree: tree
|
||||
};
|
||||
|
||||
var gitDir;
|
||||
|
||||
function git(command, callback) {
|
||||
var options = {
|
||||
env: { GIT_DIR: gitDir }
|
||||
};
|
||||
exec('git ' + command, options, function (error, stdout, stderr) {
|
||||
if (error) return callback(error);
|
||||
return callback(null, stdout);
|
||||
});
|
||||
}
|
||||
|
||||
function initialize(config, callback) {
|
||||
gitDir = config.root + '/.git';
|
||||
|
||||
git('rev-parse HEAD', function (err, sha1) {
|
||||
if (err) return callback(err);
|
||||
exports.head = sha1;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function tree(treeish, callback) {
|
||||
git('ls-tree -r ' + treeish, function (err, out) {
|
||||
var lines = out.split('\n');
|
||||
var entries = [ ];
|
||||
lines.forEach(function (line) {
|
||||
var id, mode, name, type, _ref;
|
||||
// sample line : 100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 README
|
||||
var parts = line.split(/[\t ]+/, 4);
|
||||
var mode = parts[0], type = parts[1], name = parts[3];
|
||||
entries.push({
|
||||
stat: { mode: parseInt(parts[0], 8) }, // match fs.Stat object
|
||||
sha1: parts[2],
|
||||
path: parts[3]
|
||||
});
|
||||
});
|
||||
callback(null, entries);
|
||||
});
|
||||
}
|
||||
|
||||
+16
-6
@@ -12,7 +12,9 @@ var optimist = require('optimist'),
|
||||
mkdirp = require('mkdirp'),
|
||||
db = require('./database'),
|
||||
sync = require('./sync'),
|
||||
routes = require('./routes');
|
||||
routes = require('./routes'),
|
||||
repo = require('./repo'),
|
||||
debug = require('debug');
|
||||
|
||||
var argv = optimist.usage('Usage: $0 --root <directory>')
|
||||
.alias('h', 'help')
|
||||
@@ -77,7 +79,7 @@ app.configure(function () {
|
||||
app.post('/file', routes.file.update);
|
||||
});
|
||||
|
||||
function initialize() {
|
||||
function initialize(callback) {
|
||||
var config = {
|
||||
port: argv.p || 3000,
|
||||
root: path.resolve(argv.r)
|
||||
@@ -87,13 +89,14 @@ function initialize() {
|
||||
|
||||
mkdirp(config.root);
|
||||
if (!db.initialize(config)) {
|
||||
console.error('Error initializing database');
|
||||
process.exit(1);
|
||||
return callback(new Error('Error initializing database'));
|
||||
}
|
||||
|
||||
sync.initialize(config);
|
||||
routes.file.initialize(config, sync);
|
||||
routes.volume.initialize(config, app);
|
||||
|
||||
repo.initialize(config, callback);
|
||||
}
|
||||
|
||||
function listen(next) {
|
||||
@@ -103,10 +106,17 @@ function listen(next) {
|
||||
console.log('Server listening on port ' + app.get('port') + ' in ' + app.get('env') + ' mode');
|
||||
next();
|
||||
});
|
||||
|
||||
repo.tree(repo.head, function (err, entries) { console.log(entries); });
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
initialize();
|
||||
listen();
|
||||
initialize(function (err) {
|
||||
if (err) {
|
||||
console.error('error initializing', err);
|
||||
process.exit(1);
|
||||
}
|
||||
listen();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user