evOo-local/packages/core/src/git-checkout.ts
cynarAI 2b967df569
fix(api): turn repositories found by the onboarding scan into real projects (#116)
The scan's project phase called prisma.space with fields the Space model
does not have (title, path). Both calls threw, a per-repository
try/catch swallowed the errors, and no project was ever created. With
the field names fixed, the space would still have had no organization
or membership, so nobody could have seen it.

The phase now goes through createSpaceForMember and
wireProjectWorkspace, the helpers behind spaces.create: each repository
becomes a project of the deployment owner in their current
organization, linked to its folder with its git remote, branch and
head. It still runs only in the owner's scan with ONBOARDING_SCAN_HOME
set. A repository whose real folder one of the owner's projects already
links is skipped, so scanning again creates nothing; the phase stops at
the per-member space limit; and a second start while a scan runs (a
StrictMode remount, a retry) joins it instead of linking everything
twice. Discovery no longer descends into dot folders, where shell and
editor plugin checkouts would otherwise become projects. The Prisma
client is typed, so a field the schema lacks is a compile error again
instead of a swallowed one.

readGitCheckout loaded child_process with require(), which does not
exist in the ES module runtime the API runs in, so linking any folder
with a .git directory threw "require is not defined", in the scan and
in the New project dialog alike. It now imports spawn statically; the
module is only reachable through its server-only subpath, so browser
bundles still never load it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ebcq9seT9eCoHtqxuWEUzg
2026-10-03 18:22:10 +02:00

87 lines
2.9 KiB
TypeScript

// Server-only. Kept out of the package index so browser bundles never load `node:child_process`.
import { spawn } from "node:child_process";
import { access } from "node:fs/promises";
import path from "node:path";
import { gitStatusArgv, gitUntrackedFilesArgv, parseGitNameOnly } from "./compose-update.js";
import { DEFAULT_UPDATE_REMOTE, parseGitStatusPorcelain } from "./self-update.js";
export interface GitCheckoutStatus {
present: boolean;
remoteUrl: string | null;
defaultBranch: string | null;
headCommit: string | null;
dirty: boolean;
dirtyPaths: string[];
}
export interface GitRunnerResult {
ok: boolean;
output: string;
}
export type GitRunner = (args: string[], cwd: string) => Promise<GitRunnerResult>;
export async function defaultGitRunner(args: string[], cwd: string): Promise<GitRunnerResult> {
return new Promise((resolve) => {
const child = spawn("git", args, { cwd, env: process.env });
let output = "";
child.stdout.on("data", (chunk: Buffer | string) => {
output += chunk.toString();
});
child.stderr.on("data", (chunk: Buffer | string) => {
output += chunk.toString();
});
child.on("error", () => resolve({ ok: false, output }));
child.on("close", (code) => resolve({ ok: code === 0, output }));
});
}
async function hasGitMetadata(directory: string): Promise<boolean> {
try {
await access(path.join(directory, ".git"));
return true;
} catch {
return false;
}
}
/** Reads local git metadata from an existing checkout without fetching remotes. */
export async function readGitCheckout(
directory: string,
runGit: GitRunner = defaultGitRunner,
): Promise<GitCheckoutStatus> {
const root = path.resolve(directory);
if (!(await hasGitMetadata(root))) {
return {
present: false,
remoteUrl: null,
defaultBranch: null,
headCommit: null,
dirty: false,
dirtyPaths: [],
};
}
const [head, branch, remote, status, untracked] = await Promise.all([
runGit(["rev-parse", "HEAD"], root),
runGit(["rev-parse", "--abbrev-ref", "HEAD"], root),
runGit(["remote", "get-url", DEFAULT_UPDATE_REMOTE], root),
runGit(["--no-optional-locks", ...gitStatusArgv()], root),
runGit(["--no-optional-locks", ...gitUntrackedFilesArgv()], root),
]);
const porcelain = parseGitStatusPorcelain(status.ok ? status.output : "");
const untrackedPaths = parseGitNameOnly(untracked.ok ? untracked.output : "");
const dirtyPaths = [...new Set([...porcelain.changed, ...untrackedPaths])];
const stateReadable = status.ok && untracked.ok;
return {
present: true,
remoteUrl: remote.ok ? remote.output.trim() || null : null,
defaultBranch: branch.ok ? branch.output.trim() || null : null,
headCommit: head.ok ? head.output.trim() || null : null,
dirty: stateReadable ? dirtyPaths.length > 0 : true,
dirtyPaths: stateReadable ? dirtyPaths : [],
};
}