Skip to content

Commit fa0f9d2

Browse files
limondisomorphic-git-bot
authored andcommitted
fix: readObject when reading from packed objects with format wrapped (#1935)
* fix: return packed object result early in readObject.js * chore: simplify conditional logic in readObject.js * chore: add tests for reading different format objects from packfile * docs: add @limond as a contributor
1 parent 0231054 commit fa0f9d2

File tree

6 files changed

+54
-47
lines changed

6 files changed

+54
-47
lines changed

js/isomorphic-git/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
374374
</tr>
375375
<tr>
376376
<td align="center"><a href="https://github.com/lsegurado"><img src="https://avatars.githubusercontent.com/u/27731047?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Lucas Martin Segurado</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=lsegurado" title="Documentation">📖</a> <a href="https://github.com/isomorphic-git/isomorphic-git/issues?q=author%3Alsegurado" title="Bug reports">🐛</a></td>
377+
<td align="center"><a href="https://github.com/limond"><img src="https://avatars.githubusercontent.com/u/1025682?v=4?s=60" width="60px;" alt=""/><br /><sub><b>Leon Kaucher</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=limond" title="Code">💻</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=limond" title="Tests">⚠️</a></td>
377378
</tr>
378379
</table>
379380

js/isomorphic-git/index.cjs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,43 +3137,46 @@ async function _readObject({
31373137
oid,
31383138
getExternalRefDelta,
31393139
});
3140-
}
3141-
// Finally
3142-
if (!result) {
3143-
throw new NotFoundError(oid)
3140+
3141+
if (!result) {
3142+
throw new NotFoundError(oid)
3143+
}
3144+
3145+
// Directly return packed result, as specified: packed objects always return the 'content' format.
3146+
return result
31443147
}
31453148

3149+
// Loose objects are always deflated, return early
31463150
if (format === 'deflated') {
31473151
return result
31483152
}
31493153

3154+
// 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.
31503155
if (result.format === 'deflated') {
31513156
result.object = Buffer.from(await inflate(result.object));
31523157
result.format = 'wrapped';
31533158
}
31543159

3155-
if (result.format === 'wrapped') {
3156-
if (format === 'wrapped' && result.format === 'wrapped') {
3157-
return result
3158-
}
3159-
const sha = await shasum(result.object);
3160-
if (sha !== oid) {
3161-
throw new InternalError(
3162-
`SHA check failed! Expected ${oid}, computed ${sha}`
3163-
)
3164-
}
3165-
const { object, type } = GitObject.unwrap(result.object);
3166-
result.type = type;
3167-
result.object = object;
3168-
result.format = 'content';
3160+
if (format === 'wrapped') {
3161+
return result
31693162
}
31703163

3171-
if (result.format === 'content') {
3172-
if (format === 'content') return result
3173-
return
3164+
const sha = await shasum(result.object);
3165+
if (sha !== oid) {
3166+
throw new InternalError(
3167+
`SHA check failed! Expected ${oid}, computed ${sha}`
3168+
)
3169+
}
3170+
const { object, type } = GitObject.unwrap(result.object);
3171+
result.type = type;
3172+
result.object = object;
3173+
result.format = 'content';
3174+
3175+
if (format === 'content') {
3176+
return result
31743177
}
31753178

3176-
throw new InternalError(`invalid format "${result.format}"`)
3179+
throw new InternalError(`invalid requested format "${format}"`)
31773180
}
31783181

31793182
class AlreadyExistsError extends BaseError {

js/isomorphic-git/index.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,43 +3131,46 @@ async function _readObject({
31313131
oid,
31323132
getExternalRefDelta,
31333133
});
3134-
}
3135-
// Finally
3136-
if (!result) {
3137-
throw new NotFoundError(oid)
3134+
3135+
if (!result) {
3136+
throw new NotFoundError(oid)
3137+
}
3138+
3139+
// Directly return packed result, as specified: packed objects always return the 'content' format.
3140+
return result
31383141
}
31393142

3143+
// Loose objects are always deflated, return early
31403144
if (format === 'deflated') {
31413145
return result
31423146
}
31433147

3148+
// 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.
31443149
if (result.format === 'deflated') {
31453150
result.object = Buffer.from(await inflate(result.object));
31463151
result.format = 'wrapped';
31473152
}
31483153

3149-
if (result.format === 'wrapped') {
3150-
if (format === 'wrapped' && result.format === 'wrapped') {
3151-
return result
3152-
}
3153-
const sha = await shasum(result.object);
3154-
if (sha !== oid) {
3155-
throw new InternalError(
3156-
`SHA check failed! Expected ${oid}, computed ${sha}`
3157-
)
3158-
}
3159-
const { object, type } = GitObject.unwrap(result.object);
3160-
result.type = type;
3161-
result.object = object;
3162-
result.format = 'content';
3154+
if (format === 'wrapped') {
3155+
return result
31633156
}
31643157

3165-
if (result.format === 'content') {
3166-
if (format === 'content') return result
3167-
return
3158+
const sha = await shasum(result.object);
3159+
if (sha !== oid) {
3160+
throw new InternalError(
3161+
`SHA check failed! Expected ${oid}, computed ${sha}`
3162+
)
3163+
}
3164+
const { object, type } = GitObject.unwrap(result.object);
3165+
result.type = type;
3166+
result.object = object;
3167+
result.format = 'content';
3168+
3169+
if (format === 'content') {
3170+
return result
31683171
}
31693172

3170-
throw new InternalError(`invalid format "${result.format}"`)
3173+
throw new InternalError(`invalid requested format "${format}"`)
31713174
}
31723175

31733176
class AlreadyExistsError extends BaseError {

js/isomorphic-git/index.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/isomorphic-git/index.umd.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/isomorphic-git/size_report.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)