Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/cf498033db3d6f8c4949 to your computer and use it in GitHub Desktop.
Save creationix/cf498033db3d6f8c4949 to your computer and use it in GitHub Desktop.
sample of fs-db in node
var fs = require('fs');
var dirname = require('path').dirname;
var repo = {
rootPath: pathJoin(__dirname, "repo.git")
};
require('js-git/mixins/fs-db')(repo, {
readFile: function (path, callback) {
fs.readFile(path, function (err, buffer) {
if (err) {
if (err.code === "ENOENT") return callback();
return callback(err);
}
callback(null, buffer);
});
},
writeFile: function (path, buffer, callback) {
mkdirp(dirname(path), function (err) {
if (err) return callback(err);
fs.writeFile(path, buffer, callback);
});
},
readDir: function (path, callback) {
fs.readdir(path, function (err, results) {
if (err) {
if (err.code === "ENOENT") return callback();
return callback(err);
}
return callback(null, results);
});
},
readChunk: function (path, start, end, callback) {
throw "TODO: Implement this to read packfiles"
// I didn't need it for my node server since I create the repos.
// This should return a Buffer for the file from start to end offsets.
}
});
function mkdirp(path, callback) {
fs.mkdir(path, function (err) {
if (err) {
if (err.code === "ENOENT") return mkdirp(dirname(path), function (err) {
if (err) return callback(err);
fs.mkdir(path, callback);
});
if (err.code === "EEXIST") return callback();
return callback(err);
}
callback();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment