Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function resolvePath(path, bake, callback) {
var mode = modes.commit;
var hash = storage.getRootHash();
var root = "";
var parts = path.split("/").filter(Boolean);
path = parts.join("/");
var index = 0;
var partial = "";
// In baked mode, we need to remember tree rules.
var rules = bake ? [] : null;
// Start the walk loop.
return walk();
function walk() {
while (index < parts.length) {
var entry = { root: root };
// In raw mode, simply send the result back
if (!bake) {
if (found) {
entry.hash = hash;
entry.mode = mode;
}
if (callback) return callback(null, entry);
return entry;
}
// In bake mode
if (!found) return callback();
// Resolve commits to be trees
if (mode === modes.commit) {
if (!check("commit", hash)) return;
mode = modes.tree;
hash = storage.get(hash).tree;
}
// Serve the static blob or tree
var type = modes.toType(mode);
entry.hash = hash;
entry.mode = mode;
var overlays = [];
entry.fetch = function (callback) {
return storage.loadAs(root, type, hash, function (err, result) {
if (err) return callback(err);
if (entry.mode === modes.tree) {
return applyOverlays(result, overlays, callback);
}
function writeEntry(path, entry, callback) {
if (!callback) return writeEntry.bind(null, path, entry);
if (!pendingWrites) {
// Start recording writes to be written
pendingWrites = {};
writeCallbacks = [];
// Defer starting the write to collect more writes this tick.
defer(writeEntries);
}
if (!path) {
if (!entry.hash) {
return callback(new Error("Root cannot be deleted"));
}
if (entry.mode !== modes.commit) {
return callback(new Error("Only commits can be written to root"));
}
}
pendingWrites[path] = entry;
if (callback) writeCallbacks.push(callback);
}
function onChange(hash) {
renderChild("", modes.commit, hash);
// Run any hooks
Object.keys(hooks).forEach(function (root) {
hooks[root](hash);
});
}
function init() {
if ((mode === modes.tree || mode === modes.commit) && openPaths[path]) openTree(row);
if (activePath && activePath === path) activateDoc(row, !selected);
}
fs.init(onChange, function (err, hash) {
if (err) throw err;
openPaths[""] = true;
rootRow = renderChild("", modes.commit, hash);
rootEl.appendChild(rootRow.el);
});
defer(function () {
if (mode === modes.commit) row.call(fs.readCommit, onCommit);
else init();
});
function deepCopy(source, dest, entry, callback) {
if (!callback) return deepCopy.bind(null, source, dest, entry);
if (entry.mode === modes.commit) return callback();
var type = modes.toType(entry.mode);
source.loadAs(type, entry.hash, function (err, value) {
if (!value) return callback(err || new Error("Missing " + type + " " + entry.hash));
dest.saveAs(type, value, function (err) {
if (err) return callback(err);
if (type !== "tree") return callback();
carallel(Object.keys(value).map(function (name) {
return deepCopy(source, dest, value[name]);
}), callback);
}, entry.hash);
});
}
function updateIcon() {
var value =
(errorMessage ? "icon-attention" :
busy ? "icon-spin1 animate-spin" :
mode === modes.sym ? "icon-link" :
mode === modes.file ? "icon-doc" :
mode === modes.exec ? "icon-cog" :
mode === modes.commit ? "icon-fork" :
open ? "icon-folder-open" : "icon-folder") +
(mode === modes.commit ? " tight" : "");
$.icon.setAttribute("class", value);
var title = modes.toType(mode) + " " + hash;
if (errorMessage) title += "\n" + errorMessage;
$.icon.setAttribute("title", title);
if (mode !== modes.commit) {
if ($.folder) {
$.row.removeChild($.folder);
delete $.folder;
}
}
else {
if (!$.folder) {
$.row.insertBefore(domBuilder(["i$folder"], $), $.icon);
}
$.folder.setAttribute("class", "icon-folder" + (open ? "-open" : ""));
$.folder.setAttribute("title", "tree " + treeHash);
}
if (ahead) {
if (!$.ahead) {
$.row.appendChild(domBuilder(["i$ahead"], $));
$.ahead.setAttribute("class", "icon-upload-cloud");