Skip to content

Commit 941eb98

Browse files
committed
Fix readdir() returns unsorted entries related issues
1 parent df97acf commit 941eb98

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## next
2+
3+
- Fixed methods for reading loose objects (`readObjectHeaderByOid`, `readObjectHeaderByHash`, `readObjectByOid`
4+
and `readObjectByHash`) that were not working in some cases due to incorrect formation of the fanout table when `fs.readdir()` returns an unsorted list of entries
5+
- Fixed `listRemotes()` method to always return a sorted list of remotes
6+
17
## 0.1.0 (2022-09-12)
28

39
- Initial release

src/loose-object-index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ async function createLooseObjectMap(gitdir: string): Promise<LooseObjectMap> {
3030
)
3131
);
3232

33-
return new Map(objectDirs.flat());
33+
return new Map(objectDirs.flat().sort(([a], [b]) => (a < b ? -1 : 1)));
3434
}
3535

36-
function indexObjectNames(names: string[]) {
36+
function indexObjectNames(sortedNames: string[]) {
3737
const fanoutTable = new Array<[start: number, end: number]>(256);
38-
const sortedNames = [...names].sort(([a], [b]) => (a < b ? -1 : 1));
39-
const binaryNames = Buffer.from(names.join(''), 'hex');
38+
const binaryNames = Buffer.from(sortedNames.join(''), 'hex');
4039

4140
for (let i = 0, offset = 0; i < 256; i++) {
4241
const prevOffset = offset;

src/resolve-ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ async function readRemotes(gitdir: string) {
256256
}
257257
}
258258

259-
return remotes;
259+
return remotes.sort();
260260
}
261261

262262
async function readLooseRefs(gitdir: string, remotes: string[]) {

test/helpers/fixture.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ export const fixtures = Object.create(null);
99

1010
for (const name of fixtureNames) {
1111
fixtures[name] = {
12-
repo: (options?: GitReaderOptions) =>
13-
createGitReader(
14-
fileURLToPath(new URL(fixturesPath + name + '/_git', import.meta.url)),
15-
options
16-
),
12+
repo: (options?: GitReaderOptions) => {
13+
const origReaddir = fs.promises.readdir;
14+
15+
try {
16+
// Patch readdir method to catch issues when readdir returns unsorted entries
17+
// @ts-expect-error ts(2322)
18+
fs.promises.readdir = (...args: Parameters<typeof fs.promises.readdir>) =>
19+
origReaddir(...args).then((list) => list.reverse());
20+
21+
return createGitReader(
22+
fileURLToPath(new URL(fixturesPath + name + '/_git', import.meta.url)),
23+
options
24+
);
25+
} finally {
26+
fs.promises.readdir = origReaddir;
27+
}
28+
},
1729
data: JSON.parse(
1830
fs.readFileSync(
1931
fileURLToPath(new URL(fixturesPath + name + '/data.json', import.meta.url)),

0 commit comments

Comments
 (0)