Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unpack + test against a temporary git repo
- Loading branch information
Showing
9 changed files
with
362 additions
and
53 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,10 @@ | ||
module.exports.getType = function(typeStr) { | ||
return { | ||
"001":"commit", | ||
"010":"tree", | ||
"011":"blob", | ||
"100":"tag", | ||
"110":"ofs_delta", | ||
"111":"ref_delta" | ||
}[typeStr] | ||
} |
Empty file.
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,24 @@ | ||
var PackObject = function(type, buffer, offset) { | ||
this.type = type; | ||
this.buffer = buffer; | ||
this.offset = offset; | ||
} | ||
|
||
PackObject.prototype.type = null; | ||
|
||
|
||
// createPackObject | ||
module.exports.createPackObject = function(type, buffer, offset) { | ||
return new PackObject(type, buffer, offset); | ||
}; | ||
|
||
module.exports.OBJ_BAD = -1, | ||
module.exports.OBJ_NONE = 0, | ||
module.exports.OBJ_COMMIT = 1, | ||
module.exports.OBJ_TREE = 2, | ||
module.exports.OBJ_BLOB = 3, | ||
module.exports.OBJ_TAG = 4, | ||
module.exports.OBJ_OFS_DELTA = 6, | ||
module.exports.OBJ_REF_DELTA = 7, | ||
module.exports.OBJ_ANY, | ||
module.exports.OBJ_MAX |
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,102 @@ | ||
|
||
// Unpack a git pack file | ||
// see: http://www.kernel.org/pub/software/scm/git/docs/technical/pack-format.txt | ||
|
||
var Buffer = require('buffer').Buffer, | ||
packObject = require('./pack/object'), | ||
getType = require('../common/pack').getType, | ||
rawInflate = require('../common/rawinflate'), | ||
Git = require('../git');; | ||
|
||
// TODO: I really want to use this, but there is no way to get the | ||
// number of bytes it consumed | ||
//zlib = require('zlib').createInflate(); | ||
|
||
git = new Git(); | ||
|
||
module.exports = function unpack(buffer, fn) { | ||
|
||
var obj = { | ||
version: null, | ||
count: null, | ||
objects : [] | ||
}, | ||
where = 0, | ||
fwd = function(amount) { | ||
where += amount; | ||
return where; | ||
}; | ||
|
||
if (!Buffer.isBuffer(buffer)) { | ||
return fn(new Error('Unpack expects a buffer')); | ||
} | ||
|
||
if (buffer.length < 4 || buffer.slice(0, 4).toString('ascii') !== 'PACK') { | ||
return fn(new Error('Invalid signature')); | ||
} | ||
|
||
if (buffer.length < 8) { | ||
return fn(new Error('Invalid version length')); | ||
} | ||
|
||
obj.version = buffer.readUInt32BE(4); | ||
if (obj.version !== 2) { | ||
return fn(new Error('Invalid version (expected 2)')); | ||
} | ||
|
||
if (buffer.length < 12) { | ||
return fn(new Error('Invalid object count')); | ||
} | ||
|
||
obj.count = buffer.readUInt32BE(8); | ||
|
||
if (obj.count > 0 && buffer.length === 12) { | ||
return fn(new Error('Invalid object entry section'), obj) | ||
} | ||
|
||
var where = 12, objectIndex = 0; | ||
var readObject = function(buffer) { | ||
if (objectIndex < obj.count) { | ||
|
||
// Read the type | ||
var c = buffer.readUInt8(where); | ||
|
||
var type = (c >> 4) & 7; | ||
var length = (c & 15); | ||
var shift = 4; | ||
var offset = where; | ||
|
||
fwd(1); | ||
while (c & 0x80) { | ||
c = buffer.readUInt8(where); | ||
length += (c & 0x7f) << shift; | ||
shift += 7; | ||
fwd(1); | ||
} | ||
|
||
var inflated = rawInflate(git.bytesToString(buffer.slice(fwd(2)))); | ||
// TODO: WARNING!! the compressed length returned by this function is incorrect | ||
|
||
// TODO: collect the sha | ||
if (type === packObject.OBJ_OFS_DELTA || type === packObject.OBJ_REF_DELTA) { | ||
fwd(20); | ||
} | ||
|
||
fwd(inflated.compressedLength + 4); | ||
|
||
obj.objects.push(packObject.createPackObject(type, inflated.toString() , offset)); | ||
|
||
process.nextTick(function() { | ||
objectIndex++; | ||
readObject(buffer); | ||
}); | ||
|
||
} else { | ||
console.log(obj); | ||
process.exit(); | ||
fn(null, obj); | ||
} | ||
}; | ||
|
||
readObject(buffer); | ||
}; |
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
read this | ||
hello world |
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
Submodule repo-pack
added at
db9c8a
Oops, something went wrong.