index.jsView |
---|
2 | 2 | var pull = require('pull-stream') |
3 | 3 | var cat = require('pull-cat') |
4 | 4 | var buffered = require('pull-buffered') |
5 | 5 | var pack = require('./lib/pack') |
| 6 | +var pktLine = require('./lib/pkt-line') |
| 7 | +var util = require('./lib/util') |
6 | 8 | |
7 | 9 | function handleOption(options, name, value) { |
8 | 10 | switch (name) { |
9 | 11 | case 'verbosity': |
24 | 26 | 'connect', |
25 | 27 | ].join('\n') + '\n\n') |
26 | 28 | } |
27 | 29 | |
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 | | - |
41 | 30 | function optionSource(cmd, options) { |
42 | | - var args = split2(cmd) |
| 31 | + var args = util.split2(cmd) |
43 | 32 | var msg = handleOption(options, args[0], args[1]) |
44 | 33 | msg = (msg === true) ? 'ok' |
45 | 34 | : (msg === false) ? 'unsupported' |
46 | 35 | : 'error ' + msg |
69 | 58 | * agent=git/2.7.0 */ |
70 | 59 | var sendRefs = receivePackHeader([ |
71 | 60 | ], refSource, false) |
72 | 61 | |
73 | | - var lines = packLineDecode(read, options) |
| 62 | + var lines = pktLine.decode(read, options) |
74 | 63 | var readHave = lines.haves() |
75 | 64 | var acked |
76 | 65 | var commonHash |
77 | 66 | var sendPack |
78 | 67 | var earlyDisconnect |
79 | 68 | |
80 | 69 | |
81 | 70 | return cat([ |
82 | | - packLineEncode(cat([ |
| 71 | + pktLine.encode(cat([ |
83 | 72 | sendRefs, |
84 | 73 | pull.once(''), |
85 | 74 | function (abort, cb) { |
86 | 75 | if (abort) return |
142 | 131 | } |
143 | 132 | ]) |
144 | 133 | } |
145 | 134 | |
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 | | - |
162 | | - cb(end, pkt) |
163 | | - } |
164 | | - }) |
165 | | - } |
166 | | -} |
167 | | - |
168 | | -function rev(str) { |
169 | | - return str === '0000000000000000000000000000000000000000' ? null : str |
170 | | -} |
171 | | - |
172 | | - |
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 | | - |
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 | | - |
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 | | - |
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 | | - |
259 | 135 | |
260 | 136 | |
261 | 137 | function onThroughEnd(onEnd) { |
262 | 138 | return function (read) { |
275 | 151 | report-status delete-refs side-band-64k quiet atomic ofs-delta |
276 | 152 | */ |
277 | 153 | |
278 | 154 | |
279 | | - |
| 155 | + |
280 | 156 | function receivePackHeader(capabilities, refSource, usePlaceholder) { |
281 | 157 | var first = true |
282 | 158 | var ended |
283 | 159 | return function (abort, cb) { |
308 | 184 | var sendRefs = receivePackHeader([ |
309 | 185 | 'delete-refs', |
310 | 186 | ], refSource, true) |
311 | 187 | |
312 | | - return packLineEncode( |
| 188 | + return pktLine.encode( |
313 | 189 | cat([ |
314 | 190 | |
315 | 191 | sendRefs, |
316 | 192 | pull.once(''), |
317 | 193 | function (abort, cb) { |
318 | 194 | if (abort) return |
319 | 195 | |
320 | | - var lines = packLineDecode(read, options) |
| 196 | + var lines = pktLine.decode(read, options) |
321 | 197 | pull( |
322 | 198 | lines.updates, |
323 | 199 | onThroughEnd(refsDone), |
324 | 200 | refSink |
365 | 241 | progress: false |
366 | 242 | } |
367 | 243 | |
368 | 244 | function handleConnect(cmd, read) { |
369 | | - var args = split2(cmd) |
| 245 | + var args = util.split2(cmd) |
370 | 246 | switch (args[0]) { |
371 | 247 | case 'git-upload-pack': |
372 | 248 | return prepend('\n', uploadPack(read, haveObject, getObjects, refSource, |
373 | 249 | wantSink, options)) |
379 | 255 | } |
380 | 256 | } |
381 | 257 | |
382 | 258 | function handleCommand(line, read) { |
383 | | - var args = split2(line) |
| 259 | + var args = util.split2(line) |
384 | 260 | switch (args[0]) { |
385 | 261 | case 'capabilities': |
386 | 262 | return capabilitiesSource() |
387 | 263 | case 'list': |