git ssb

0+

cel / pull-git-remote-helper



Commit f60f57ddecf31964b98f5147c7ee02faa6c871fc

Add length to object sink. Test hash and length

Charles Lehner committed on 2/8/2016, 4:37:24 AM
Parent: bd6d9a71a043a6ca2c3ea890cd8f1798dfd9d2cc

Files changed

pack.jschanged
test/git-remote-test.jschanged
test/run.jschanged
util.jsadded
pack.jsView
@@ -1,9 +1,9 @@
11 var buffered = require('pull-buffered')
2-var crypto = require('crypto')
32 var pull = require('pull-stream')
43 var toPull = require('stream-to-pull-stream')
54 var Inflate = require('pako/lib/inflate').Inflate
5+var createHash = require('./util').createHash
66
77 exports.decode = decodePack
88
99 var objectTypes = [
@@ -16,15 +16,8 @@
1616 cb(err || true)
1717 }
1818 }
1919
20-function createHash(type) {
21- var hash = crypto.createHash(type)
22- var hasher = pull.through(hash.update.bind(hash))
23- hasher.digest = hash.digest.bind(hash)
24- return hasher
25-}
26-
2720 function inflateBytes(read) {
2821 var inflate = new Inflate()
2922 var ended, dataOut
3023
@@ -145,9 +138,9 @@
145138 if (end === true && expectChecksum)
146139 onEnd(new Error('Missing checksum'))
147140 if (ended = end) return cb(end)
148141 // TODO: verify that the inflated data is the correct length
149- cb(null, type, inflateBytes(readByte))
142+ cb(null, type, length, inflateBytes(readByte))
150143 })
151144 }
152145
153146 function readTrailer(cb) {
test/git-remote-test.jsView
@@ -1,8 +1,9 @@
11 #!/usr/bin/env node
22
33 var toPull = require('stream-to-pull-stream')
44 var pull = require('pull-stream')
5+var util = require('../util')
56
67 process.on('uncaughtException', function (err) {
78 if (err.stack)
89 err = {stack: err.stack, message: err.message}
@@ -14,17 +15,24 @@
1415 toPull(process.stdin),
1516 require('../')({
1617 prefix: 'foo',
1718 objectSink: function (readObject) {
18- readObject(null, function next(end, type, read) {
19+ readObject(null, function next(end, type, length, read) {
1920 if (end === true) return
2021 if (end) throw end
22+ var hasher = util.createGitObjectHash(type, length)
2123 pull(
2224 read,
25+ hasher,
2326 pull.collect(function (err, bufs) {
2427 if (err) throw err
25- var data = Buffer.concat(bufs).toString('ascii')
26- process.send({object: {type: type, data: data}})
28+ var buf = Buffer.concat(bufs)
29+ process.send({object: {
30+ type: type,
31+ data: buf.toString('ascii'),
32+ length: length,
33+ hash: hasher.digest('hex')
34+ }})
2735 readObject(null, next)
2836 })
2937 )
3038 })
test/run.jsView
@@ -13,9 +13,9 @@
1313 var user = {
1414 name: 'test',
1515 email: 'test@localhost'
1616 }
17-var userStr = user.name + ' <' + user.email + '>'
17+user.str = user.name + ' <' + user.email + '>'
1818 var remote = 'test.js://foo'
1919
2020 var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX'))
2121
@@ -71,42 +71,61 @@
7171 t.end()
7272 })
7373 })
7474
75+function hexToStr(str) {
76+ var buf = new Buffer(str.length / 2)
77+ buf.hexWrite(str)
78+ return buf.toString('ascii')
79+}
80+
7581 tape('make a commit and push', function (t) {
82+ var file = {
83+ name: 'blah.txt',
84+ data: 'i am a file',
85+ hash: '68bd10497ea68e91fa85024d0a0b2fe54e212914'
86+ }
87+
88+ var tree = {
89+ hash: '75c54aa020772a916853987a03bff7079463a861',
90+ data: '100644 ' + file.name + '\0' + hexToStr(file.hash)
91+ }
92+
7693 var commitMessage = 'Initial commit'
77- var fileName = 'blah.txt'
78- var fileContents = 'i am a file'
79- var fileHash = '68bd10497ea68e91fa85024d0a0b2fe54e212914'
80- var treeHash = '75c54aa020772a916853987a03bff7079463a861'
81- var commitHash = 'edb5b50e8019797925820007d318870f8c346726'
82- var fileHashBuf = new Buffer(20)
83- fileHashBuf.hexWrite(fileHash)
94+ var commit = {
95+ hash: 'edb5b50e8019797925820007d318870f8c346726',
96+ data: ['tree ' + tree.hash,
97+ 'author ' + user.str + ' 1000000000 -0500',
98+ 'committer ' + user.str + ' 1000000000 -0500',
99+ '', commitMessage, ''
100+ ].join('\n')
101+ }
84102
103+ function obj(type, o) {
104+ return {
105+ type: type,
106+ data: o.data,
107+ length: o.data.length,
108+ hash: o.hash
109+ }
110+ }
111+
85112 var objects = t.items(t.deepEquals, [
86- [{
87- type: 'commit',
88- data: 'tree ' + treeHash + '\nauthor ' + userStr + ' 1000000000 -0500\ncommitter ' + userStr + ' 1000000000 -0500\n\n' + commitMessage + '\n'
89- }, 'got the commit'],
90- [{
91- type: 'tree',
92- data: '100644 ' + fileName + '\0' + fileHashBuf.toString('ascii')
93- }, 'got the tree'],
94- [{
95- type: 'blob', data: fileContents
96- }, 'got the blob']
113+ [obj('commit', commit), 'got the commit'],
114+ [obj('tree', tree), 'got the tree'],
115+ [obj('blob', file), 'got the blob']
97116 ])
98117
99118 var refs = t.items(t.deepEquals, [
100119 [{
101120 name: 'refs/heads/master',
102- new: commitHash,
121+ new: commit.hash,
103122 old: null
104123 }, 'got the ref']
105124 ])
106125
107- var filePath = path.join(tmpDir, fileName)
108- fs.writeFile(filePath, fileContents, function (err) {
126+ var filePath = path.join(tmpDir, file.name)
127+ fs.writeFile(filePath, file.data, function (err) {
109128 t.error(err, 'wrote a file')
110129 t.git('add', filePath, function (code) {
111130 t.equals(code, 0, 'added file')
112131 t.git('commit', '-m', commitMessage, function (code) {
util.jsView
@@ -1,0 +1,16 @@
1+var crypto = require('crypto')
2+var pull = require('pull-stream')
3+
4+exports.createHash = function (type) {
5+ var hash = crypto.createHash(type)
6+ var hasher = pull.through(hash.update.bind(hash))
7+ hasher.hash = hash
8+ hasher.digest = hash.digest.bind(hash)
9+ return hasher
10+}
11+
12+exports.createGitObjectHash = function (objectType, objectLength) {
13+ var hasher = exports.createHash('sha1')
14+ hasher.hash.update(objectType + ' ' + objectLength + '\0')
15+ return hasher
16+}

Built with git-ssb-web