Commit 609ae28b8d57e7d4f47e0a987d5d1c7286001a60
Minimize buffer to ascii string conversions
It work nowCharles Lehner committed on 2/8/2016, 4:37:24 AM
Parent: 666eaeb2e3d956c1c7e70c3e279e5b993a71a829
Files changed
pack.js | changed |
test/remote/git-remote-empty.js | changed |
test/repo.js | changed |
test/run.js | changed |
test/pack.js | added |
pack.js | ||
---|---|---|
@@ -260,8 +260,11 @@ | ||
260 | 260 | } |
261 | 261 | */ |
262 | 262 | |
263 | 263 | function encodePack(numObjects, readObject) { |
264 | + if (readObject === undefined) | |
265 | + return encodePack.bind(this, numObjects) | |
266 | + | |
264 | 267 | // var ended |
265 | 268 | var header = new Buffer(12) |
266 | 269 | header.write('PACK') |
267 | 270 | header.writeUInt32BE(PACK_VERSION, 4) |
test/remote/git-remote-empty.js | ||
---|---|---|
@@ -23,9 +23,9 @@ | ||
23 | 23 | read, |
24 | 24 | hasher, |
25 | 25 | pull.collect(function (err, bufs) { |
26 | 26 | if (err) throw err |
27 | - var buf = Buffer.concat(bufs) | |
27 | + var buf = Buffer.concat(bufs, length) | |
28 | 28 | console.error('obj', type, length, JSON.stringify(buf.toString('ascii'))) |
29 | 29 | process.send({object: { |
30 | 30 | type: type, |
31 | 31 | data: buf.toString('ascii'), |
test/repo.js | ||
---|---|---|
@@ -12,30 +12,32 @@ | ||
12 | 12 | } |
13 | 13 | user.str = user.name + ' <' + user.email + '>' |
14 | 14 | |
15 | 15 | var file = { |
16 | - name: 'blah.txt', | |
17 | - data: 'i am a file', | |
16 | + data: new Buffer('i am a file'), | |
18 | 17 | hash: '68bd10497ea68e91fa85024d0a0b2fe54e212914' |
19 | 18 | } |
19 | +var fileName = 'blah.txt' | |
20 | 20 | |
21 | 21 | var tree = { |
22 | 22 | hash: '75c54aa020772a916853987a03bff7079463a861', |
23 | - data: '100644 ' + file.name + '\0' + hexToStr(file.hash) | |
23 | + data: new Buffer([0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x62, 0x6c, 0x61, 0x68, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x68, 0xbd, 0x10, 0x49, 0x7e, 0xa6, 0x8e, 0x91, 0xfa, 0x85, 0x02, 0x4d, 0x0a, 0x0b, 0x2f, 0xe5, 0x4e, 0x21, 0x29, 0x14]) | |
24 | + // data: '100644 ' + fileName + '\0' + hexToStr(file.hash) | |
24 | 25 | } |
25 | 26 | |
26 | 27 | var commitMessage = 'Initial commit' |
27 | 28 | var commit = { |
28 | 29 | hash: 'edb5b50e8019797925820007d318870f8c346726', |
29 | - data: ['tree ' + tree.hash, | |
30 | + data: new Buffer(['tree ' + tree.hash, | |
30 | 31 | 'author ' + user.str + ' ' + date, |
31 | 32 | 'committer ' + user.str + ' ' + date, |
32 | 33 | '', commitMessage, '' |
33 | - ].join('\n') | |
34 | + ].join('\n')) | |
34 | 35 | } |
35 | 36 | |
36 | 37 | exports.date = date |
37 | 38 | exports.user = user |
38 | 39 | exports.file = file |
40 | +exports.fileName = fileName | |
39 | 41 | exports.tree = tree |
40 | 42 | exports.commitMessage = commitMessage |
41 | 43 | exports.commit = commit |
test/run.js | ||
---|---|---|
@@ -17,8 +17,9 @@ | ||
17 | 17 | |
18 | 18 | var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX')) |
19 | 19 | tape.onFinish(function () { |
20 | 20 | if (tmpDir) |
21 | + // console.error(tmpDir) | |
21 | 22 | rimraf.sync(tmpDir) |
22 | 23 | }) |
23 | 24 | |
24 | 25 | function handleIpcMessage(t, cb) { |
@@ -84,9 +85,9 @@ | ||
84 | 85 | |
85 | 86 | function obj(type, o) { |
86 | 87 | return { |
87 | 88 | type: type, |
88 | - data: o.data, | |
89 | + data: o.data.toString('ascii'), | |
89 | 90 | length: o.data.length, |
90 | 91 | hash: o.hash |
91 | 92 | } |
92 | 93 | } |
@@ -104,9 +105,9 @@ | ||
104 | 105 | old: null |
105 | 106 | }, 'got the ref'] |
106 | 107 | ]) |
107 | 108 | |
108 | - var filePath = path.join(tmpDir, file.name) | |
109 | + var filePath = path.join(tmpDir, repo.fileName) | |
109 | 110 | fs.writeFile(filePath, file.data, function (err) { |
110 | 111 | t.error(err, 'wrote a file') |
111 | 112 | t.git('add', filePath, function (code) { |
112 | 113 | t.equals(code, 0, 'added file') |
test/pack.js | ||
---|---|---|
@@ -1,0 +1,81 @@ | ||
1 | +var tape = require('tape') | |
2 | +var pack = require('../pack') | |
3 | +var pull = require('pull-stream') | |
4 | +var repo = require('./repo') | |
5 | +var util = require('../util') | |
6 | + | |
7 | +var objects = [ | |
8 | + {type: 'commit', object: repo.commit}, | |
9 | + {type: 'tree', object: repo.tree}, | |
10 | + {type: 'blob', object: repo.file} | |
11 | +] | |
12 | + | |
13 | +function sha1(str) { | |
14 | + return require('crypto').createHash('sha1').update(str).digest('hex') | |
15 | +} | |
16 | + | |
17 | +function streamObject(read) { | |
18 | + var ended | |
19 | + return function readObject(abort, cb) { | |
20 | + read(abort, function (end, item) { | |
21 | + if (ended = end) return cb(end) | |
22 | + var data = item.object.data | |
23 | + cb(null, item.type, data.length, pull.once(data)) | |
24 | + }) | |
25 | + } | |
26 | +} | |
27 | + | |
28 | +function bufferObject(readObject) { | |
29 | + var ended | |
30 | + return function (abort, cb) { | |
31 | + readObject(abort, function next(end, type, length, read) { | |
32 | + if (ended = end) return cb(end) | |
33 | + var hasher = util.createGitObjectHash(type, length) | |
34 | + pull( | |
35 | + read, | |
36 | + hasher, | |
37 | + pull.collect(function (err, bufs) { | |
38 | + if (err) console.error(err) | |
39 | + // console.error('obj', type, length, JSON.stringify(buf.toString('ascii'))) | |
40 | + cb(err, { | |
41 | + type: type, | |
42 | + object: { | |
43 | + hash: hasher.digest('hex'), | |
44 | + data: Buffer.concat(bufs) | |
45 | + } | |
46 | + }) | |
47 | + }) | |
48 | + ) | |
49 | + }) | |
50 | + } | |
51 | +} | |
52 | + | |
53 | +tape('pack', function (t) { | |
54 | + var i = 0 | |
55 | + pull( | |
56 | + pull.values(objects), | |
57 | + streamObject, | |
58 | + pack.encode(objects.length), | |
59 | + pack.decode(function (err) { | |
60 | + t.error(err, 'decoded pack') | |
61 | + }), | |
62 | + bufferObject, | |
63 | + pull.drain(function (obj) { | |
64 | + var a = obj.object | |
65 | + var b = objects[i].object | |
66 | + if (a.hash != b.hash) | |
67 | + console.error(new Buffer(b.data)) | |
68 | + console.error(a.hash, b.hash, a.data.length, b.data.length, | |
69 | + a.data === b.data, | |
70 | + '"' + objects[i].type + ' ' + b.data.length + '\0' + b.data + '"', | |
71 | + sha1(objects[i].type + ' ' + b.data.length + '\0' + b.data)) | |
72 | + if (i < objects.length) | |
73 | + t.deepEquals(obj, objects[i++]) | |
74 | + else | |
75 | + t.notOk(obj, 'unexpected object') | |
76 | + }, function (err) { | |
77 | + t.error(err, 'got objects') | |
78 | + t.end() | |
79 | + }) | |
80 | + ) | |
81 | +}) |
Built with git-ssb-web