mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-26 11:12:53 +00:00
Speed up cold diffs and wrapped text measurement (#4676)
* perf(app): optimize wrapped measurements and checkpoint native layout experiment Compute full grapheme geometry only for final wrap fragments. Preserve shaped-text search behavior while bounding additive wrap probes. Keep the Android background layout experiment isolated from the normal diff renderer; native renderer integration and iOS bindings remain unfinished. * perf(server): reduce cold diff generation work Keep active diff reads owned across invalidation and wait for the initial watcher inventory before cold generation. Batch patch and blob reads while preserving per-file size isolation, bounded output, and daemon responsiveness. Avoid constructing fallback tokens when full-file highlighting already covers both sides. * test(server): exercise oversized highlighted diff rejection The single-line fixture now bypasses highlighting under the long-line guard, so it no longer reaches the structured payload limit. Use short dense lines to keep testing oversized rejection and continued socket usability. * fix(server): queue fresh diffs for forced reads A forced request following a repository mutation must not reuse an older active build. Invalidate that result and coalesce waiting callers onto one subsequent build. Give the cache a named object input and completed-value type. * test(server): share deferred diff loader setup * test(server): name completed diff cache setup * refactor(server): use aliases for new diff imports * fix(server): keep shipped diff imports resolvable by Node The TypeScript build preserves path aliases without a runtime resolver. Restore relative imports for shipped code and declarations, and correct the server import guideline that prompted the regression.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import { expect, test } from "vitest";
|
||||
import { CheckoutDiffCache } from "@server/server/checkout-diff-cache.js";
|
||||
|
||||
function createSequencedLoader<T>(first: Promise<T>, second: Promise<T>) {
|
||||
let calls = 0;
|
||||
return {
|
||||
load: () => (++calls === 1 ? first : second),
|
||||
get calls() {
|
||||
return calls;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function fillCompletedCache(cache: CheckoutDiffCache) {
|
||||
for (let i = 0; i < 70; i++) {
|
||||
await cache.read({
|
||||
cwd: String(i),
|
||||
compare: { mode: "base" },
|
||||
load: async () => ({ diff: "" }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test("forced reads after a mutation share a fresh build after the active build finishes", async () => {
|
||||
const cache = new CheckoutDiffCache(() => 0);
|
||||
const first = Promise.withResolvers<{ diff: string }>();
|
||||
const second = Promise.withResolvers<{ diff: string }>();
|
||||
const loader = createSequencedLoader(first.promise, second.promise);
|
||||
const { load } = loader;
|
||||
const pending = cache.read({ cwd: "repo", compare: { mode: "base" }, load });
|
||||
await Promise.resolve();
|
||||
const options = { force: true, reason: "repository-mutation" };
|
||||
const fresh = cache.read({ cwd: "repo", compare: { mode: "base" }, ...options, load });
|
||||
const another = cache.read({ cwd: "repo", compare: { mode: "base" }, ...options, load });
|
||||
expect(loader.calls).toBe(1);
|
||||
first.resolve({ diff: "before-mutation" });
|
||||
expect(await pending).toEqual({ diff: "before-mutation" });
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
expect(loader.calls).toBe(2);
|
||||
second.resolve({ diff: "after-mutation" });
|
||||
expect(await fresh).toEqual({ diff: "after-mutation" });
|
||||
expect(await another).toEqual({ diff: "after-mutation" });
|
||||
expect(await cache.read({ cwd: "repo", compare: { mode: "base" }, load })).toEqual({
|
||||
diff: "after-mutation",
|
||||
});
|
||||
expect(loader.calls).toBe(2);
|
||||
});
|
||||
|
||||
test("reads after invalidation wait for fresh work without overlapping the original read", async () => {
|
||||
const cache = new CheckoutDiffCache(() => 0);
|
||||
const first = Promise.withResolvers<{ diff: string }>();
|
||||
const second = Promise.withResolvers<{ diff: string }>();
|
||||
const loader = createSequencedLoader(first.promise, second.promise);
|
||||
const { load } = loader;
|
||||
const read = () => cache.read({ cwd: "repo", compare: { mode: "base" }, load });
|
||||
const pending = read();
|
||||
await Promise.resolve();
|
||||
cache.invalidate("repo", "base");
|
||||
cache.invalidate("repo", "base");
|
||||
const fresh = read();
|
||||
const another = read();
|
||||
expect(loader.calls).toBe(1);
|
||||
first.resolve({ diff: "old" });
|
||||
expect(await pending).toEqual({ diff: "old" });
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
expect(loader.calls).toBe(2);
|
||||
second.resolve({ diff: "new" });
|
||||
expect(await fresh).toEqual({ diff: "new" });
|
||||
expect(await another).toEqual({ diff: "new" });
|
||||
expect(await read()).toEqual({ diff: "new" });
|
||||
expect(loader.calls).toBe(2);
|
||||
});
|
||||
|
||||
test("edits during a read do not delay its caller or cache its outdated snapshot", async () => {
|
||||
const cache = new CheckoutDiffCache(() => 0);
|
||||
const deferred = Promise.withResolvers<{ diff: string }>();
|
||||
let calls = 0;
|
||||
const load = () => {
|
||||
calls += 1;
|
||||
return deferred.promise;
|
||||
};
|
||||
const pending = cache.read({ cwd: "repo", compare: { mode: "uncommitted" }, load });
|
||||
await Promise.resolve();
|
||||
cache.invalidate("repo", "uncommitted");
|
||||
deferred.resolve({ diff: "snapshot" });
|
||||
expect(await pending).toEqual({ diff: "snapshot" });
|
||||
expect(calls).toBe(1);
|
||||
expect(
|
||||
await cache.read({
|
||||
cwd: "repo",
|
||||
compare: { mode: "uncommitted" },
|
||||
load: async () => ({ diff: "fresh" }),
|
||||
}),
|
||||
).toEqual({ diff: "fresh" });
|
||||
});
|
||||
|
||||
test("active reads survive completed-payload eviction and failed reads can retry", async () => {
|
||||
const cache = new CheckoutDiffCache(() => 0);
|
||||
const deferred = Promise.withResolvers<{ diff: string }>();
|
||||
const load = () => deferred.promise;
|
||||
const first = cache.read({ cwd: "active", compare: { mode: "uncommitted" }, load });
|
||||
const rejected = expect(first).rejects.toThrow("read failed");
|
||||
await fillCompletedCache(cache);
|
||||
expect(cache.read({ cwd: "active", compare: { mode: "uncommitted" }, load })).toBe(first);
|
||||
deferred.reject(new Error("read failed"));
|
||||
await rejected;
|
||||
expect(
|
||||
await cache.read({
|
||||
cwd: "active",
|
||||
compare: { mode: "uncommitted" },
|
||||
load: async () => ({ diff: "retry" }),
|
||||
}),
|
||||
).toEqual({ diff: "retry" });
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { LRUCache } from "lru-cache";
|
||||
import type { CheckoutDiffCompare, CheckoutDiffResult } from "../utils/checkout-git.js";
|
||||
|
||||
interface ActiveRead {
|
||||
promise: Promise<CheckoutDiffResult>;
|
||||
invalidated: boolean;
|
||||
}
|
||||
|
||||
interface CachedDiff {
|
||||
value: CheckoutDiffResult;
|
||||
loadedAt: number;
|
||||
lastReadStarted: number;
|
||||
}
|
||||
|
||||
interface CheckoutDiffReadInput {
|
||||
cwd: string;
|
||||
compare: CheckoutDiffCompare;
|
||||
force?: boolean;
|
||||
reason?: string;
|
||||
load: () => Promise<CheckoutDiffResult>;
|
||||
}
|
||||
|
||||
/** Completed payloads are bounded; active work survives invalidation and eviction. */
|
||||
export class CheckoutDiffCache {
|
||||
private readonly values = new LRUCache<string, CachedDiff>({
|
||||
max: 64,
|
||||
});
|
||||
private readonly active = new Map<string, ActiveRead>();
|
||||
|
||||
constructor(private readonly now: () => number) {}
|
||||
|
||||
read(input: CheckoutDiffReadInput): Promise<CheckoutDiffResult> {
|
||||
const { cwd, compare, force, reason, load } = input;
|
||||
if (force && !reason) throw new Error("WorkspaceGitService forced read requires a reason");
|
||||
const key = JSON.stringify([
|
||||
cwd,
|
||||
compare.mode,
|
||||
compare.mode === "base" ? (compare.baseRef ?? null) : null,
|
||||
compare.ignoreWhitespace === true,
|
||||
compare.includeStructured === true,
|
||||
]);
|
||||
const cached = this.values.get(key);
|
||||
const now = this.now();
|
||||
if (
|
||||
!force &&
|
||||
cached &&
|
||||
(now - cached.loadedAt <= 15_000 || now - cached.lastReadStarted < 2_000)
|
||||
)
|
||||
return Promise.resolve(cached.value);
|
||||
const pending = this.active.get(key);
|
||||
if (pending) {
|
||||
if (force) {
|
||||
pending.invalidated = true;
|
||||
this.values.delete(key);
|
||||
}
|
||||
if (!pending.invalidated) return pending.promise;
|
||||
// A caller arriving after a change needs a fresh read, once the old one
|
||||
// finishes. The original caller can still receive its snapshot promptly.
|
||||
// The older result cannot enter the cache. Drop force when retrying so
|
||||
// callers waiting on that same result share the next fresh build.
|
||||
const reload = () => this.read({ ...input, force: false });
|
||||
return pending.promise.then(reload, reload);
|
||||
}
|
||||
if (cached) cached.lastReadStarted = now;
|
||||
|
||||
// Defer load until ownership is installed, including synchronous invalidations.
|
||||
const read: ActiveRead = {
|
||||
invalidated: false,
|
||||
promise: Promise.resolve()
|
||||
.then(load)
|
||||
.then((value) => {
|
||||
// Never repopulate the cache with a snapshot invalidated during its build.
|
||||
if (!read.invalidated)
|
||||
this.values.set(key, { value, loadedAt: this.now(), lastReadStarted: now });
|
||||
return value;
|
||||
})
|
||||
.finally(() => this.active.delete(key)),
|
||||
};
|
||||
this.active.set(key, read);
|
||||
return read.promise;
|
||||
}
|
||||
|
||||
invalidate(cwd: string, mode: CheckoutDiffCompare["mode"]): void {
|
||||
for (const key of new Set([...this.values.keys(), ...this.active.keys()])) {
|
||||
const [cachedCwd, cachedMode] = JSON.parse(key) as string[];
|
||||
if (cachedCwd !== cwd || cachedMode !== mode) continue;
|
||||
this.values.delete(key);
|
||||
const pending = this.active.get(key);
|
||||
if (pending) pending.invalidated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,7 +146,9 @@ test("keeps the socket usable after rejecting an oversized structured diff", asy
|
||||
initGitRepo(cwd);
|
||||
commitFile(cwd, "large-a.js", "const value = 0;\n");
|
||||
commitFile(cwd, "large-b.js", "const value = 0;\n");
|
||||
const denseExpression = `const value = ${"a+".repeat(450_000)}a;\n`;
|
||||
// Short lines stay eligible for highlighting; a giant single line is skipped
|
||||
// before token expansion and cannot exercise the structured-payload limit.
|
||||
const denseExpression = `const value = ${"a+".repeat(100)}a;\n`.repeat(4_000);
|
||||
writeFileSync(path.join(cwd, "large-a.js"), denseExpression);
|
||||
writeFileSync(path.join(cwd, "large-b.js"), denseExpression);
|
||||
|
||||
|
||||
@@ -332,30 +332,30 @@ export async function highlightDiffWithFileContent(
|
||||
return file;
|
||||
}
|
||||
|
||||
const reconstructedTokens = buildReconstructedTokenLookups(file);
|
||||
let newTokensByLine = reconstructedTokens.newTokensByLine;
|
||||
let oldTokensByLine = reconstructedTokens.oldTokensByLine;
|
||||
|
||||
if (typeof options.oldFileContent === "string") {
|
||||
oldTokensByLine =
|
||||
buildFullFileTokenLookup(options.oldFileContent, file.path) ?? oldTokensByLine;
|
||||
}
|
||||
|
||||
let newTokensByLine: Map<number, HighlightToken[]> | null = null;
|
||||
const oldTokensByLine =
|
||||
typeof options.oldFileContent === "string"
|
||||
? buildFullFileTokenLookup(options.oldFileContent, file.path)
|
||||
: null;
|
||||
if (typeof options.newFileContent === "string") {
|
||||
newTokensByLine =
|
||||
buildFullFileTokenLookup(options.newFileContent, file.path) ?? newTokensByLine;
|
||||
newTokensByLine = buildFullFileTokenLookup(options.newFileContent, file.path);
|
||||
} else {
|
||||
try {
|
||||
const fileContent = await readFile(resolve(cwd, file.path), "utf-8");
|
||||
newTokensByLine = buildFullFileTokenLookup(fileContent, file.path);
|
||||
} catch {
|
||||
// Deleted or unavailable files use reconstructed tokens below.
|
||||
}
|
||||
}
|
||||
|
||||
if (newTokensByLine && oldTokensByLine)
|
||||
return applyTokensToHunks(file, newTokensByLine, oldTokensByLine);
|
||||
}
|
||||
|
||||
const filePath = resolve(cwd, file.path);
|
||||
try {
|
||||
const fileContent = await readFile(filePath, "utf-8");
|
||||
newTokensByLine = buildFullFileTokenLookup(fileContent, file.path) ?? newTokensByLine;
|
||||
} catch {
|
||||
// If file read fails (deleted file, etc.), fall back to reconstructed new-side tokens.
|
||||
}
|
||||
|
||||
return applyTokensToHunks(file, newTokensByLine, oldTokensByLine);
|
||||
const reconstructedTokens = buildReconstructedTokenLookups(file);
|
||||
return applyTokensToHunks(
|
||||
file,
|
||||
newTokensByLine ?? reconstructedTokens.newTokensByLine,
|
||||
oldTokensByLine ?? reconstructedTokens.oldTokensByLine,
|
||||
);
|
||||
}
|
||||
|
||||
function applyTokensToHunks(
|
||||
|
||||
@@ -216,6 +216,35 @@ describe("WorkspaceGitService checkout observation", () => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
test("waits for the initial watcher inventory before building a cold diff", async () => {
|
||||
const watcher = createWatcherHarness();
|
||||
const inventoryFinished = createDeferred<void>();
|
||||
let builds = 0;
|
||||
const service = createService(watcher, {
|
||||
subscribe: async (...args: Parameters<typeof watcher.subscribe>) => {
|
||||
await inventoryFinished.promise;
|
||||
return watcher.subscribe(...args);
|
||||
},
|
||||
getCheckoutDiff: async () => {
|
||||
builds += 1;
|
||||
return { diff: "", structured: [] };
|
||||
},
|
||||
});
|
||||
const subscription = service.registerWorkspace({ cwd: REPO_CWD }, () => {});
|
||||
try {
|
||||
const read = service.getCheckoutDiff(REPO_CWD, { mode: "uncommitted" });
|
||||
await flushPromises();
|
||||
expect(builds).toBe(0);
|
||||
inventoryFinished.resolve();
|
||||
await read;
|
||||
expect(builds).toBe(1);
|
||||
} finally {
|
||||
inventoryFinished.resolve();
|
||||
subscription.unsubscribe();
|
||||
await service.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
test("dispose waits for file observation to finish closing", async () => {
|
||||
const watcher = createWatcherHarness();
|
||||
const closeFinished = createDeferred<void>();
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { basename, join, resolve } from "node:path";
|
||||
import { LRUCache } from "lru-cache";
|
||||
import { CheckoutDiffCache } from "./checkout-diff-cache.js";
|
||||
import pLimit from "p-limit";
|
||||
import type pino from "pino";
|
||||
import type { ProjectCheckoutLitePayload } from "@getpaseo/protocol/messages";
|
||||
@@ -94,8 +95,6 @@ const WATCH_RECOVERY_MAX_ATTEMPTS = 3;
|
||||
const WORKSPACE_GIT_AUXILIARY_READ_TTL_MS = 15_000;
|
||||
// Non-forced refresh triggers share this minimum gap to absorb watcher/self-heal bursts; force bypasses it.
|
||||
const WORKSPACE_GIT_INTERNAL_MIN_GAP_MS = 2_000;
|
||||
// Heavy values (multi-MB highlighted diffs); cap aggressively. Ephemeral worktree cwds would otherwise pile up forever.
|
||||
const WORKSPACE_GIT_CHECKOUT_DIFF_CACHE_MAX = 64;
|
||||
// Small values (booleans, short strings, small arrays); generous cap.
|
||||
const WORKSPACE_GIT_AUXILIARY_CACHE_MAX = 256;
|
||||
|
||||
@@ -566,10 +565,7 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
string,
|
||||
WorkspaceGitAuxiliaryReadCacheEntry<string>
|
||||
>({ max: WORKSPACE_GIT_AUXILIARY_CACHE_MAX });
|
||||
private readonly checkoutDiffCache = new LRUCache<
|
||||
string,
|
||||
WorkspaceGitAuxiliaryReadCacheEntry<CheckoutDiffResult>
|
||||
>({ max: WORKSPACE_GIT_CHECKOUT_DIFF_CACHE_MAX });
|
||||
private readonly checkoutDiffCache = new CheckoutDiffCache(() => this.deps.now().getTime());
|
||||
private watcherErrorCallbackCount = 0;
|
||||
constructor(options: WorkspaceGitServiceOptions) {
|
||||
this.logger = options.logger.child({ module: "workspace-git-service" });
|
||||
@@ -726,7 +722,7 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
return this.workspaceTargets.get(cwd)?.latestSnapshot ?? null;
|
||||
}
|
||||
|
||||
getCheckoutDiff(
|
||||
async getCheckoutDiff(
|
||||
cwd: string,
|
||||
options: CheckoutDiffCompare,
|
||||
readOptions?: WorkspaceGitReadOptions,
|
||||
@@ -734,13 +730,20 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
this.assertNotDisposed();
|
||||
const normalizedCwd = resolve(cwd);
|
||||
const normalizedOptions = this.normalizeCheckoutDiffOptions(options);
|
||||
const key = this.buildCheckoutDiffCacheKey(normalizedCwd, normalizedOptions);
|
||||
return this.readAuxiliaryCache(this.checkoutDiffCache, key, readOptions, () =>
|
||||
this.deps.getCheckoutDiff(normalizedCwd, normalizedOptions, {
|
||||
paseoHome: this.paseoHome,
|
||||
worktreesRoot: this.worktreesRoot,
|
||||
}),
|
||||
);
|
||||
// Initial inventory events cover the watcher setup gap. Process them before
|
||||
// starting a cold diff, rather than invalidating that build halfway through.
|
||||
await this.workspaceTargets.get(normalizedCwd)?.observationSetupPromise;
|
||||
this.assertNotDisposed();
|
||||
return this.checkoutDiffCache.read({
|
||||
cwd: normalizedCwd,
|
||||
compare: normalizedOptions,
|
||||
...readOptions,
|
||||
load: () =>
|
||||
this.deps.getCheckoutDiff(normalizedCwd, normalizedOptions, {
|
||||
paseoHome: this.paseoHome,
|
||||
worktreesRoot: this.worktreesRoot,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
private normalizeCheckoutDiffOptions(options: CheckoutDiffCompare): CheckoutDiffCompare {
|
||||
@@ -754,26 +757,8 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
};
|
||||
}
|
||||
|
||||
private buildCheckoutDiffCacheKey(cwd: string, options: CheckoutDiffCompare): string {
|
||||
// Diff content varies by compare signature. Keep the cache per exact diff read shape so
|
||||
// hot diff panes coalesce while base refs and rendering options never share stale patches.
|
||||
return JSON.stringify([
|
||||
"checkout-diff",
|
||||
cwd,
|
||||
options.mode,
|
||||
options.mode === "base" ? (options.baseRef ?? null) : null,
|
||||
options.ignoreWhitespace === true,
|
||||
options.includeStructured === true,
|
||||
]);
|
||||
}
|
||||
|
||||
private invalidateCheckoutDiffCache(cwd: string, mode: CheckoutDiffCompare["mode"]): void {
|
||||
for (const key of this.checkoutDiffCache.keys()) {
|
||||
const [kind, cachedCwd, cachedMode] = JSON.parse(key) as unknown[];
|
||||
if (kind === "checkout-diff" && cachedCwd === cwd && cachedMode === mode) {
|
||||
this.checkoutDiffCache.delete(key);
|
||||
}
|
||||
}
|
||||
this.checkoutDiffCache.invalidate(cwd, mode);
|
||||
}
|
||||
|
||||
validateBranchRef(
|
||||
|
||||
@@ -2,39 +2,8 @@ import { execFileSync } from "node:child_process";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync, realpathSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { beforeEach, afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const spawnCounters = vi.hoisted(() => ({
|
||||
trackedTextDiffCalls: 0,
|
||||
}));
|
||||
|
||||
vi.mock("child_process", async () => {
|
||||
const actual = await vi.importActual<typeof import("child_process")>("child_process");
|
||||
return {
|
||||
...actual,
|
||||
spawn: (...args: Parameters<typeof actual.spawn>) => {
|
||||
const [command, commandArgs] = args;
|
||||
if (command === "git" && Array.isArray(commandArgs)) {
|
||||
const normalizedArgs = commandArgs.map((arg) => String(arg));
|
||||
// `runGitCommand` always prepends its two config overrides; skip them
|
||||
// to find the actual git subcommand.
|
||||
const subcommandIndex =
|
||||
normalizedArgs[0] === "-c" && normalizedArgs[1] === "core.quotepath=false" ? 4 : 0;
|
||||
const isTrackedTextDiff =
|
||||
normalizedArgs[subcommandIndex] === "diff" &&
|
||||
normalizedArgs.includes("HEAD") &&
|
||||
!normalizedArgs.includes("--numstat") &&
|
||||
!normalizedArgs.includes("--no-index") &&
|
||||
!normalizedArgs.includes("--shortstat") &&
|
||||
!normalizedArgs.includes("--name-status");
|
||||
if (isTrackedTextDiff) {
|
||||
spawnCounters.trackedTextDiffCalls += 1;
|
||||
}
|
||||
}
|
||||
return actual.spawn(...args);
|
||||
},
|
||||
};
|
||||
});
|
||||
import { beforeEach, afterEach, describe, expect, it } from "vitest";
|
||||
import { startGitCommandMetrics, stopGitCommandMetrics } from "@server/utils/run-git-command.js";
|
||||
|
||||
import { getCheckoutDiff } from "./checkout-git.js";
|
||||
|
||||
@@ -70,14 +39,14 @@ describe("checkout git diff batching", () => {
|
||||
const setup = initRepoWithTrackedChanges(20);
|
||||
tempDir = setup.tempDir;
|
||||
repoDir = setup.repoDir;
|
||||
spawnCounters.trackedTextDiffCalls = 0;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("uses per-file tracked git diff commands for tracked file diffs", async () => {
|
||||
it("collects ordinary tracked patches with one bounded Git command", async () => {
|
||||
startGitCommandMetrics();
|
||||
const result = await getCheckoutDiff(repoDir, {
|
||||
mode: "uncommitted",
|
||||
includeStructured: false,
|
||||
@@ -85,6 +54,44 @@ describe("checkout git diff batching", () => {
|
||||
|
||||
expect(result.diff).toContain("file-0.txt");
|
||||
expect(result.diff).toContain("file-19.txt");
|
||||
expect(spawnCounters.trackedTextDiffCalls).toBe(20);
|
||||
const metrics = stopGitCommandMetrics();
|
||||
expect(
|
||||
metrics.commands.filter(({ args }) => args[0] === "diff" && args.includes("--")),
|
||||
).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("batches committed file contents and preserves the highlighted diff", async () => {
|
||||
execFileSync("git", ["checkout", "-b", "feature"], { cwd: repoDir });
|
||||
execFileSync("git", ["add", "."], { cwd: repoDir });
|
||||
execFileSync("git", ["-c", "commit.gpgsign=false", "commit", "-m", "change"], { cwd: repoDir });
|
||||
startGitCommandMetrics();
|
||||
const result = await getCheckoutDiff(repoDir, {
|
||||
mode: "base",
|
||||
baseRef: "main",
|
||||
includeStructured: true,
|
||||
});
|
||||
const metrics = stopGitCommandMetrics();
|
||||
expect(result.structured).toHaveLength(20);
|
||||
expect(result.structured?.every((file) => file.additions === 1 && file.deletions === 1)).toBe(
|
||||
true,
|
||||
);
|
||||
expect(metrics.commands.filter(({ args }) => args[0] === "show")).toHaveLength(0);
|
||||
expect(metrics.commands.filter(({ args }) => args[0] === "cat-file")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("keeps small neighbors when a patch exceeds the entire batch budget", async () => {
|
||||
writeFileSync(join(repoDir, "file-0.txt"), "x".repeat(9 * 1024 * 1024) + "\n");
|
||||
const result = await getCheckoutDiff(repoDir, {
|
||||
mode: "uncommitted",
|
||||
includeStructured: true,
|
||||
});
|
||||
expect(result.structured).toHaveLength(20);
|
||||
expect(result.structured?.find((file) => file.path === "file-0.txt")).toMatchObject({
|
||||
status: "too_large",
|
||||
hunks: [],
|
||||
});
|
||||
expect(result.structured?.filter((file) => file.status === "ok")).toHaveLength(19);
|
||||
expect(result.diff).toContain("+after-19");
|
||||
expect(result.diff).not.toContain("x".repeat(1_000));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1507,8 +1507,7 @@ const x = 1;
|
||||
expect(diff.diff).toContain(`-export const value = "old";`);
|
||||
expect(diff.diff).toContain(`+export const value = "new";`);
|
||||
expect(commands).toContain("diff --numstat HEAD");
|
||||
expect(commands).toContain("diff HEAD -- generated.js");
|
||||
expect(commands).toContain("diff HEAD -- small.ts");
|
||||
expect(commands).toContain("diff HEAD -- :(literal)generated.js :(literal)small.ts");
|
||||
expect(metrics.maxConcurrent).toBeLessThanOrEqual(8);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { resolve, dirname, basename } from "path";
|
||||
import { existsSync, realpathSync } from "fs";
|
||||
import { open as openFile, readFile, stat as statFile } from "fs/promises";
|
||||
import { setImmediate } from "node:timers/promises";
|
||||
import { TTLCache } from "@isaacs/ttlcache";
|
||||
import type { CheckoutCommit, CheckoutCommitFile } from "@getpaseo/protocol/messages";
|
||||
import { parseGitHubRemoteIdentity, parseGitRemoteLocation } from "@getpaseo/protocol/git-remote";
|
||||
@@ -29,6 +30,7 @@ import {
|
||||
} from "../services/forge-cli-command.js";
|
||||
import { parseGitRevParsePath, resolveGitRevParsePath } from "./git-rev-parse-path.js";
|
||||
import { runGitCommand, type RunGitCommand } from "./run-git-command.js";
|
||||
import { readGitFileContents } from "./git-file-contents.js";
|
||||
import { isPaseoOwnedWorktreeCwd, resolvePaseoWorktreesBaseRoot } from "./worktree.js";
|
||||
import {
|
||||
branchNameFromRef,
|
||||
@@ -639,7 +641,7 @@ function buildGitDiffArgs(args: { ignoreWhitespace?: boolean; extra: string[] })
|
||||
}
|
||||
|
||||
const TRACKED_DIFF_NUMSTAT_MAX_BYTES = 2 * 1024 * 1024; // 2MB
|
||||
const TRACKED_DIFF_BATCH_SIZE = 8;
|
||||
const TRACKED_DIFF_BATCH_SIZE = 64;
|
||||
const EMPTY_TREE_OBJECT_ID = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
|
||||
|
||||
function isUnbornHeadDiffError(error: unknown): boolean {
|
||||
@@ -716,7 +718,7 @@ async function getTrackedDiffTextForPath(input: {
|
||||
const result = await runGitCommand(
|
||||
buildGitDiffArgs({
|
||||
ignoreWhitespace: input.ignoreWhitespace,
|
||||
extra: [...getCheckoutDiffRefArgs(input.refsForDiff), "--", input.path],
|
||||
extra: [...getCheckoutDiffRefArgs(input.refsForDiff), "--", `:(literal)${input.path}`],
|
||||
}),
|
||||
{
|
||||
cwd: input.cwd,
|
||||
@@ -732,6 +734,56 @@ async function getTrackedDiffTextForPath(input: {
|
||||
};
|
||||
}
|
||||
|
||||
async function getTrackedDiffTexts(input: {
|
||||
cwd: string;
|
||||
refsForDiff: CheckoutDiffRefs;
|
||||
paths: string[];
|
||||
ignoreWhitespace: boolean;
|
||||
}): Promise<Array<{ path: string; text: string; truncated: boolean }>> {
|
||||
if (input.paths.length === 0) return [];
|
||||
if (input.paths.length === 1)
|
||||
return [await getTrackedDiffTextForPath({ ...input, path: input.paths[0] })];
|
||||
const result = await runGitCommand(
|
||||
buildGitDiffArgs({
|
||||
ignoreWhitespace: input.ignoreWhitespace,
|
||||
extra: [
|
||||
...getCheckoutDiffRefArgs(input.refsForDiff),
|
||||
"--",
|
||||
...input.paths.map((path) => `:(literal)${path}`),
|
||||
],
|
||||
}),
|
||||
{ cwd: input.cwd, envOverlay: READ_ONLY_GIT_ENV, maxOutputBytes: 8 * PER_FILE_DIFF_MAX_BYTES },
|
||||
);
|
||||
const files = new Map<string, { path: string; text: string; truncated: boolean }>();
|
||||
if (!result.truncated) {
|
||||
for (const text of result.stdout.split(/(?=^diff --git )/m).filter(Boolean)) {
|
||||
// Only identify the file here. Oversized bodies must not reach the parser.
|
||||
const hunkStart = text.indexOf("\n@@ ");
|
||||
const file = parseDiff(hunkStart < 0 ? text : text.slice(0, hunkStart))[0];
|
||||
if (!file || !input.paths.includes(file.path)) break;
|
||||
files.set(file.path, {
|
||||
path: file.path,
|
||||
text,
|
||||
truncated: Buffer.byteLength(text) > PER_FILE_DIFF_MAX_BYTES,
|
||||
});
|
||||
}
|
||||
// Empty patches are valid when whitespace is ignored. Every emitted section
|
||||
// must be accounted for before accepting a combined command.
|
||||
if (
|
||||
[...files.values()].reduce((length, file) => length + file.text.length, 0) ===
|
||||
result.stdout.length
|
||||
) {
|
||||
return input.paths.map((path) => files.get(path) ?? { path, text: "", truncated: false });
|
||||
}
|
||||
}
|
||||
// Bound oversized output without letting one large file hide its neighbors.
|
||||
const middle = Math.ceil(input.paths.length / 2);
|
||||
return [
|
||||
...(await getTrackedDiffTexts({ ...input, paths: input.paths.slice(0, middle) })),
|
||||
...(await getTrackedDiffTexts({ ...input, paths: input.paths.slice(middle) })),
|
||||
];
|
||||
}
|
||||
|
||||
export class NotGitRepoError extends Error {
|
||||
readonly cwd: string;
|
||||
readonly code = "NOT_GIT_REPO";
|
||||
@@ -2978,13 +3030,23 @@ async function buildHighlightedTrackedDiffFile(input: {
|
||||
change: CheckoutFileChange;
|
||||
parsedFile: ParsedDiffFile;
|
||||
refsForDiff: CheckoutDiffRefs;
|
||||
contents?: Map<string, string | null>;
|
||||
}): Promise<ParsedDiffFile> {
|
||||
const { cwd, change, parsedFile, refsForDiff } = input;
|
||||
const refPath = change.oldPath ?? change.path;
|
||||
const [oldFileContent, newFileContent] = await Promise.all([
|
||||
change.isNew ? null : readGitFileContentAtRef(cwd, refsForDiff.baseRef, refPath),
|
||||
refsForDiff.targetRef ? readGitFileContentAtRef(cwd, refsForDiff.targetRef, change.path) : null,
|
||||
]);
|
||||
const [oldFileContent, newFileContent] = input.contents
|
||||
? [
|
||||
change.isNew ? null : input.contents.get(`${refsForDiff.baseRef}:${refPath}`),
|
||||
refsForDiff.targetRef
|
||||
? input.contents.get(`${refsForDiff.targetRef}:${change.path}`)
|
||||
: null,
|
||||
]
|
||||
: await Promise.all([
|
||||
change.isNew ? null : readGitFileContentAtRef(cwd, refsForDiff.baseRef, refPath),
|
||||
refsForDiff.targetRef
|
||||
? readGitFileContentAtRef(cwd, refsForDiff.targetRef, change.path)
|
||||
: null,
|
||||
]);
|
||||
const highlightedFile = await highlightDiffWithFileContent(parsedFile, cwd, {
|
||||
oldFileContent,
|
||||
newFileContent,
|
||||
@@ -3012,6 +3074,19 @@ function isWhitespaceOnlyTrackedChange(input: {
|
||||
);
|
||||
}
|
||||
|
||||
function readTrackedDiffContents(
|
||||
cwd: string,
|
||||
changes: CheckoutFileChange[],
|
||||
refs: CheckoutDiffRefs,
|
||||
): Promise<Map<string, string | null> | null> {
|
||||
const specs: string[] = [];
|
||||
for (const change of changes) {
|
||||
if (!change.isNew) specs.push(`${refs.baseRef}:${change.oldPath ?? change.path}`);
|
||||
if (refs.targetRef) specs.push(`${refs.targetRef}:${change.path}`);
|
||||
}
|
||||
return readGitFileContents(cwd, specs);
|
||||
}
|
||||
|
||||
async function appendStructuredTrackedDiffs(
|
||||
input: AppendStructuredTrackedDiffsInput,
|
||||
): Promise<boolean> {
|
||||
@@ -3030,54 +3105,68 @@ async function appendStructuredTrackedDiffs(
|
||||
const parsedTrackedFiles = trackedDiffText.length > 0 ? parseDiff(trackedDiffText) : [];
|
||||
const parsedTrackedByPath = new Map(parsedTrackedFiles.map((file) => [file.path, file]));
|
||||
|
||||
for (const change of trackedChanges) {
|
||||
const placeholder = trackedPlaceholderByPath.get(change.path);
|
||||
if (placeholder) {
|
||||
const file = buildPlaceholderParsedDiffFile(change, {
|
||||
status: placeholder.status,
|
||||
stat: placeholder.stat,
|
||||
});
|
||||
for (let start = 0; start < trackedChanges.length; start += TRACKED_DIFF_BATCH_SIZE) {
|
||||
const changes = trackedChanges.slice(start, start + TRACKED_DIFF_BATCH_SIZE);
|
||||
const contents = await readTrackedDiffContents(
|
||||
cwd,
|
||||
changes.filter(
|
||||
(change) =>
|
||||
!trackedPlaceholderByPath.has(change.path) && parsedTrackedByPath.has(change.path),
|
||||
),
|
||||
refsForDiff,
|
||||
);
|
||||
for (const change of changes) {
|
||||
const placeholder = trackedPlaceholderByPath.get(change.path);
|
||||
if (placeholder) {
|
||||
const file = buildPlaceholderParsedDiffFile(change, {
|
||||
status: placeholder.status,
|
||||
stat: placeholder.stat,
|
||||
});
|
||||
if (!appendStructuredFile(structured, file)) {
|
||||
return false;
|
||||
}
|
||||
appendTrackedPlaceholderComment(change, placeholder.status);
|
||||
continue;
|
||||
}
|
||||
|
||||
const stat = trackedNumstatByPath.get(change.path) ?? null;
|
||||
const parsedFile = parsedTrackedByPath.get(change.path);
|
||||
if (parsedFile) {
|
||||
const file = await buildHighlightedTrackedDiffFile({
|
||||
cwd,
|
||||
change,
|
||||
parsedFile,
|
||||
refsForDiff,
|
||||
contents: contents ?? undefined,
|
||||
});
|
||||
if (!appendStructuredFile(structured, file)) {
|
||||
return false;
|
||||
}
|
||||
// Batched blobs remove the per-file I/O pause; keep serving other daemon work.
|
||||
await setImmediate();
|
||||
continue;
|
||||
}
|
||||
|
||||
// `git diff -w --name-status` can still report a modified path even when the
|
||||
// whitespace-filtered patch and numstat are both empty. Skip emitting a
|
||||
// structured placeholder in that case so whitespace-only edits truly disappear.
|
||||
if (isWhitespaceOnlyTrackedChange({ change, stat, ignoreWhitespace })) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const file = {
|
||||
path: change.path,
|
||||
...(change.oldPath ? { oldPath: change.oldPath } : {}),
|
||||
isNew: change.isNew,
|
||||
isDeleted: change.isDeleted,
|
||||
additions: stat?.additions ?? 0,
|
||||
deletions: stat?.deletions ?? 0,
|
||||
hunks: [],
|
||||
status: "ok",
|
||||
} satisfies ParsedDiffFile;
|
||||
if (!appendStructuredFile(structured, file)) {
|
||||
return false;
|
||||
}
|
||||
appendTrackedPlaceholderComment(change, placeholder.status);
|
||||
continue;
|
||||
}
|
||||
|
||||
const stat = trackedNumstatByPath.get(change.path) ?? null;
|
||||
const parsedFile = parsedTrackedByPath.get(change.path);
|
||||
if (parsedFile) {
|
||||
const file = await buildHighlightedTrackedDiffFile({
|
||||
cwd,
|
||||
change,
|
||||
parsedFile,
|
||||
refsForDiff,
|
||||
});
|
||||
if (!appendStructuredFile(structured, file)) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// `git diff -w --name-status` can still report a modified path even when the
|
||||
// whitespace-filtered patch and numstat are both empty. Skip emitting a
|
||||
// structured placeholder in that case so whitespace-only edits truly disappear.
|
||||
if (isWhitespaceOnlyTrackedChange({ change, stat, ignoreWhitespace })) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const file = {
|
||||
path: change.path,
|
||||
...(change.oldPath ? { oldPath: change.oldPath } : {}),
|
||||
isNew: change.isNew,
|
||||
isDeleted: change.isDeleted,
|
||||
additions: stat?.additions ?? 0,
|
||||
deletions: stat?.deletions ?? 0,
|
||||
hunks: [],
|
||||
status: "ok",
|
||||
} satisfies ParsedDiffFile;
|
||||
if (!appendStructuredFile(structured, file)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3196,18 +3285,32 @@ async function processTrackedChanges(
|
||||
|
||||
let trackedDiffText = "";
|
||||
let trackedDiffBytes = 0;
|
||||
const renamedPaths = new Set(
|
||||
trackedChanges.filter((change) => change.oldPath).map((change) => change.path),
|
||||
);
|
||||
for (let start = 0; start < trackedDiffPaths.length; start += TRACKED_DIFF_BATCH_SIZE) {
|
||||
const paths = trackedDiffPaths.slice(start, start + TRACKED_DIFF_BATCH_SIZE);
|
||||
const trackedDiffs = await Promise.all(
|
||||
paths.map((path) =>
|
||||
getTrackedDiffTextForPath({
|
||||
cwd,
|
||||
refsForDiff,
|
||||
path,
|
||||
ignoreWhitespace,
|
||||
}),
|
||||
),
|
||||
// Keep rename/copy path filtering identical to the existing per-file command.
|
||||
const ordinary = await getTrackedDiffTexts({
|
||||
cwd,
|
||||
refsForDiff,
|
||||
paths: paths.filter((path) => !renamedPaths.has(path)),
|
||||
ignoreWhitespace,
|
||||
});
|
||||
const renamed = await Promise.all(
|
||||
paths
|
||||
.filter((path) => renamedPaths.has(path))
|
||||
.map((path) =>
|
||||
getTrackedDiffTextForPath({
|
||||
cwd,
|
||||
refsForDiff,
|
||||
path,
|
||||
ignoreWhitespace,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const byPath = new Map([...ordinary, ...renamed].map((file) => [file.path, file]));
|
||||
const trackedDiffs = paths.map((path) => byPath.get(path)!);
|
||||
|
||||
for (const fileDiff of trackedDiffs) {
|
||||
if (fileDiff.truncated) {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, expect, test } from "vitest";
|
||||
import { readGitFileContents } from "@server/utils/git-file-contents.js";
|
||||
import { runGitCommand, runGitCommandBytes } from "@server/utils/run-git-command.js";
|
||||
|
||||
let cwd: string;
|
||||
beforeEach(() => {
|
||||
cwd = mkdtempSync(join(tmpdir(), "git-contents-"));
|
||||
execFileSync("git", ["init", "-q"], { cwd });
|
||||
});
|
||||
afterEach(() => rmSync(cwd, { recursive: true, force: true }));
|
||||
|
||||
test("batch byte framing survives Unicode, invalid UTF-8, and missing objects", async () => {
|
||||
const text = Buffer.concat([Buffer.from("中文👩💻\n"), Buffer.from([0xff]), Buffer.from("\n")]);
|
||||
const { stdout: oid } = await runGitCommand(["hash-object", "-w", "--stdin"], {
|
||||
cwd,
|
||||
input: text,
|
||||
});
|
||||
const ref = oid.trim();
|
||||
const empty = execFileSync("git", ["hash-object", "-w", "--stdin"], { cwd, input: "" })
|
||||
.toString()
|
||||
.trim();
|
||||
const contents = await readGitFileContents(cwd, [ref, "HEAD:missing", empty, ref]);
|
||||
expect(contents).toEqual(
|
||||
new Map([
|
||||
[ref, text.toString("utf8")],
|
||||
["HEAD:missing", null],
|
||||
[empty, ""],
|
||||
]),
|
||||
);
|
||||
const raw = await runGitCommandBytes(["cat-file", "blob", ref], { cwd });
|
||||
expect(raw.stdout.equals(text)).toBe(true);
|
||||
});
|
||||
|
||||
test("oversized output stays bounded and requests the existing per-file path", async () => {
|
||||
writeFileSync(join(cwd, "big"), Buffer.alloc(20 * 1024 * 1024, 97));
|
||||
const oid = execFileSync("git", ["hash-object", "-w", "big"], { cwd }).toString().trim();
|
||||
expect(await readGitFileContents(cwd, [oid])).toBeNull();
|
||||
const limited = await runGitCommandBytes(["cat-file", "--batch"], {
|
||||
cwd,
|
||||
input: oid + "\n",
|
||||
maxOutputBytes: 100,
|
||||
});
|
||||
expect(limited.truncated).toBe(true);
|
||||
expect(limited.stdout.length).toBe(100);
|
||||
});
|
||||
|
||||
test("newline paths do not become extra batch requests", async () => {
|
||||
expect(await readGitFileContents(cwd, ["HEAD:with\nnewline"])).toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { runGitCommandBytes } from "./run-git-command.js";
|
||||
|
||||
// Match the existing git-show ceiling. Oversized batches use bounded per-file reads.
|
||||
const MAX_CONTENT_BYTES = 20 * 1024 * 1024;
|
||||
|
||||
/** Null means the batch cannot preserve per-file read semantics within its budget. */
|
||||
export async function readGitFileContents(
|
||||
cwd: string,
|
||||
specs: string[],
|
||||
): Promise<Map<string, string | null> | null> {
|
||||
const unique = [...new Set(specs)];
|
||||
if (unique.length === 0) return new Map();
|
||||
// The line-based batch protocol cannot represent embedded newlines.
|
||||
if (unique.some((spec) => spec.includes("\n"))) return null;
|
||||
const result = await runGitCommandBytes(["cat-file", "--batch"], {
|
||||
cwd,
|
||||
input: unique.join("\n") + "\n",
|
||||
envOverlay: { GIT_OPTIONAL_LOCKS: "0", LC_ALL: "C" },
|
||||
maxOutputBytes: MAX_CONTENT_BYTES,
|
||||
});
|
||||
if (result.truncated) return null;
|
||||
const contents = new Map<string, string | null>();
|
||||
let offset = 0;
|
||||
for (const spec of unique) {
|
||||
const end = result.stdout.indexOf(10, offset);
|
||||
if (end < 0) throw new Error("Incomplete git object header");
|
||||
const header = result.stdout.subarray(offset, end).toString("utf8");
|
||||
offset = end + 1;
|
||||
if (header === `${spec} missing`) {
|
||||
contents.set(spec, null);
|
||||
continue;
|
||||
}
|
||||
const match = /^[a-f0-9]+ blob (\d+)$/.exec(header);
|
||||
// Submodules and other non-blob objects retain git-show behavior.
|
||||
if (!match) return null;
|
||||
const size = Number(match[1]);
|
||||
if (
|
||||
!Number.isSafeInteger(size) ||
|
||||
offset + size >= result.stdout.length ||
|
||||
result.stdout[offset + size] !== 10
|
||||
)
|
||||
throw new Error("Invalid git object length");
|
||||
contents.set(spec, result.stdout.subarray(offset, offset + size).toString("utf8"));
|
||||
offset += size + 1;
|
||||
}
|
||||
if (offset !== result.stdout.length) throw new Error("Unexpected git object output");
|
||||
return contents;
|
||||
}
|
||||
@@ -46,6 +46,7 @@ export function configureGitProcessPolicy(policy: GitProcessPolicy): void {
|
||||
|
||||
export interface GitCommandOptions {
|
||||
cwd: string;
|
||||
input?: string | Buffer;
|
||||
env?: ProcessEnvRecord;
|
||||
envOverlay?: ProcessEnvRecord;
|
||||
logger?: Pick<Logger, "trace">;
|
||||
@@ -54,8 +55,8 @@ export interface GitCommandOptions {
|
||||
acceptExitCodes?: number[];
|
||||
}
|
||||
|
||||
export interface GitCommandResult {
|
||||
stdout: string;
|
||||
export interface GitCommandResult<Output = string> {
|
||||
stdout: Output;
|
||||
stderr: string;
|
||||
truncated: boolean;
|
||||
exitCode: number | null;
|
||||
@@ -263,6 +264,23 @@ function runGitCommandWithProvenance(
|
||||
options: GitCommandOptions,
|
||||
provenance?: string,
|
||||
): Promise<GitCommandResult> {
|
||||
return executeGitCommand(args, options, (output) => output.toString("utf8"), provenance);
|
||||
}
|
||||
|
||||
/** Binary stdout preserves cat-file byte framing before text decoding. */
|
||||
export function runGitCommandBytes(
|
||||
args: string[],
|
||||
options: GitCommandOptions,
|
||||
): Promise<GitCommandResult<Buffer>> {
|
||||
return executeGitCommand(args, options, (output) => output);
|
||||
}
|
||||
|
||||
function executeGitCommand<Output>(
|
||||
args: string[],
|
||||
options: GitCommandOptions,
|
||||
decode: (output: Buffer) => Output,
|
||||
provenance?: string,
|
||||
): Promise<GitCommandResult<Output>> {
|
||||
const metricsState = submitGitCommandMetric(args, options.cwd);
|
||||
const commandTrace = submitGitCommandTrace(args, options.cwd, {
|
||||
active: gitProcessScheduler.activeCount,
|
||||
@@ -274,7 +292,7 @@ function runGitCommandWithProvenance(
|
||||
const exited = new Promise<void>((resolve) => {
|
||||
releaseProcessSlot = resolve;
|
||||
});
|
||||
const resultPromise = new Promise<GitCommandResult>((resolve, reject) => {
|
||||
const resultPromise = new Promise<GitCommandResult<Output>>((resolve, reject) => {
|
||||
startGitCommandTrace(commandTrace, {
|
||||
active: gitProcessScheduler.activeCount,
|
||||
pending: gitProcessScheduler.pendingCount,
|
||||
@@ -385,7 +403,7 @@ function runGitCommandWithProvenance(
|
||||
cwd: options.cwd,
|
||||
envOverlay,
|
||||
shell: false,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
stdio: [options.input === undefined ? "ignore" : "pipe", "pipe", "pipe"],
|
||||
},
|
||||
);
|
||||
spawnGitCommandTrace(commandTrace, child.pid);
|
||||
@@ -465,12 +483,23 @@ function runGitCommandWithProvenance(
|
||||
}
|
||||
});
|
||||
|
||||
if (options.input !== undefined) {
|
||||
child.stdin!.on("error", (error: NodeJS.ErrnoException) => {
|
||||
// A command may close stdin when it exits or reaches the output limit.
|
||||
if (error.code !== "EPIPE") {
|
||||
processError = error;
|
||||
child.kill("SIGKILL");
|
||||
}
|
||||
});
|
||||
child.stdin!.end(options.input);
|
||||
}
|
||||
|
||||
child.on("exit", markProcessExited);
|
||||
|
||||
child.on("close", (exitCode, signal) => {
|
||||
markProcessExited(exitCode, signal);
|
||||
const result: GitCommandResult = {
|
||||
stdout: Buffer.concat(stdoutChunks).toString("utf8"),
|
||||
const result: GitCommandResult<Output> = {
|
||||
stdout: decode(Buffer.concat(stdoutChunks)),
|
||||
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
||||
truncated,
|
||||
exitCode,
|
||||
|
||||
Reference in New Issue
Block a user