Build server prototype (integration with GitHub / NuGet / etc)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
micro-build-server/BuildServer/lib/git/copy.js

84 lines
1.9 KiB

"use strict";
var EventEmitter = require('events').EventEmitter,
path = require('path'),
fs = require('fs'),
nodegit = require('nodegit'),
async = require('async'),
Copier = require('recursive-tree-copy').Copier;
var gitToFsCopier = new Copier({
concurrency: 4,
walkSourceTree: function (tree) {
var emitter = new EventEmitter();
process.nextTick(function () {
var entries;
try {
entries = tree.gitTree.entries();
} catch(err) {
return emitter.emit('error', err);
}
async.parallel(entries.map(function (entry) {
return function (callback) {
if (entry.isTree()) {
entry.getTree(function (err, subTree) {
if (err) {
return callback(err);
}
emitter.emit('tree', { gitTree: subTree, name: entry.name() });
callback();
});
} else if (entry.isFile()) {
emitter.emit('leaf', entry);
callback();
} else {
callback();
}
};
}), function (err) {
if (err) {
return emitter.emit('error', err);
}
return emitter.emit('done');
});
});
return emitter;
},
createTargetTree: function (tree, targetDir, callback) {
var targetSubdir = path.join(targetDir, tree.name);
fs.mkdir(targetSubdir, function (err) {
if (err && err.code != 'EEXIST' /* workaround for broken trees */) {
return callback(err);
}
callback(undefined, targetSubdir);
});
},
finalizeTargetTree: function (targetSubdir, callback) {
callback();
},
copyLeaf: function (entry, targetDir, callback) {
var targetPath = path.join(targetDir, entry.name());
entry.getBlob(function (err, blob) {
if (err) {
return callback(err);
}
fs.writeFile(targetPath, blob.content(), callback);
});
}
});
exports.gitToFs = function (commit, exportDir, callback) {
commit.getTree(function (err, tree) {
if (err) {
return callback(err);
}
gitToFsCopier.copy({ gitTree: tree, name: "." }, exportDir, callback);
});
};