Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General vaults review and fixes #720

Merged
merged 9 commits into from
May 20, 2024
19 changes: 13 additions & 6 deletions src/git/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ObjectIdList,
} from './types';
import type { EncryptedFS } from 'encryptedfs';
import type { PackObjectsResult } from 'isomorphic-git';
import { Buffer } from 'buffer';
import git from 'isomorphic-git';
import * as gitUtils from './utils';
Expand Down Expand Up @@ -398,12 +399,18 @@ async function* generatePackData({
objectIds: Array<ObjectId>;
chunkSize?: number;
}): AsyncGenerator<Buffer, void, void> {
const packFile = await git.packObjects({
fs: efs,
dir,
gitdir: gitDir,
oids: objectIds,
});
let packFile: PackObjectsResult;
try {
packFile = await git.packObjects({
fs: efs,
dir,
gitdir: gitDir,
oids: objectIds,
});
} catch (e) {
// Return without sending any data
return;
}
if (packFile.packfile == null) utils.never('failed to create packFile data');
let packFileBuffer = Buffer.from(packFile.packfile.buffer);
tegefaulkes marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
31 changes: 31 additions & 0 deletions src/git/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
RequestType,
} from './types';
import type { EncryptedFS } from 'encryptedfs';
import path from 'path';
import git from 'isomorphic-git';
import { requestTypes } from './types';
import * as utils from '../utils';
Expand Down Expand Up @@ -230,6 +231,35 @@ async function listObjects({
return [...commits, ...trees, ...blobs, ...tags];
}

const objectsDirName = 'objects';
const excludedDirs = ['pack', 'info'];

/**
* Walks the filesystem to list out all git objects in the objects directory
*/
async function listObjectsAll({
fs,
gitDir,
}: {
fs: EncryptedFS;
gitDir: string;
}) {
const objectsDirPath = path.join(gitDir, objectsDirName);
const objectSet: Set<string> = new Set();
const objectDirs = await fs.promises.readdir(objectsDirPath);
for (const objectDir of objectDirs) {
if (typeof objectDir !== 'string') utils.never();
if (excludedDirs.includes(objectDir)) continue;
const objectIds = await fs.promises.readdir(
path.join(objectsDirPath, objectDir),
);
for (const objectId of objectIds) {
objectSet.add(objectDir + objectId);
}
}
return [...objectSet];
}

/**
* Parses a want/has line from ref negotiation phase.
*/
Expand Down Expand Up @@ -309,6 +339,7 @@ export {
listReferencesGenerator,
referenceCapability,
listObjects,
listObjectsAll,
parseRequestLine,
isObjectId,
assertObjectId,
Expand Down
Loading