Skip to content

Instantly share code, notes, and snippets.

@legokichi
Last active August 29, 2015 14:06
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 legokichi/8c89cd4314d9207952bc to your computer and use it in GitHub Desktop.
Save legokichi/8c89cd4314d9207952bc to your computer and use it in GitHub Desktop.
// This provides symbolic names for the octal modes used by git trees.
window.modes = require('./lib/modes');
// Create a repo by creating a plain object.
window.repo = {};
// This provides an in-memory storage backend that provides the following APIs:
// - saveAs(type, value) => hash
// - loadAs(type, hash) => hash
// - saveRaw(hash, binary) =>
// - loadRaw(hash) => binary
require('./mixins/mem-db')(repo);
// This adds a high-level API for creating multiple git objects by path.
// - createTree(entries) => hash
require('./mixins/create-tree')(repo);
// This provides extra methods for dealing with packfile streams.
// It depends on
// - unpack(packStream, opts) => hashes
// - pack(hashes, opts) => packStream
require('./mixins/pack-ops')(repo);
// This adds in walker algorithms for quickly walking history or a tree.
// - logWalk(ref|hash) => stream<commit>
// - treeWalk(hash) => stream<object>
require('./mixins/walkers')(repo);
// This combines parallel requests for the same resource for effeciency under load.
require('./mixins/read-combiner')(repo);
// This makes the object interface less strict. See it's docs for details
require('./mixins/formats')(repo);
<script src="bundle.js"></script>
$ browserify main.js -o bundle.js
$ open index.html
// First we create a blob from a string. The `formats` mixin allows us to
// use a string directly instead of having to pass in a binary buffer.
repo.saveAs("blob", "Hello World\n", function(err, blobHash){
console.log(err, blobHash);
// Now we create a tree that is a folder containing the blob as `greeting.txt`
repo.saveAs("tree", {
"greeting.txt": { mode: modes.file, hash: blobHash }
}, function(err, treeHash){
console.log(err, treeHash);
// With that tree, we can create a commit.
// Again the `formats` mixin allows us to omit details like committer, date,
// and parents. It assumes sane defaults for these.
repo.saveAs("commit", {
author: {
name: "Tim Caswell",
email: "tim@creationix.com"
},
tree: treeHash,
message: "Test commit\n"
}, function(err, commitHash){
console.log(err, commitHash);
});
});
});
repo.saveAs("blob", "Hello World\n", function(err, blobHash){
console.dir(blobHash);
repo.loadAs("blob", blobHash, function(err, byteArray){
console.log(byteArray);// -> [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 10]
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment