git ssb

0+

cel / pull-git-remote-helper



Commit 609ae28b8d57e7d4f47e0a987d5d1c7286001a60

Minimize buffer to ascii string conversions

It work now
Charles Lehner committed on 2/8/2016, 4:37:24 AM
Parent: 666eaeb2e3d956c1c7e70c3e279e5b993a71a829

Files changed

pack.jschanged
test/remote/git-remote-empty.jschanged
test/repo.jschanged
test/run.jschanged
test/pack.jsadded
pack.jsView
@@ -260,8 +260,11 @@
260260 }
261261 */
262262
263263 function encodePack(numObjects, readObject) {
264+ if (readObject === undefined)
265+ return encodePack.bind(this, numObjects)
266+
264267 // var ended
265268 var header = new Buffer(12)
266269 header.write('PACK')
267270 header.writeUInt32BE(PACK_VERSION, 4)
test/remote/git-remote-empty.jsView
@@ -23,9 +23,9 @@
2323 read,
2424 hasher,
2525 pull.collect(function (err, bufs) {
2626 if (err) throw err
27- var buf = Buffer.concat(bufs)
27+ var buf = Buffer.concat(bufs, length)
2828 console.error('obj', type, length, JSON.stringify(buf.toString('ascii')))
2929 process.send({object: {
3030 type: type,
3131 data: buf.toString('ascii'),
test/repo.jsView
@@ -12,30 +12,32 @@
1212 }
1313 user.str = user.name + ' <' + user.email + '>'
1414
1515 var file = {
16- name: 'blah.txt',
17- data: 'i am a file',
16+ data: new Buffer('i am a file'),
1817 hash: '68bd10497ea68e91fa85024d0a0b2fe54e212914'
1918 }
19+var fileName = 'blah.txt'
2020
2121 var tree = {
2222 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)
2425 }
2526
2627 var commitMessage = 'Initial commit'
2728 var commit = {
2829 hash: 'edb5b50e8019797925820007d318870f8c346726',
29- data: ['tree ' + tree.hash,
30+ data: new Buffer(['tree ' + tree.hash,
3031 'author ' + user.str + ' ' + date,
3132 'committer ' + user.str + ' ' + date,
3233 '', commitMessage, ''
33- ].join('\n')
34+ ].join('\n'))
3435 }
3536
3637 exports.date = date
3738 exports.user = user
3839 exports.file = file
40+exports.fileName = fileName
3941 exports.tree = tree
4042 exports.commitMessage = commitMessage
4143 exports.commit = commit
test/run.jsView
@@ -17,8 +17,9 @@
1717
1818 var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX'))
1919 tape.onFinish(function () {
2020 if (tmpDir)
21+ // console.error(tmpDir)
2122 rimraf.sync(tmpDir)
2223 })
2324
2425 function handleIpcMessage(t, cb) {
@@ -84,9 +85,9 @@
8485
8586 function obj(type, o) {
8687 return {
8788 type: type,
88- data: o.data,
89+ data: o.data.toString('ascii'),
8990 length: o.data.length,
9091 hash: o.hash
9192 }
9293 }
@@ -104,9 +105,9 @@
104105 old: null
105106 }, 'got the ref']
106107 ])
107108
108- var filePath = path.join(tmpDir, file.name)
109+ var filePath = path.join(tmpDir, repo.fileName)
109110 fs.writeFile(filePath, file.data, function (err) {
110111 t.error(err, 'wrote a file')
111112 t.git('add', filePath, function (code) {
112113 t.equals(code, 0, 'added file')
test/pack.jsView
@@ -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