Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/server/routes/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ import type { CommitDiffPayload, FileDiff, WorkingDiffPayload } from '../../shar

const DIFF_ARGS = ['--no-color', '--find-renames'];

/** Max concurrent `git diff --no-index` subprocesses when diffing untracked files. */
const UNTRACKED_DIFF_CONCURRENCY = 8;

/** Runs `fn` over `items` with at most `limit` calls in flight at once. */
export async function mapWithConcurrency<T, R>(
items: T[],
limit: number,
fn: (item: T) => Promise<R>,
): Promise<R[]> {
const results = new Array<R>(items.length);
let next = 0;
async function worker() {
while (next < items.length) {
const i = next++;
results[i] = await fn(items[i]!);
}
}
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
return results;
}

export function diffRoutes(ctx: AppContext) {
const r = new Hono();

Expand Down Expand Up @@ -37,19 +58,17 @@ export function diffRoutes(ctx: AppContext) {
]);
const status = parseStatus(statusOut);
const untracked: FileDiff[] = (
await Promise.all(
status.untracked.map(async (f) => {
const out = await repo.git.read(
['diff', '--no-color', '--no-index', '--', '/dev/null', join(cwd, f.path)],
{ cwd, okCodes: [0, 1] },
);
const parsed = parseUnifiedDiff(out, 'untracked');
const fd = parsed[0];
if (!fd) return null;
fd.path = f.path;
return fd;
}),
)
await mapWithConcurrency(status.untracked, UNTRACKED_DIFF_CONCURRENCY, async (f) => {
const out = await repo.git.read(
['diff', '--no-color', '--no-index', '--', '/dev/null', join(cwd, f.path)],
{ cwd, okCodes: [0, 1] },
);
const parsed = parseUnifiedDiff(out, 'untracked');
const fd = parsed[0];
if (!fd) return null;
fd.path = f.path;
return fd;
})
).filter((f): f is FileDiff => f !== null);
return c.json({
staged: parseUnifiedDiff(stagedOut),
Expand Down
38 changes: 38 additions & 0 deletions tests/concurrency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';
import { mapWithConcurrency } from '../src/server/routes/diff.js';

describe('mapWithConcurrency', () => {
it('keeps the number of in-flight calls bounded on a large fixture', async () => {
const items = Array.from({ length: 250 }, (_, i) => i);
let inFlight = 0;
let maxInFlight = 0;

const results = await mapWithConcurrency(items, 8, async (n) => {
inFlight++;
maxInFlight = Math.max(maxInFlight, inFlight);
await new Promise((resolve) => setTimeout(resolve, 1));
inFlight--;
return n * 2;
});

expect(maxInFlight).toBeLessThanOrEqual(8);
expect(results).toEqual(items.map((n) => n * 2));
});

it('processes every item exactly once, in order', async () => {
const items = Array.from({ length: 37 }, (_, i) => i);
const seen: number[] = [];

await mapWithConcurrency(items, 5, async (n) => {
seen.push(n);
return n;
});

expect(seen.sort((a, b) => a - b)).toEqual(items);
});

it('handles an empty list and a limit larger than the item count', async () => {
expect(await mapWithConcurrency([], 8, async (n) => n)).toEqual([]);
expect(await mapWithConcurrency([1, 2], 8, async (n) => n * 10)).toEqual([10, 20]);
});
});