mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
Read a checkout's identity for reconciliation without scanning its working tree
This commit is contained in:
@@ -134,6 +134,36 @@ test("a fresh checkout read makes a newly initialized repository available to ca
|
||||
expect(realpathSync.native(repoRoot)).toBe(realpathSync.native(repo));
|
||||
}, 30_000);
|
||||
|
||||
test("a checkout read for reconciliation never scans the working tree", async () => {
|
||||
const root = realpathSync(mkdtempSync(path.join(tmpdir(), "paseo-checkout-read-")));
|
||||
const repo = path.join(root, "repo");
|
||||
mkdirSync(repo);
|
||||
const git = (...args: string[]) =>
|
||||
execFileSync("git", ["-c", "commit.gpgsign=false", ...args], { cwd: repo, stdio: "pipe" });
|
||||
git("init", "-b", "main");
|
||||
git("config", "user.email", "test@example.com");
|
||||
git("config", "user.name", "Test");
|
||||
writeFileSync(path.join(repo, "README.md"), "base\n");
|
||||
git("add", ".");
|
||||
git("commit", "-m", "base");
|
||||
const service = new WorkspaceGitServiceImpl({
|
||||
logger: pino({ enabled: false }),
|
||||
paseoHome: path.join(root, "home"),
|
||||
});
|
||||
cleanup.push(async () => {
|
||||
await service.dispose();
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
startGitCommandMetrics();
|
||||
const checkout = await service.getCheckout(repo, { priority: "background" });
|
||||
const verbs = stopGitCommandMetrics().commands.map((command) => command.args[0]);
|
||||
|
||||
expect(checkout).toMatchObject({ isGit: true, currentBranch: "main" });
|
||||
expect(verbs.length).toBeGreaterThan(0);
|
||||
expect(verbs.filter((verb) => ["status", "ls-files", "rev-list"].includes(verb))).toEqual([]);
|
||||
}, 30_000);
|
||||
|
||||
test("recursive observation updates tracked state and prunes ignored storms", async () => {
|
||||
const tempDir = realpathSync(mkdtempSync(path.join(tmpdir(), "paseo-git-observation-")));
|
||||
const repoDir = path.join(tempDir, "repo");
|
||||
|
||||
@@ -427,7 +427,7 @@ describe("WorkspaceGitServiceImpl primitive refresh entrypoint", () => {
|
||||
|
||||
test("getCheckout surfaces an unexpected Git read failure", async () => {
|
||||
const service = createService({
|
||||
getCheckoutStatus: vi.fn(async () => {
|
||||
getCheckoutIdentity: vi.fn(async () => {
|
||||
throw new Error("Git read failed");
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
getCheckoutRefDerivedState,
|
||||
getCheckoutSnapshotFacts,
|
||||
getCheckoutShortstat,
|
||||
getCheckoutIdentity,
|
||||
getCheckoutStatus,
|
||||
getCheckoutWorktreeState,
|
||||
getPullRequestStatus,
|
||||
@@ -407,6 +408,7 @@ interface WorkspaceGitServiceDependencies {
|
||||
subscribe: SubscribeToFileChanges;
|
||||
getCheckoutSnapshotFacts: typeof getCheckoutSnapshotFacts;
|
||||
getCheckoutRefDerivedState: typeof getCheckoutRefDerivedState;
|
||||
getCheckoutIdentity: typeof getCheckoutIdentity;
|
||||
getCheckoutStatus: typeof getCheckoutStatus;
|
||||
getCheckoutShortstat: typeof getCheckoutShortstat;
|
||||
getCheckoutWorktreeState: typeof getCheckoutWorktreeState;
|
||||
@@ -624,6 +626,7 @@ function buildDefaultWorkspaceGitServiceDeps(
|
||||
subscribe,
|
||||
getCheckoutSnapshotFacts,
|
||||
getCheckoutRefDerivedState,
|
||||
getCheckoutIdentity,
|
||||
getCheckoutStatus,
|
||||
getCheckoutShortstat,
|
||||
getCheckoutWorktreeState,
|
||||
@@ -857,7 +860,7 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
): Promise<ProjectCheckoutLitePayload> {
|
||||
this.assertNotDisposed();
|
||||
const normalizedCwd = resolve(cwd);
|
||||
const status = await this.deps.getCheckoutStatus(normalizedCwd, {
|
||||
const identity = await this.deps.getCheckoutIdentity(normalizedCwd, {
|
||||
paseoHome: this.paseoHome,
|
||||
worktreesRoot: this.worktreesRoot,
|
||||
logger: this.logger,
|
||||
@@ -866,7 +869,7 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
: {}),
|
||||
});
|
||||
const cached = this.getCachedSnapshot(normalizedCwd);
|
||||
if (cached && cached.git.isGit !== status.isGit) {
|
||||
if (cached && cached.git.isGit !== identity.isGit) {
|
||||
// A fresh checkout read discovered a repository transition. Keep the
|
||||
// shared snapshot current before exposing the new project capability.
|
||||
await this.getSnapshot(normalizedCwd, {
|
||||
@@ -875,7 +878,7 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
reason: "checkout-classification-changed",
|
||||
});
|
||||
}
|
||||
if (!status.isGit) {
|
||||
if (!identity.isGit) {
|
||||
return checkoutLiteFromGitSnapshot(normalizedCwd, {
|
||||
isGit: false,
|
||||
currentBranch: null,
|
||||
@@ -887,11 +890,11 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
|
||||
}
|
||||
return checkoutLiteFromGitSnapshot(normalizedCwd, {
|
||||
isGit: true,
|
||||
currentBranch: status.currentBranch,
|
||||
remoteUrl: status.remoteUrl,
|
||||
repoRoot: status.repoRoot,
|
||||
isPaseoOwnedWorktree: status.isPaseoOwnedWorktree,
|
||||
mainRepoRoot: status.mainRepoRoot,
|
||||
currentBranch: identity.currentBranch,
|
||||
remoteUrl: identity.remoteUrl,
|
||||
repoRoot: identity.repoRoot,
|
||||
isPaseoOwnedWorktree: identity.isPaseoOwnedWorktree,
|
||||
mainRepoRoot: identity.mainRepoRoot,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { getCheckoutIdentity, getCheckoutStatus } from "./checkout-git.js";
|
||||
import { createWorktree } from "./worktree.js";
|
||||
import { startGitCommandMetrics, stopGitCommandMetrics } from "./run-git-command.js";
|
||||
|
||||
function git(cwd: string, ...args: string[]): void {
|
||||
execFileSync("git", ["-c", "commit.gpgsign=false", ...args], { cwd, stdio: "ignore" });
|
||||
}
|
||||
|
||||
describe("getCheckoutIdentity", () => {
|
||||
let tempDir: string;
|
||||
let paseoHome: string;
|
||||
let repoDir: string;
|
||||
let linkedDir: string;
|
||||
let plainDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = realpathSync(mkdtempSync(join(tmpdir(), "checkout-identity-")));
|
||||
paseoHome = join(tempDir, "paseo-home");
|
||||
repoDir = join(tempDir, "repo");
|
||||
linkedDir = join(tempDir, "linked");
|
||||
plainDir = join(tempDir, "plain");
|
||||
mkdirSync(paseoHome);
|
||||
mkdirSync(repoDir);
|
||||
mkdirSync(plainDir);
|
||||
git(repoDir, "init", "-b", "main");
|
||||
git(repoDir, "config", "user.email", "test@test.com");
|
||||
git(repoDir, "config", "user.name", "Test");
|
||||
writeFileSync(join(repoDir, "tracked.txt"), "tracked\n");
|
||||
git(repoDir, "add", ".");
|
||||
git(repoDir, "commit", "-m", "initial");
|
||||
git(repoDir, "remote", "add", "origin", "https://github.com/acme/widgets.git");
|
||||
git(repoDir, "worktree", "add", "-b", "feature", linkedDir);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("reports the same identity as the full status for a checkout, linked and Paseo worktrees and a plain folder", async () => {
|
||||
const paseoWorktree = await createWorktree({
|
||||
cwd: repoDir,
|
||||
worktreeSlug: "alpha",
|
||||
source: { kind: "branch-off", baseBranch: "main", branchName: "alpha" },
|
||||
paseoHome,
|
||||
});
|
||||
for (const cwd of [repoDir, linkedDir, paseoWorktree.worktreePath]) {
|
||||
const status = await getCheckoutStatus(cwd, { paseoHome });
|
||||
const identity = await getCheckoutIdentity(cwd, { paseoHome });
|
||||
if (!status.isGit) throw new Error(`${cwd} should be a git checkout`);
|
||||
expect(identity).toEqual({
|
||||
isGit: true,
|
||||
repoRoot: status.repoRoot,
|
||||
mainRepoRoot: status.mainRepoRoot,
|
||||
currentBranch: status.currentBranch,
|
||||
remoteUrl: status.remoteUrl,
|
||||
isPaseoOwnedWorktree: status.isPaseoOwnedWorktree,
|
||||
});
|
||||
}
|
||||
expect(await getCheckoutIdentity(linkedDir, { paseoHome })).toMatchObject({
|
||||
currentBranch: "feature",
|
||||
mainRepoRoot: repoDir,
|
||||
});
|
||||
expect(await getCheckoutIdentity(paseoWorktree.worktreePath, { paseoHome })).toMatchObject({
|
||||
isPaseoOwnedWorktree: true,
|
||||
mainRepoRoot: repoDir,
|
||||
});
|
||||
expect(await getCheckoutIdentity(plainDir, { paseoHome })).toEqual({ isGit: false });
|
||||
});
|
||||
|
||||
it("never scans the working tree or compares commits", async () => {
|
||||
startGitCommandMetrics();
|
||||
await getCheckoutIdentity(repoDir, { paseoHome });
|
||||
await getCheckoutIdentity(linkedDir, { paseoHome });
|
||||
const verbs = stopGitCommandMetrics().commands.map((command) => command.args[0]);
|
||||
|
||||
expect(verbs.length).toBeGreaterThan(0);
|
||||
expect(
|
||||
verbs.filter((verb) => ["status", "ls-files", "rev-list", "diff"].includes(verb)),
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -2406,6 +2406,60 @@ async function getUntrackedDiffText(
|
||||
};
|
||||
}
|
||||
|
||||
export type CheckoutIdentity =
|
||||
| { isGit: false }
|
||||
| {
|
||||
isGit: true;
|
||||
repoRoot: string;
|
||||
mainRepoRoot: string | null;
|
||||
currentBranch: string | null;
|
||||
remoteUrl: string | null;
|
||||
isPaseoOwnedWorktree: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* What a checkout is, without the state of its files. Reports the same identity fields as
|
||||
* `getCheckoutStatus` but never scans the working tree (`status`), compares against a base, or
|
||||
* counts commits, so reading many checkouts in the background stays cheap.
|
||||
*/
|
||||
export async function getCheckoutIdentity(
|
||||
cwd: string,
|
||||
context?: CheckoutContext,
|
||||
): Promise<CheckoutIdentity> {
|
||||
const inspected = await inspectCheckoutContext(cwd, context);
|
||||
if (!inspected) {
|
||||
return { isGit: false };
|
||||
}
|
||||
const { worktreeRoot, currentBranch, remoteUrl, paseoWorktree } = inspected;
|
||||
const mainRepoRoot = await getMainRepoRootFromCommonDir(
|
||||
cwd,
|
||||
inspected.gitCommonDir,
|
||||
context,
|
||||
).catch(() => null);
|
||||
// `getCheckoutStatus` reports a worktree as Paseo-owned only when it also resolves a base.
|
||||
const isPaseoOwnedWorktree =
|
||||
paseoWorktree.isPaseoOwnedWorktree &&
|
||||
Boolean(
|
||||
storedBaseRefFromMetadata(await readPaseoWorktreeMetadata(worktreeRoot)) ??
|
||||
(await resolveBaseRef(cwd, context)),
|
||||
);
|
||||
const isLinkedCheckout = mainRepoRoot !== null && resolve(mainRepoRoot) !== resolve(worktreeRoot);
|
||||
let reportedMainRepoRoot: string | null = null;
|
||||
if (isPaseoOwnedWorktree) {
|
||||
reportedMainRepoRoot = mainRepoRoot ?? worktreeRoot;
|
||||
} else if (isLinkedCheckout) {
|
||||
reportedMainRepoRoot = mainRepoRoot;
|
||||
}
|
||||
return {
|
||||
isGit: true,
|
||||
repoRoot: worktreeRoot,
|
||||
mainRepoRoot: reportedMainRepoRoot,
|
||||
currentBranch,
|
||||
remoteUrl,
|
||||
isPaseoOwnedWorktree,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getCheckoutStatus(
|
||||
cwd: string,
|
||||
context?: CheckoutContext,
|
||||
|
||||
Reference in New Issue
Block a user