/
PackFile.js
404 lines (340 loc) · 10.3 KB
/
PackFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/** @flow */
import { Record, Map } from 'immutable';
import Dissolve from 'dissolve';
import { Inflate } from 'pako';
import varint from 'varint';
import uint8ToBuffer from 'typedarray-to-buffer';
import GitObject from './GitObject';
import type { SHA } from '../types/SHA';
/*
* Model to represent the content of a packfile.
*
* https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt
*/
const ENTRY_TYPES = {
COMMIT: 1,
TREE: 2,
BLOB: 3,
TAG: 4,
OFS_DELTA: 6,
REF_DELTA: 7
};
const TYPES_FOROBJECT = {
[ENTRY_TYPES.COMMIT]: 'commit',
[ENTRY_TYPES.TREE]: 'tree',
[ENTRY_TYPES.BLOB]: 'blob',
[ENTRY_TYPES.TAG]: 'tag'
};
const DEFAULTS: {
version: string,
objects: Map<SHA, GitObject>
} = {
version: '',
objects: new Map()
};
class PackFile extends Record(DEFAULTS) {
/*
* Get an object.
*/
getObject(sha: SHA): ?GitObject {
const { objects } = this;
return objects.get(sha);
}
/*
* Read an entire pack file from a buffer.
*/
static createFromBuffer(buffer: Buffer): ?PackFile {
const parser = PackFile.createStreamReader();
let result;
parser.on('pack', pack => {
result = pack;
});
parser.write(buffer);
return result;
}
/*
* Read a packfile from a stream of data.
*
* It emits an event "object" for each GitObject found.
* It emits an event "pack" with the complete PackFile at the end.
*/
static createStreamReader(): WritableStream {
const parser = Dissolve({
objectMode: true
});
return parsePack(parser).tap(() => {
const pack = new PackFile({
version: parser.vars.version,
objects: new Map(parser.vars.objects)
});
parser.emit('pack', pack);
});
}
}
/*
* Parse header of an object in the pack.
* It sets the vars: size and type.
*/
function parseObjectHeader(parser: Dissolve): Dissolve {
return parser.uint8be('byte').tap(() => {
const byte = parser.vars.byte;
let left = 4;
parser.vars.type = (byte >> 4) & 7;
parser.vars.size = byte & 0xf;
parser
.loop(end => {
if (!(parser.vars.byte & 0x80)) {
end();
return;
}
parser.uint8be('byte').tap(() => {
parser.vars.size |= (parser.vars.byte & 0x7f) << left;
left += 7;
});
})
.tap(() => {
parser.vars.size >>>= 0;
});
});
}
/*
* Parse inflate content for blob, commit and tree.
* Also index the content for this specific index.
*
* It sets the vars: content.
*/
function parseInflateContent(parser: Dissolve): Dissolve {
const inflator = new Inflate();
// Iterate while we found end of zip content
return parser.loop(end => {
parser.buffer('byte', 1).tap(() => {
const byte = parser.vars.byte;
const ab = new Uint8Array(1);
ab.fill(byte[0]);
inflator.push(ab);
if (inflator.ended) {
if (inflator.err) {
parser.emit('error', new Error(inflator.msg));
}
parser.vars.content = uint8ToBuffer(inflator.result);
end();
}
});
});
}
/*
* Parse a REF delta.
*/
function parseRefDelta(parser: Dissolve): Dissolve {
return parser.string('ref', 20).then(() => {
throw new Error('Not yet implemented');
});
}
/*
* Parse OFS delta's header.
*/
function parseOFSDeltaHeader(parser: Dissolve): Dissolve {
return parser.uint8be('byte').tap(() => {
const byte = parser.vars.byte;
parser.vars.rv = byte & 0x7f;
parser.loop(end => {
if (!(parser.vars.byte & 0x80)) {
end();
return;
}
parser.uint8be('byte').tap(() => {
parser.vars.rv += 1;
parser.vars.rv <<= 7;
parser.vars.rv |= parser.vars.byte & 0x7f;
});
});
});
}
/*
* Parse OFS delta, apply delta and return with vars "type" and "content".
*/
function parseOFSDelta(parser: Dissolve): Dissolve {
return (
parseOFSDeltaHeader(parser)
// Extract delta content
.tap(() => {
parseInflateContent(parser);
})
// Apply delta to content
.tap(() => {
const { objectOffset, rv } = parser.vars;
const offset = objectOffset - rv;
// Get referenced type/content
const origin = parser.packObjects[offset];
if (origin) {
// Apply delta on content
const delta = parser.vars.content;
const output = applyDelta(origin.content, delta);
// Export as variables
parser.vars.content = output;
parser.vars.type = origin.type;
} else {
throw new Error(
`Content for OFS_DELTA is not indexed: ${offset} - ${JSON.stringify(
Object.keys(parser.packObjects)
)} `
);
}
})
);
}
/*
* Parse an object entry.
*
* It sets the vars: object.
*/
function parseObject(parser: Dissolve): Dissolve {
return (
parser
.tap(() => {
parser.vars = {
objectOffset: parser.offset
};
parseObjectHeader(parser);
})
.tap(() => {
if (parser.vars.type < 5) {
parseInflateContent(parser);
} else if (parser.vars.type === ENTRY_TYPES.OFS_DELTA) {
parseOFSDelta(parser);
} else if (parser.vars.type === ENTRY_TYPES.REF_DELTA) {
parseRefDelta(parser);
} else {
parser.emit(
'error',
new Error('Invalid entry type in pack')
);
}
})
// Index content for this offset
// TODO: index content by sha for REF_DELTA
.tap(() => {
const { content, type, objectOffset } = parser.vars;
if (!type || !content) {
return;
}
// Index the object for this offset so that it can be accessed
// for delta.
parser.packObjects = parser.packObjects || {};
parser.packObjects[objectOffset] = { type, content };
if (!TYPES_FOROBJECT[type]) {
throw new Error(`Unknow type: ${type}`);
}
const object = new GitObject({
type: TYPES_FOROBJECT[type],
content
});
parser.vars.object = object;
})
);
}
/*
* Parse an entire packfile.
*
* it sets the vars "version" and "objects"
*/
function parsePack(parser: Dissolve): Dissolve {
return parser
.string('signature', 4)
.uint32be('version')
.uint32be('count')
.tap(() => {
const { signature, version, count } = parser.vars;
// Check header
if (signature !== 'PACK') {
parser.emit('error', new Error('Invalid pack signature'));
return;
}
let objectIndex = 0;
const objects: { [string]: GitObject } = {};
parser
.loop(stopLoop => {
parseObject(parser).tap(() => {
const { object } = parser.vars;
objects[object.sha] = object;
parser.emit('object', object);
objectIndex += 1;
if (objectIndex == count) {
stopLoop();
}
});
})
.tap(() => {
parser.vars = {
version,
objects
};
});
});
}
/*
* Apply delta from packfile to a buffer.
* This code is mostly based on https://github.com/chrisdickinson/git-apply-delta
*/
function applyDelta(base: Buffer, deltaContent: Buffer): Buffer {
let command;
let idx = 0;
let len = 0;
let outIdx = 0;
let delta = deltaContent;
const OFFSET_BUFFER = new Buffer(4);
const LENGTH_BUFFER = new Buffer(4);
const baseSize = varint.decode(delta);
delta = delta.slice(varint.decode.bytes);
if (baseSize !== base.length) {
throw new Error(
`Base doesn't match expected size: ${baseSize} != ${base.length}`
);
}
const outputSize = varint.decode(delta);
delta = delta.slice(varint.decode.bytes);
const output = new Buffer(outputSize);
// Apply deltas
len = delta.length;
while (idx < len) {
command = delta[idx];
idx += 1;
if (command & 0x80) {
copy();
} else {
insert();
}
}
function copy() {
OFFSET_BUFFER.writeUInt32LE(0, 0);
LENGTH_BUFFER.writeUInt32LE(0, 0);
let check = 1;
let x;
for (x = 0; x < 4; x += 1) {
if (command & check) {
OFFSET_BUFFER[3 - x] = delta[idx];
idx += 1;
}
check <<= 1;
}
for (x = 0; x < 3; x += 1) {
if (command & check) {
LENGTH_BUFFER[3 - x] = delta[idx];
idx += 1;
}
check <<= 1;
}
LENGTH_BUFFER[0] = 0;
const length = LENGTH_BUFFER.readUInt32BE(0) || 0x10000;
const offset = OFFSET_BUFFER.readUInt32BE(0);
base.copy(output, outIdx, offset, offset + length);
outIdx += length;
}
function insert() {
delta.copy(output, outIdx, idx, command + idx);
idx += command;
outIdx += command;
}
return output;
}
export default PackFile;