node-git-core
Library that provides simple object-oriented api for working with git data at a
lower level, see git internals for more info:
Installation
npm install git-core
Usage
git = require('git-core');
Blob = git.Blob;
Tree = git.Tree;
Commit = git.Commit;
Tag = git.Tag;
Pack = git.Pack;
b1 = new Blob('Some file');
b2 = new Blob(new Buffer([1,2,3,4,5]));
b3 = new Blob('Another file\n');
t1 = new Tree({
'file-under-tree': b3
});
t2 = new Tree({
'some-file.txt': b2,
'some-file2.txt': b1,
'sub-directory.d': t1
});
t3 = new Tree({
'another-file.txt': b1
});
c1 = new Commit({
tree: t1,
author: {
name: 'Git Author',
email: 'author@git.com',
date: d1
},
message: 'Artificial commit 1'
});
c2 = new Commit({
tree: t2,
author: {
name: 'Git Author',
email: 'author@git.com',
date: d2
},
message: 'Artificial commit 2',
parents: [c1]
});
c3 = new Commit({
tree: t3,
author: {
name: 'Git User',
email: 'user@domain.com',
date: d3
},
committer: {
name: 'Git Commiter',
email: 'committer@git.com',
date: d4
},
message: 'Artificial commit 3',
parents: [c2]
});
tag = new Tag({
object: c2,
name: 'v0.0.1',
tagger: {
name: 'Git Tagger',
email: 'tagger@git.com'
},
date: d2,
message: 'Tag second commit'
});
pack = new Pack([c3, tag]);
serializedPack = pack.serialize();
This library is all about working with git data in-memory, no repositories are
needed. Above is an example
on how git objects can be created, connected and serialized, the inverse
is also supported:
pack = Pack.deserialize(buffer);
for (var i = 0;i < pack.objects.length;i++) {
var obj = pack.objects[i];
if (obj instanceof Blob) {
console.log(obj.serialize().getHash(), ':', obj.contents.toString()));
}
}
pack = Pack.deserialize(buffer, function(baseSha1) {
});
Delta compression is only fully supported on 'deserialization. If you need to
encode objects using delta compression then add the deltas manually:
str = '';
for (i = _i = 0; _i < 1000; i = ++_i) {
str += 'test content/test content2/test content3\n';
}
b1 = new Blob(str);
b2 = new Blob(str + 'append\n');
b3 = new Blob(str + 'append\nappend2\n');
b4 = new Blob(str + 'append\nappend2\nappend3\n');
pack = new Pack([
b1,
b2.diff(b1),
b3.diff(b2),
b4.diff(b3)
]);
pack.serialize();