Commit f60f57ddecf31964b98f5147c7ee02faa6c871fc
Add length to object sink. Test hash and length
Charles Lehner committed on 2/8/2016, 4:37:24 AMParent: bd6d9a71a043a6ca2c3ea890cd8f1798dfd9d2cc
Files changed
pack.js | changed |
test/git-remote-test.js | changed |
test/run.js | changed |
util.js | added |
pack.js | ||
---|---|---|
@@ -1,9 +1,9 @@ | ||
1 | 1 | var buffered = require('pull-buffered') |
2 | -var crypto = require('crypto') | |
3 | 2 | var pull = require('pull-stream') |
4 | 3 | var toPull = require('stream-to-pull-stream') |
5 | 4 | var Inflate = require('pako/lib/inflate').Inflate |
5 | +var createHash = require('./util').createHash | |
6 | 6 | |
7 | 7 | exports.decode = decodePack |
8 | 8 | |
9 | 9 | var objectTypes = [ |
@@ -16,15 +16,8 @@ | ||
16 | 16 | cb(err || true) |
17 | 17 | } |
18 | 18 | } |
19 | 19 | |
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 | - | |
27 | 20 | function inflateBytes(read) { |
28 | 21 | var inflate = new Inflate() |
29 | 22 | var ended, dataOut |
30 | 23 | |
@@ -145,9 +138,9 @@ | ||
145 | 138 | if (end === true && expectChecksum) |
146 | 139 | onEnd(new Error('Missing checksum')) |
147 | 140 | if (ended = end) return cb(end) |
148 | 141 | // TODO: verify that the inflated data is the correct length |
149 | - cb(null, type, inflateBytes(readByte)) | |
142 | + cb(null, type, length, inflateBytes(readByte)) | |
150 | 143 | }) |
151 | 144 | } |
152 | 145 | |
153 | 146 | function readTrailer(cb) { |
test/git-remote-test.js | ||
---|---|---|
@@ -1,8 +1,9 @@ | ||
1 | 1 | #!/usr/bin/env node |
2 | 2 | |
3 | 3 | var toPull = require('stream-to-pull-stream') |
4 | 4 | var pull = require('pull-stream') |
5 | +var util = require('../util') | |
5 | 6 | |
6 | 7 | process.on('uncaughtException', function (err) { |
7 | 8 | if (err.stack) |
8 | 9 | err = {stack: err.stack, message: err.message} |
@@ -14,17 +15,24 @@ | ||
14 | 15 | toPull(process.stdin), |
15 | 16 | require('../')({ |
16 | 17 | prefix: 'foo', |
17 | 18 | objectSink: function (readObject) { |
18 | - readObject(null, function next(end, type, read) { | |
19 | + readObject(null, function next(end, type, length, read) { | |
19 | 20 | if (end === true) return |
20 | 21 | if (end) throw end |
22 | + var hasher = util.createGitObjectHash(type, length) | |
21 | 23 | pull( |
22 | 24 | read, |
25 | + hasher, | |
23 | 26 | pull.collect(function (err, bufs) { |
24 | 27 | 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 | + }}) | |
27 | 35 | readObject(null, next) |
28 | 36 | }) |
29 | 37 | ) |
30 | 38 | }) |
test/run.js | ||
---|---|---|
@@ -13,9 +13,9 @@ | ||
13 | 13 | var user = { |
14 | 14 | name: 'test', |
15 | 15 | email: 'test@localhost' |
16 | 16 | } |
17 | -var userStr = user.name + ' <' + user.email + '>' | |
17 | +user.str = user.name + ' <' + user.email + '>' | |
18 | 18 | var remote = 'test.js://foo' |
19 | 19 | |
20 | 20 | var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX')) |
21 | 21 | |
@@ -71,42 +71,61 @@ | ||
71 | 71 | t.end() |
72 | 72 | }) |
73 | 73 | }) |
74 | 74 | |
75 | +function hexToStr(str) { | |
76 | + var buf = new Buffer(str.length / 2) | |
77 | + buf.hexWrite(str) | |
78 | + return buf.toString('ascii') | |
79 | +} | |
80 | + | |
75 | 81 | 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 | + | |
76 | 93 | 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 | + } | |
84 | 102 | |
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 | + | |
85 | 112 | 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'] | |
97 | 116 | ]) |
98 | 117 | |
99 | 118 | var refs = t.items(t.deepEquals, [ |
100 | 119 | [{ |
101 | 120 | name: 'refs/heads/master', |
102 | - new: commitHash, | |
121 | + new: commit.hash, | |
103 | 122 | old: null |
104 | 123 | }, 'got the ref'] |
105 | 124 | ]) |
106 | 125 | |
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) { | |
109 | 128 | t.error(err, 'wrote a file') |
110 | 129 | t.git('add', filePath, function (code) { |
111 | 130 | t.equals(code, 0, 'added file') |
112 | 131 | t.git('commit', '-m', commitMessage, function (code) { |
util.js | ||
---|---|---|
@@ -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