Skip to content
Merged
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
62 changes: 30 additions & 32 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const LEFTOVER_WORKTREE_REMOVE_DELAY = Duration.millis(250);
const WORKTREE_REMOVE_TIMEOUT_MS = 5 * 60_000;
/** Checkouts inspected at once while listing worktree statuses. */
const WORKTREE_STATUS_CONCURRENCY = 4;
/** Folder checks for `git worktree list` entries; each is one cheap stat. */
const WORKTREE_EXISTS_CONCURRENCY = 64;
/**
* Shipping verdicts kept per (base commit, branch commit) pair. Commit ids pin
* both histories, so a verdict never goes stale; the cap only bounds memory.
Expand Down Expand Up @@ -859,7 +861,8 @@ interface WorktreeRegistration {
*/
function parseWorktreeRegistrations(stdout: string): ReadonlyArray<WorktreeRegistration> {
const registrations: WorktreeRegistration[] = [];
for (const block of stdout.split("\n\n")) {
// Entries are separated by a blank line; CRLF output would hide that.
for (const block of stdout.replaceAll("\r\n", "\n").split("\n\n")) {
const lines = block.split("\n");
const header = lines.find((line) => line.startsWith("worktree "));
if (header === undefined) {
Expand Down Expand Up @@ -4172,44 +4175,39 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
* hand, so callers that resolve a path (branch pickers, cwd inference) would
* otherwise point at nothing.
*/
/**
* The checkouts `git worktree list --porcelain` reports whose folder still
* exists. The folders are checked side by side: a repository with ~90
* worktrees took seconds checking them one after another on a busy server,
* and every branch list waits on this.
*/
const parseWorktreeList = Effect.fn("parseWorktreeList")(function* (
result: GitVcsDriver.ExecuteGitResult,
) {
if (result.exitCode !== 0) {
return [] as ReadonlyArray<GitVcsDriver.GitWorktreeEntry>;
}
const entries: Array<GitVcsDriver.GitWorktreeEntry> = [];
let currentPath: string | null = null;
for (const line of result.stdout.split("\n")) {
if (line.startsWith("worktree ")) {
const candidatePath = line.slice("worktree ".length);
const exists = yield* fileSystem.stat(candidatePath).pipe(
Effect.map(() => true),
const registrations = parseWorktreeRegistrations(result.stdout);
const present = yield* Effect.forEach(
registrations,
(registration) =>
fileSystem.stat(registration.path).pipe(
Effect.as(true),
Effect.catch(() => Effect.succeed(false)),
);
currentPath = exists ? candidatePath : null;
if (currentPath) {
entries.push({ path: currentPath, branch: null });
}
} else if (line.startsWith("branch refs/heads/") && currentPath) {
const branch = line.slice("branch refs/heads/".length);
const pending = entries.at(-1);
if (pending && pending.path === currentPath) {
entries[entries.length - 1] = { ...pending, branch };
}
} else if ((line === "locked" || line.startsWith("locked ")) && currentPath) {
const pending = entries.at(-1);
if (pending && pending.path === currentPath) {
entries[entries.length - 1] = {
...pending,
lockReason: line.slice("locked".length).trim(),
};
}
} else if (line === "") {
currentPath = null;
}
}
return entries as ReadonlyArray<GitVcsDriver.GitWorktreeEntry>;
),
{ concurrency: WORKTREE_EXISTS_CONCURRENCY },
);
return registrations.flatMap((registration, index): Array<GitVcsDriver.GitWorktreeEntry> =>
present[index]
? [
{
path: registration.path,
branch: registration.branch,
...(registration.lockReason === null ? {} : { lockReason: registration.lockReason }),
},
]
: [],
);
});

const listWorktrees: GitVcsDriver.GitVcsDriverShape["listWorktrees"] = Effect.fn("listWorktrees")(
Expand Down
59 changes: 59 additions & 0 deletions apps/server/src/vcs/VcsProjectConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,65 @@ describe("VcsProjectConfig", () => {
);
});

it.layer(TestLayer)("prefers the nearest vcs.json", (it) => {
it.effect("uses a nested workspace's config over the repository root's", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fileSystem.makeTempDirectoryScoped({
prefix: "threadlines-vcs-config-test-",
});
const nested = path.join(root, "packages", "app");
const writeConfig = (directory: string, kind: string) =>
fileSystem
.makeDirectory(directory, { recursive: true })
.pipe(
Effect.andThen(
fileSystem.writeFileString(
path.join(directory, "vcs.json"),
JSON.stringify({ vcs: { kind } }),
),
),
);
yield* writeConfig(path.join(root, ".threadlines"), "jj");
yield* writeConfig(path.join(nested, ".t3code"), "git");

const config = yield* VcsProjectConfig.VcsProjectConfig;
const kind = yield* config.resolveKind({ cwd: nested });

assert.equal(kind, "git");
}),
);
});

// A check further up that never answers (an unreachable network mount)
// must not hold back the nearer config that exists.
it.layer(
VcsProjectConfig.layer.pipe(
Layer.provide(
FileSystem.layerNoop({
exists: (candidate) =>
candidate === "/repo/packages/app/.t3code/vcs.json"
? Effect.succeed(true)
: candidate === "/repo/.threadlines/vcs.json"
? Effect.never
: Effect.succeed(false),
readFileString: () => Effect.succeed('{"vcs":{"kind":"git"}}'),
}),
),
Layer.provide(NodeServices.layer),
),
)("answers once the nearest config is known", (it) => {
it.effect("doesn't wait on a stalled check further up", () =>
Effect.gen(function* () {
const config = yield* VcsProjectConfig.VcsProjectConfig;
const kind = yield* config.resolveKind({ cwd: "/repo/packages/app" });

assert.equal(kind, "git");
}),
);
});

it.layer(TestLayer)("falls back to legacy .t3code/vcs.json", (it) => {
it.effect("returns the configured kind", () =>
Effect.gen(function* () {
Expand Down
26 changes: 20 additions & 6 deletions apps/server/src/vcs/VcsProjectConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
Expand Down Expand Up @@ -60,22 +61,35 @@ export const make = Effect.fn("makeVcsProjectConfig")(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;

/**
* The nearest `vcs.json` at or above `cwd`. Every candidate up to the root
* is checked side by side (each check waits its turn on a busy server). The
* answer comes as soon as the nearest existing one is known, so a stalled
* check further up (an unreachable network mount) doesn't hold it.
*/
const findConfigPath = Effect.fn("VcsProjectConfig.findConfigPath")(function* (cwd: string) {
const candidates: Array<string> = [];
let current = cwd;
while (true) {
for (const directoryName of PROJECT_VCS_CONFIG_DIR_NAMES) {
const candidate = path.join(current, directoryName, "vcs.json");
if (yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))) {
return candidate;
}
candidates.push(path.join(current, directoryName, "vcs.json"));
}

const parent = path.dirname(current);
if (parent === current) {
return null;
break;
}
current = parent;
}
const checks = yield* Effect.forEach(candidates, (candidate) =>
Effect.forkChild(fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))),
);
for (const [index, check] of checks.entries()) {
if (yield* Fiber.join(check)) {
yield* Fiber.interruptAll(checks.slice(index + 1));
return candidates[index] ?? null;
}
}
return null;
});

const readConfiguredKind = Effect.fn("VcsProjectConfig.readConfiguredKind")(function* (
Expand Down
Loading