Skip to content

Instantly share code, notes, and snippets.

@creationix creationix/sample.js
Last active Dec 18, 2015

Embed
What would you like to do?
Low Level JS API for local git repos
var gitRepo = require('../.');
var fs = require('min-fs');
var run = require('gen-run');
var consume = require('../stream-to-string.js');
run(function* main() {
// Configure the repo API to work from a local clone.
var repo = gitRepo({ fs: fs("./my-repo") });
// Find out what sha1 hash HEAD points to
var hash = yield repo.db.read("HEAD");
console.log("HEAD", hash);
// Load that commit as an object
var head = yield repo.load(hash);
console.log(head);
// Load the tree it points to
var top = yield repo.load(head.commit.tree);
console.log(top);
// Load the first file in the tree
var obj = yield repo.load(top.tree[0].hash);
console.log(obj);
// Buffer the file stream to a string
var text = yield consume(obj.blob.source);
// Notice that hash is now defined
console.log({ hash: obj.hash, body: text });
// Load an annotated tag
hash = yield repo.db.read("/refs/tags/test-tag");
console.log("test-tag", hash);
var tag = yield repo.load(hash);
console.log(tag);
});
tim@localhost:~/Code/git-repo$ cd example/
tim@localhost:~/Code/git-repo/example$ node --harmony-generators sample.js
HEAD 168b0147775a6c240893ee7e12671fbd58144ca2
{ hash: '168b0147775a6c240893ee7e12671fbd58144ca2',
commit:
{ parents: [],
message: 'Initial Commit\n',
tree: '5e1f1f71e00c3a16333121d492d88556d298e479',
author: 'Tim Caswell <tim@creationix.com> 1371196478 -0500',
committer: 'Tim Caswell <tim@creationix.com> 1371196478 -0500' } }
{ hash: '5e1f1f71e00c3a16333121d492d88556d298e479',
tree:
[ { mode: 33188,
path: 'README.md',
hash: 'e7aecb2c038b16c2ef544b1413d41cbe40aa514d' } ] }
{ hash: undefined,
blob: { length: 44, source: [Function: read] } }
{ hash: 'e7aecb2c038b16c2ef544b1413d41cbe40aa514d',
body: '# Sample Project\n\nThis is a sample project.\n' }
test-tag a58ff6c38f1af29044dd2b5b796b78a8ebd114bc
{ hash: 'a58ff6c38f1af29044dd2b5b796b78a8ebd114bc',
tag:
{ message: 'This is a test tag\n',
object: '168b0147775a6c240893ee7e12671fbd58144ca2',
type: 'commit',
tag: 'test-tag',
tagger: 'Tim Caswell <tim@creationix.com> 1371204916 -0500' } }
var gitRepo = require('../.');
var fs = require('min-fs');
var consume = require('../stream-to-string.js');
// Configure the repo API to work from a local clone.
var repo = gitRepo({ fs: fs("./my-repo") });
// Find out what sha1 hash HEAD points to
repo.db.read("HEAD")(function (err, hash) {
if (err) throw err;
console.log("HEAD", hash);
// Load that commit as an object
repo.load(hash)(function (err, head) {
if (err) throw err;
console.log(head);
// Load the tree it points to
repo.load(head.commit.tree)(function (err, top) {
if (err) throw err;
console.log(top);
// Load the first file in the tree
repo.load(top.tree[0].hash)(function (err, obj) {
if (err) throw err;
console.log(obj);
// Buffer the file stream to a string
consume(obj.blob.source)(function (err, text) {
if (err) throw err;
// Notice that hash is now defined
console.log({ hash: obj.hash, body: text });
});
});
});
});
});
// Load an annotated tag
repo.db.read("/refs/tags/test-tag")(function (err, hash) {
if (err) throw err;
console.log("test-tag", hash);
repo.load(hash)(function (err, tag) {
if (err) throw err;
console.log(tag);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.