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
31 changes: 23 additions & 8 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,14 +399,28 @@ async function* generatePackData({
objectIds: Array<ObjectId>;
chunkSize?: number;
}): AsyncGenerator<Buffer, void, void> {
const packFile = await git.packObjects({
fs: efs,
dir,
gitdir: gitDir,
oids: objectIds,
});
if (packFile.packfile == null) utils.never('failed to create packFile data');
let packFileBuffer = Buffer.from(packFile.packfile.buffer);
let packFile: PackObjectsResult;
// In case of errors we don't want to throw them. This will result in the error being thrown into `isometric-git`
// when it consumes the response. It handles this by logging out the error which we don't want to happen.
try {
packFile = await git.packObjects({
fs: efs,
dir,
gitdir: gitDir,
oids: objectIds,
});
} catch {
// Return without sending any data
return;
}
// Pack file will only be undefined if it was written to disk instead
if (packFile.packfile == null) return;
// Convert to a buffer without copy, so we can process it
let packFileBuffer = Buffer.from(
packFile.packfile.buffer,
0,
packFile.packfile.byteLength,
);

// Streaming the packFile as chunks of the length specified by the `chunkSize`.
// Each line is formatted as a `PKT-LINE`
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