git ssb

0+

cel / pull-git-remote-helper



Commit 5545486e2d374382a5c133c77bae7172e35e95fb

Factor out pktLine and split utils

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

Files changed

index.jschanged
lib/util.jschanged
lib/pkt-line.jsadded
index.jsView
@@ -2,8 +2,10 @@
22 var pull = require('pull-stream')
33 var cat = require('pull-cat')
44 var buffered = require('pull-buffered')
55 var pack = require('./lib/pack')
6+var pktLine = require('./lib/pkt-line')
7+var util = require('./lib/util')
68
79 function handleOption(options, name, value) {
810 switch (name) {
911 case 'verbosity':
@@ -24,23 +26,10 @@
2426 'connect',
2527 ].join('\n') + '\n\n')
2628 }
2729
28-function split2(str, delim) {
29- var i = str.indexOf(delim || ' ')
30- return (i === -1) ? [str, ''] : [
31- str.substr(0, i),
32- str.substr(i + 1)
33- ]
34-}
35-
36-function split3(str) {
37- var args = split2(str)
38- return [args[0]].concat(split2(args[1]))
39-}
40-
4130 function optionSource(cmd, options) {
42- var args = split2(cmd)
31+ var args = util.split2(cmd)
4332 var msg = handleOption(options, args[0], args[1])
4433 msg = (msg === true) ? 'ok'
4534 : (msg === false) ? 'unsupported'
4635 : 'error ' + msg
@@ -69,18 +58,18 @@
6958 * agent=git/2.7.0 */
7059 var sendRefs = receivePackHeader([
7160 ], refSource, false)
7261
73- var lines = packLineDecode(read, options)
62+ var lines = pktLine.decode(read, options)
7463 var readHave = lines.haves()
7564 var acked
7665 var commonHash
7766 var sendPack
7867 var earlyDisconnect
7968
8069 // Packfile negotiation
8170 return cat([
82- packLineEncode(cat([
71+ pktLine.encode(cat([
8372 sendRefs,
8473 pull.once(''),
8574 function (abort, cb) {
8675 if (abort) return
@@ -142,121 +131,8 @@
142131 }
143132 ])
144133 }
145134
146-function packLineEncode(read) {
147- var ended
148- return function (end, cb) {
149- if (ended) return cb(ended)
150- read(end, function (end, data) {
151- if (ended = end) {
152- cb(end)
153- } else {
154- if (data)
155- data += '\n'
156- else
157- data = ''
158- var len = data ? data.length + 4 : 0
159- var hexLen = ('000' + len.toString(16)).substr(-4)
160- var pkt = hexLen + data
161- // console.error('>', JSON.stringify(pkt))
162- cb(end, pkt)
163- }
164- })
165- }
166-}
167-
168-function rev(str) {
169- return str === '0000000000000000000000000000000000000000' ? null : str
170-}
171-
172-/* pull-stream/source.js */
173-function abortCb(cb, abort, onAbort) {
174- cb(abort)
175- onAbort && onAbort(abort === true ? null: abort)
176- return
177-}
178-
179-function packLineDecode(read, options) {
180- var b = buffered(read)
181- var readPrefix = b.chunks(4)
182- var ended
183-
184- function readPackLine(abort, cb) {
185- if (ended) return cb(ended)
186- readPrefix(abort, function (end, buf) {
187- if (ended = end) return cb(end)
188- var len = parseInt(buf, 16)
189- if (!len)
190- return cb(null, new Buffer(''))
191- // TODO: figure out this -4 thing
192- b.chunks(len - 4)(null, function (end, buf) {
193- if (ended = end) return cb(end)
194- cb(end, buf)
195- })
196- })
197- }
198-
199- function readPackLineStr(abort, cb) {
200- if (ended) return cb(ended)
201- readPackLine(abort, function (end, buf) {
202- if (ended = end) return cb(end)
203- // trim newline
204- var len = buf.length
205- if (buf[len - 1] == 0xa)
206- len--
207- var line = buf.toString('ascii', 0, len)
208- cb(null, line)
209- })
210- }
211-
212- function readUpdate(abort, cb) {
213- readPackLine(abort, function (end, line) {
214- if (end) return cb(end)
215- if (options.verbosity >= 2)
216- console.error('line', line.toString('ascii'))
217- if (!line.length) return cb(true)
218- var args = split3(line.toString('ascii'))
219- var args2 = split2(args[2], '\0')
220- var caps = args2[1]
221- if (caps && options.verbosity >= 2)
222- console.error('update capabilities:', caps)
223- cb(null, {
224- old: rev(args[0]),
225- new: rev(args[1]),
226- name: args2[0]
227- })
228- })
229- }
230-
231- function havesWants(onEnd) {
232- return function readWant(abort, cb) {
233- readPackLineStr(abort, function (end, line) {
234- if (end) return abortCb(cb, end, onEnd)
235- if (options.verbosity >= 2)
236- console.error('line', line)
237- // if (!line.length) return cb(true)
238- if (!line.length || line == 'done')
239- return abortCb(cb, true, onEnd)
240- var args = split3(line)
241- var caps = args[2]
242- if (caps && options.verbosity >= 2)
243- console.error('want capabilities:', caps)
244- cb(null, {
245- type: args[0],
246- hash: args[1],
247- })
248- })
249- }
250- }
251-
252- b.packLines = readPackLine
253- b.updates = readUpdate
254- b.wants = b.haves = havesWants
255-
256- return b
257-}
258-
259135 // run a callback when a pipeline ends
260136 // TODO: find a better way to do this
261137 function onThroughEnd(onEnd) {
262138 return function (read) {
@@ -275,9 +151,9 @@
275151 report-status delete-refs side-band-64k quiet atomic ofs-delta
276152 */
277153
278154 // Get a line for each ref that we have. The first line also has capabilities.
279-// Wrap with packLineEncode.
155+// Wrap with pktLine.encode.
280156 function receivePackHeader(capabilities, refSource, usePlaceholder) {
281157 var first = true
282158 var ended
283159 return function (abort, cb) {
@@ -308,17 +184,17 @@
308184 var sendRefs = receivePackHeader([
309185 'delete-refs',
310186 ], refSource, true)
311187
312- return packLineEncode(
188+ return pktLine.encode(
313189 cat([
314190 // send our refs
315191 sendRefs,
316192 pull.once(''),
317193 function (abort, cb) {
318194 if (abort) return
319195 // receive their refs
320- var lines = packLineDecode(read, options)
196+ var lines = pktLine.decode(read, options)
321197 pull(
322198 lines.updates,
323199 onThroughEnd(refsDone),
324200 refSink
@@ -365,9 +241,9 @@
365241 progress: false
366242 }
367243
368244 function handleConnect(cmd, read) {
369- var args = split2(cmd)
245+ var args = util.split2(cmd)
370246 switch (args[0]) {
371247 case 'git-upload-pack':
372248 return prepend('\n', uploadPack(read, haveObject, getObjects, refSource,
373249 wantSink, options))
@@ -379,9 +255,9 @@
379255 }
380256 }
381257
382258 function handleCommand(line, read) {
383- var args = split2(line)
259+ var args = util.split2(line)
384260 switch (args[0]) {
385261 case 'capabilities':
386262 return capabilitiesSource()
387263 case 'list':
lib/util.jsView
@@ -18,4 +18,17 @@
1818 var hasher = exports.createHash('sha1')
1919 hasher.hash.update(objectType + ' ' + objectLength + '\0')
2020 return hasher
2121 }
22+
23+exports.split2 = function (str, delim) {
24+ var i = str.indexOf(delim || ' ')
25+ return (i === -1) ? [str, ''] : [
26+ str.substr(0, i),
27+ str.substr(i + 1)
28+ ]
29+}
30+
31+exports.split3 = function (str) {
32+ var args = exports.split2(str)
33+ return [args[0]].concat(exports.split2(args[1]))
34+}
lib/pkt-line.jsView
@@ -1,0 +1,116 @@
1+var buffered = require('pull-buffered')
2+var util = require('./util')
3+
4+function rev(str) {
5+ return str === '0000000000000000000000000000000000000000' ? null : str
6+}
7+
8+// from pull-stream/source.js
9+function abortCb(cb, abort, onAbort) {
10+ cb(abort)
11+ onAbort && onAbort(abort === true ? null: abort)
12+ return
13+}
14+
15+function pktLineEncode(read) {
16+ var ended
17+ return function (end, cb) {
18+ if (ended) return cb(ended)
19+ read(end, function (end, data) {
20+ if (ended = end) {
21+ cb(end)
22+ } else {
23+ if (data)
24+ data += '\n'
25+ else
26+ data = ''
27+ var len = data ? data.length + 4 : 0
28+ var hexLen = ('000' + len.toString(16)).substr(-4)
29+ var pkt = hexLen + data
30+ // console.error('>', JSON.stringify(pkt))
31+ cb(end, pkt)
32+ }
33+ })
34+ }
35+}
36+
37+function pktLineDecode(read, options) {
38+ var b = buffered(read)
39+ var readPrefix = b.chunks(4)
40+ var ended
41+
42+ function readPackLine(abort, cb) {
43+ if (ended) return cb(ended)
44+ readPrefix(abort, function (end, buf) {
45+ if (ended = end) return cb(end)
46+ var len = parseInt(buf, 16)
47+ if (!len)
48+ return cb(null, new Buffer(''))
49+ b.chunks(len - 4)(null, function (end, buf) {
50+ if (ended = end) return cb(end)
51+ cb(end, buf)
52+ })
53+ })
54+ }
55+
56+ function readPackLineStr(abort, cb) {
57+ if (ended) return cb(ended)
58+ readPackLine(abort, function (end, buf) {
59+ if (ended = end) return cb(end)
60+ // trim newline
61+ var len = buf.length
62+ if (buf[len - 1] == 0xa)
63+ len--
64+ var line = buf.toString('ascii', 0, len)
65+ cb(null, line)
66+ })
67+ }
68+
69+ function readUpdate(abort, cb) {
70+ readPackLine(abort, function (end, line) {
71+ if (end) return cb(end)
72+ if (options.verbosity >= 2)
73+ console.error('line', line.toString('ascii'))
74+ if (!line.length) return cb(true)
75+ var args = util.split3(line.toString('ascii'))
76+ var args2 = util.split2(args[2], '\0')
77+ var caps = args2[1]
78+ if (caps && options.verbosity >= 2)
79+ console.error('update capabilities:', caps)
80+ cb(null, {
81+ old: rev(args[0]),
82+ new: rev(args[1]),
83+ name: args2[0]
84+ })
85+ })
86+ }
87+
88+ function havesWants(onEnd) {
89+ return function readWant(abort, cb) {
90+ readPackLineStr(abort, function (end, line) {
91+ if (end) return abortCb(cb, end, onEnd)
92+ if (options.verbosity >= 2)
93+ console.error('line', line)
94+ if (!line.length || line == 'done')
95+ return abortCb(cb, true, onEnd)
96+ var args = util.split3(line)
97+ var caps = args[2]
98+ if (caps && options.verbosity >= 2)
99+ console.error('want capabilities:', caps)
100+ cb(null, {
101+ type: args[0],
102+ hash: args[1],
103+ })
104+ })
105+ }
106+ }
107+
108+ b.pktLines = readPackLine
109+ b.updates = readUpdate
110+ b.wants = b.haves = havesWants
111+
112+ return b
113+}
114+
115+exports.encode = pktLineEncode
116+exports.decode = pktLineDecode

Built with git-ssb-web