Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tag: encodeTag,
tree: encodeTree,
blob: encodeBlob
};
var decoders = {
commit: decodeCommit,
tag: decodeTag,
tree: decodeTree,
blob: decodeBlob,
};
// Precompute hashes for empty blob and empty tree since github won't
var empty = bodec.create(0);
var emptyBlob = sha1(frame({ type: "blob", body: empty }));
var emptyTree = sha1(frame({ type: "tree", body: empty }));
// Implement the js-git object interface using github APIs
module.exports = function (repo, root, accessToken) {
var apiRequest = xhr(root, accessToken);
repo.loadAs = loadAs; // (type, hash) -> value, hash
repo.saveAs = saveAs; // (type, value) -> hash, value
repo.readRef = readRef; // (ref) -> hash
repo.updateRef = updateRef; // (ref, hash) -> hash
repo.createTree = createTree; // (entries) -> hash, tree
repo.hasHash = hasHash;
function loadAs(type, hash, callback) {
if (!callback) return loadAs.bind(repo, type, hash);
// Github doesn't like empty trees, but we know them already.
function onBase(err, base) {
if (!base) return callback(err);
var object = codec.deframe(base);
var buffer;
try {
object.body = applyDelta(entry.body, object.body);
buffer = codec.frame(object);
}
catch (err) { return callback(err); }
callback(null, buffer);
}
});
function saveAs(type, body, callback) {
if (!callback) return saveAs.bind(repo, type, body);
var raw, hash;
try {
raw = codec.frame({
type: type,
body: codec.encoders[type](body)
});
hash = sha1(raw);
}
catch (err) { return callback(err); }
saveRaw(hash, raw, function (err) {
if (err) return callback(err);
callback(null, hash);
});
}
fs.readChunk(packFile, start, end, function (err, chunk) {
if (!chunk) return callback(err);
var raw;
try {
var entry = parsePackEntry(chunk);
if (entry.type === "ref-delta") {
return loadRaw.call(repo, hash, onBase);
}
else if (entry.type === "ofs-delta") {
return loadChunk(packFile, start - entry.ref, onBase);
}
raw = codec.frame(entry);
}
catch (err) { return callback(err); }
callback(null, raw);
function onBase(err, base) {
if (!base) return callback(err);
var object = codec.deframe(base);
var buffer;
try {
object.body = applyDelta(entry.body, object.body);
buffer = codec.frame(object);
}
catch (err) { return callback(err); }
callback(null, buffer);
}
});
function hashAs(type, body) {
var buffer = frame({type: type, body: body});
return sha1(buffer);
}
});