({
fs,
cache,
gitdir,
oid,
format = 'content',
})
| 15 | * @param {string} [args.format] |
| 16 | */ |
| 17 | export async function _readObject({ |
| 18 | fs, |
| 19 | cache, |
| 20 | gitdir, |
| 21 | oid, |
| 22 | format = 'content', |
| 23 | }) { |
| 24 | // Curry the current read method so that the packfile un-deltification |
| 25 | // process can acquire external ref-deltas. |
| 26 | const getExternalRefDelta = oid => _readObject({ fs, cache, gitdir, oid }) |
| 27 | |
| 28 | let result |
| 29 | // Empty tree - hard-coded so we can use it as a shorthand. |
| 30 | // Note: I think the canonical git implementation must do this too because |
| 31 | // `git cat-file -t 4b825dc642cb6eb9a060e54bf8d69288fbee4904` prints "tree" even in empty repos. |
| 32 | if (oid === '4b825dc642cb6eb9a060e54bf8d69288fbee4904') { |
| 33 | result = { format: 'wrapped', object: Buffer.from(`tree 0\x00`) } |
| 34 | } |
| 35 | // Look for it in the loose object directory. |
| 36 | if (!result) { |
| 37 | result = await readObjectLoose({ fs, gitdir, oid }) |
| 38 | } |
| 39 | // Check to see if it's in a packfile. |
| 40 | if (!result) { |
| 41 | result = await readObjectPacked({ |
| 42 | fs, |
| 43 | cache, |
| 44 | gitdir, |
| 45 | oid, |
| 46 | getExternalRefDelta, |
| 47 | }) |
| 48 | |
| 49 | if (!result) { |
| 50 | throw new NotFoundError(oid) |
| 51 | } |
| 52 | |
| 53 | // Directly return packed result, as specified: packed objects always return the 'content' format. |
| 54 | return result |
| 55 | } |
| 56 | |
| 57 | // Loose objects are always deflated, return early |
| 58 | if (format === 'deflated') { |
| 59 | return result |
| 60 | } |
| 61 | |
| 62 | // All loose objects are deflated but the hard-coded empty tree is `wrapped` so we have to check if we need to inflate the object. |
| 63 | if (result.format === 'deflated') { |
| 64 | result.object = Buffer.from(await inflate(result.object)) |
| 65 | result.format = 'wrapped' |
| 66 | } |
| 67 | |
| 68 | if (format === 'wrapped') { |
| 69 | return result |
| 70 | } |
| 71 | |
| 72 | const sha = await shasum(result.object) |
| 73 | if (sha !== oid) { |
| 74 | throw new InternalError( |
no test coverage detected
searching dependent graphs…