Commit 548ff18b7fa54b0dfe6b4e70b69367c0c1734dbf
Refactor getting wants and haves
Remove extra close callbackCharles Lehner committed on 8/1/2016, 6:40:52 AM
Parent: 4d7cb1b2be2593d1acbdfbfbab1d9c642a0017ef
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -72,17 +72,16 @@ | ||
72 | 72 | var wants = {} |
73 | 73 | var shallows = {} |
74 | 74 | var aborted |
75 | 75 | var hasWants |
76 | - var gotWants, gotHaves | |
76 | + var gotHaves | |
77 | 77 | |
78 | 78 | function readWant(abort, cb) { |
79 | 79 | if (abort) return |
80 | 80 | // read upload request (wants list) from client |
81 | 81 | readWantHave(null, function next(end, want) { |
82 | 82 | if (end || want.type == 'flush-pkt') { |
83 | - gotWants = true | |
84 | - readHave(end === true ? null : end, cb) | |
83 | + cb(end || true, cb) | |
85 | 84 | return |
86 | 85 | } |
87 | 86 | if (want.type == 'want') { |
88 | 87 | wants[want.hash] = true |
@@ -102,17 +101,17 @@ | ||
102 | 101 | // On first obj-id that we have, ACK |
103 | 102 | // If we have none, NAK. |
104 | 103 | // TODO: implement multi_ack_detailed |
105 | 104 | if (abort) return |
105 | + if (gotHaves) return cb(true) | |
106 | 106 | readWantHave(null, function next(end, have) { |
107 | 107 | if (end === true) { |
108 | 108 | gotHaves = true |
109 | 109 | if (!acked) { |
110 | 110 | cb(null, 'NAK') |
111 | 111 | } else { |
112 | 112 | cb(true) |
113 | 113 | } |
114 | - cb(true) | |
115 | 114 | } else if (have.type === 'flush-pkt') { |
116 | 115 | // found no common object |
117 | 116 | if (!acked) { |
118 | 117 | cb(null, 'NAK') |
@@ -136,37 +135,39 @@ | ||
136 | 135 | }) |
137 | 136 | }) |
138 | 137 | } |
139 | 138 | |
139 | + function readPack(abort, cb) { | |
140 | + if (abort || aborted) return console.error('abrt', abort || aborted), cb(abort || aborted) | |
141 | + if (sendPack) return sendPack(abort, cb) | |
142 | + // send pack file to client | |
143 | + if (!hasWants) return cb(true) | |
144 | + if (options.verbosity >= 2) { | |
145 | + console.error('common', commonHash, 'wants', wants) | |
146 | + } | |
147 | + getObjects(repo, commonHash, wants, shallows, | |
148 | + function (err, numObjects, readObjects) { | |
149 | + if (err) return cb(err) | |
150 | + var progress = progressObjects(options) | |
151 | + progress.setNumObjects(numObjects) | |
152 | + sendPack = pack.encode(options, numObjects, progress(readObjects)) | |
153 | + if (options.verbosity >= 1) { | |
154 | + console.error('retrieving', numObjects, 'git objects') | |
155 | + } | |
156 | + sendPack(null, cb) | |
157 | + } | |
158 | + ) | |
159 | + } | |
160 | + | |
140 | 161 | // Packfile negotiation |
141 | 162 | return cat([ |
142 | 163 | pktLine.encode(cat([ |
143 | 164 | sendRefs, |
144 | 165 | pull.once(''), |
145 | - function (abort, cb) { | |
146 | - if (!gotWants) readWant(abort, cb) | |
147 | - else if (!gotHaves) readHave(abort, cb) | |
148 | - else cb(true) | |
149 | - } | |
166 | + readWant, | |
167 | + readHave | |
150 | 168 | ])), |
151 | - | |
152 | - function (abort, cb) { | |
153 | - if (abort || aborted) return cb(abort || aborted) | |
154 | - // send pack file to client | |
155 | - if (sendPack) | |
156 | - return sendPack(abort, cb) | |
157 | - if (!hasWants) return cb(true) | |
158 | - getObjects(repo, commonHash, wants, shallows, | |
159 | - function (err, numObjects, readObjects) { | |
160 | - if (err) return cb(err) | |
161 | - var progress = progressObjects(options) | |
162 | - progress.setNumObjects(numObjects) | |
163 | - sendPack = pack.encode(options, numObjects, | |
164 | - progress(readObjects)) | |
165 | - cb(null, '') | |
166 | - } | |
167 | - ) | |
168 | - } | |
169 | + readPack | |
169 | 170 | ]) |
170 | 171 | } |
171 | 172 | |
172 | 173 | // through stream to show a progress bar for objects being read |
@@ -245,8 +246,9 @@ | ||
245 | 246 | } |
246 | 247 | |
247 | 248 | done(function (err) { |
248 | 249 | if (err) return cb(err) |
250 | + // console.error(objects.reduce(function (n, obj) { return obj.length + n}, 0) + ' bytes') | |
249 | 251 | cb(null, objects.length, pull.values(objects)) |
250 | 252 | }) |
251 | 253 | } |
252 | 254 |
Built with git-ssb-web