Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51873b5
commit b9305d7
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# git-object-tree | ||
|
||
git tree objects as javascript objects. | ||
|
||
tree objects are immutable once created. | ||
|
||
```javascript | ||
var Buffer = require('buffer').Buffer | ||
, tree = require('git-object-tree') | ||
|
||
var b = tree.create(new Buffer(...)) | ||
|
||
b = tree.read(<some git buffer>) | ||
|
||
``` | ||
|
||
## API | ||
|
||
#### tree.read(<git buffer>) -> Tree | ||
|
||
read a tree from some git buffer data. | ||
|
||
#### tree.create(author, tree, message, parent[, treeter][, attrs]) -> Tree | ||
|
||
create a tree from some source data. | ||
|
||
all fields (save for message) may be arrays. | ||
|
||
#### Tree.prototype.entries() -> [entry list] | ||
|
||
return a list of tree entries: | ||
|
||
```javascript | ||
{ mode: number | ||
, name: string | ||
, hash: Buffer(20) } | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
module.exports = {read: read, create: create} | ||
|
||
function Tree(members, raw) { | ||
this._members = members | ||
this._raw = raw | ||
} | ||
|
||
var cons = Tree | ||
, proto = cons.prototype | ||
|
||
proto.type = 2 | ||
proto.looseType = 'tree' | ||
|
||
proto.entries = function() { | ||
return this._members.slice() | ||
} | ||
|
||
proto.serialize = function() { | ||
if(this._raw) { | ||
return this._raw | ||
} | ||
|
||
var buffers = [] | ||
, size = 0 | ||
, buf | ||
|
||
for(var i = 0, len = this._members.length; i < len; ++i) { | ||
buf = new Buffer(pad(this._members[i].mode.toString(8))+' ', 'utf8') | ||
size += buf.length | ||
buffers.push(buf) | ||
buf = new Buffer(this._members[i].name + '\0', 'utf8') | ||
size += buf.length | ||
buffers.push(buf) | ||
|
||
if(typeof this._members[i].hash === 'string') { | ||
buf = new Buffer(this._members[i].hash, 'hex') | ||
} else { | ||
buf = this._members[i].hash | ||
} | ||
size += buf.length | ||
buffers.push(buf) | ||
} | ||
|
||
return Buffer.concat(buffers, size) | ||
} | ||
|
||
var STATE_MODE = 0 | ||
, STATE_NAME = 1 | ||
, STATE_HASH = 2 | ||
|
||
function read(buf) { | ||
var idx = 0 | ||
, len = buf.length | ||
, state = STATE_MODE | ||
, start = 0 | ||
, end_mode = 0 | ||
, end_name = 0 | ||
, members = [] | ||
, _byte | ||
|
||
while(idx < len) { | ||
_byte = buf.readUInt8(idx++) | ||
switch(state) { | ||
case STATE_MODE: mode(); break | ||
case STATE_NAME: name(); break | ||
case STATE_HASH: hash(); break | ||
} | ||
} | ||
|
||
return new Tree(members, buf) | ||
|
||
function mode() { | ||
if(_byte !== 32) { | ||
return | ||
} | ||
end_mode = idx | ||
state = STATE_NAME | ||
} | ||
|
||
function name() { | ||
if(_byte !== 0) { | ||
return | ||
} | ||
end_name = idx | ||
state = STATE_HASH | ||
} | ||
|
||
function hash() { | ||
if(idx - end_name < 20) { | ||
return | ||
} | ||
|
||
members.push({ | ||
mode: parseInt(buf.slice(start, end_mode - 1).toString('utf8'), 8) | ||
, name: buf.slice(end_mode, end_name - 1).toString('utf8') | ||
, hash: buf.slice(end_name, idx) | ||
}) | ||
|
||
start = idx | ||
state = STATE_MODE | ||
} | ||
} | ||
|
||
// members = [{id: <hash>, mode: <number>, name: <string>}] | ||
function create(members) { | ||
return new Tree(members) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "git-object-tree", | ||
"version": "0.0.0", | ||
"description": "parse and create git tree objects", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/chrisdickinson/git-object-tree.git" | ||
}, | ||
"keywords": [ | ||
"git", | ||
"tree", | ||
"object" | ||
], | ||
"author": "Chris Dickinson <chris@neversaw.us>", | ||
"license": "MIT", | ||
"gitHead": "51873b555c78d4eeb4750e6dcf9e2bc33dadeb2e" | ||
} |