This carries #727's reviewed part forward, without auto-approve (that comes as its own PR). Permission behaviour is the same as 1.2. - Windows Git Bash detection, never the WSL System32 bash. It can be overridden in Settings and is passed per spawn only. - GBK / system code page decoding of raw process output. - Folded tool steps that don't wrap at ~600px. - Select-to-quote, and copy/edit under the user bubble. - Instant send with the echo replacing the bubble in place, and retry when a send fails. - `/app` heals itself after sleep/wake. `frame-ancestors` is limited to loopback and Magpie's window. - Grayscale timeline polish: one-line tool rows, a task-list pill, a reply footer and code blocks. - Test fixes: - The `initializing` push is awaited. - `afterEach` waits for the thread store's writes. - `runtimeInfo().starting` is exposed, and the steer test waits on it. - "idle recycling" waits for closed. Run on mote (macOS) at 65ab7c94: - `npm ci`, `npm run check` and `build` are ok; `verify:build` gives 5/5 OK. - Full `vitest run` x5: 4 green (427 passed). One red, "send while running → queued", is a race that is on main too. - Cause: `setStatus(idle)` runs before `queue.shift()`. - A 300ms pause injected there makes it red 3/3 on main and on this head. - Filed on #585 with the cause and the repro. - Focused send-latency + manager runs: 12/12. `e2e:v1`: 25/25, with no pageerror or console error. - Broken by hand: - With the old steer wait and the mock send held 400ms, the test is red 3/3; the new wait passes 3/3. - With `recycleIdle` made a no-op, "idle recycling" goes red. - Playwright render: - Chromium and WebKit × zh/en × light/dark × 1280/420. - No gradients, no coloured left borders, no `<select>`, no page errors. - At 420 the page scrolls sideways (scrollWidth 480), the same as main. Not tried: Windows (Git Bash detection, GBK decoding) and a real agent, rather than the mock.
142 lines
7.1 KiB
TypeScript
142 lines
7.1 KiB
TypeScript
// GitService (§5.8): runs the local git via execFile (no shell), cwd = project path, 15 s timeout.
|
|
import { execFile } from "node:child_process";
|
|
import { existsSync } from "node:fs";
|
|
import { cp, mkdir, readFile, rename, rm, stat } from "node:fs/promises";
|
|
import { join, normalize, sep } from "node:path";
|
|
import { tr } from "../util/i18n.js";
|
|
import { decodeOutput } from "../util/decode.js";
|
|
|
|
export type GitFileEntry = {
|
|
path: string;
|
|
index: string;
|
|
worktree: string;
|
|
kind: "modified" | "added" | "deleted" | "untracked" | "renamed";
|
|
};
|
|
export type GitStatus = { isRepo: boolean; branch?: string; files?: GitFileEntry[]; error?: string };
|
|
|
|
const TIMEOUT = 15_000;
|
|
const DIFF_LIMIT = 1024 * 1024;
|
|
|
|
function runGit(cwd: string, args: string[]): Promise<{ code: number; out: string; err: string }> {
|
|
return new Promise((res) => {
|
|
// core.quotepath=false: non-ASCII paths (e.g. Chinese file names) are no longer turned into "\346\226\207" octal escapes in diff headers
|
|
// stdout (paths, diffs) is UTF-8; stderr may carry hook / credential-helper messages in the Windows code page (1.3)
|
|
execFile("git", ["-c", "core.quotepath=false", ...args], { cwd, timeout: TIMEOUT, maxBuffer: 8 * 1024 * 1024, windowsHide: true, encoding: "buffer" }, (e, stdout, stderr) => {
|
|
const code = e && typeof (e as { code?: number }).code === "number" ? (e as { code: number }).code! : e ? 1 : 0;
|
|
const missing = (e as NodeJS.ErrnoException | null)?.code === "ENOENT";
|
|
const errText = stderr?.length ? decodeOutput(stderr) : "";
|
|
res({ code, out: stdout ? stdout.toString("utf8") : "", err: missing ? tr("git.missing") : errText || (e?.message ?? "") });
|
|
});
|
|
});
|
|
}
|
|
|
|
// The path must be inside the project (no .. escape); returns the project-relative path (posix form)
|
|
function insidePath(projectPath: string, p: string): string | null {
|
|
const rel = normalize(p).replace(/\\/g, "/");
|
|
const abs = normalize(join(projectPath, rel));
|
|
const proj = normalize(projectPath);
|
|
if (abs !== proj && !abs.startsWith(proj + sep)) return null;
|
|
return rel.replace(/\\/g, "/");
|
|
}
|
|
|
|
// rename across drives/volumes (Windows project on D:, data dir on C:; different mount points on Linux) fails with EXDEV → copy then delete
|
|
export async function moveAcrossDevices(
|
|
from: string,
|
|
to: string,
|
|
ops: { rename?: typeof rename; cp?: typeof cp; rm?: typeof rm } = {},
|
|
): Promise<void> {
|
|
const ren = ops.rename ?? rename;
|
|
try {
|
|
await ren(from, to);
|
|
} catch (e) {
|
|
const code = (e as NodeJS.ErrnoException)?.code;
|
|
if (code !== "EXDEV") throw e;
|
|
await (ops.cp ?? cp)(from, to, { recursive: true, errorOnExist: true, force: false, preserveTimestamps: true });
|
|
await (ops.rm ?? rm)(from, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
export class GitService {
|
|
// projectPath is resolved from projectId by the caller (the service doesn't know about the store)
|
|
async status(projectPath: string): Promise<GitStatus> {
|
|
const inside = await runGit(projectPath, ["rev-parse", "--is-inside-work-tree"]);
|
|
if (inside.code !== 0 || inside.out.trim() !== "true") return { isRepo: false };
|
|
const br = await runGit(projectPath, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
|
const st = await runGit(projectPath, ["status", "--porcelain=v1", "-z", "-uall"]);
|
|
if (st.code !== 0) return { isRepo: true, branch: br.out.trim() || undefined, error: st.err.trim() || "status 失败" };
|
|
const files: GitFileEntry[] = [];
|
|
const parts = st.out.split("\0");
|
|
for (let i = 0; i < parts.length; i++) {
|
|
const rec = parts[i];
|
|
if (!rec || rec.length < 4) continue;
|
|
const index = rec[0];
|
|
const worktree = rec[1];
|
|
let path = rec.slice(3);
|
|
let kind: GitFileEntry["kind"] = "modified";
|
|
if (index === "?" && worktree === "?") kind = "untracked";
|
|
else if (index === "R" || worktree === "R") {
|
|
kind = "renamed";
|
|
// A renamed record looks like "R new\0old": the next part is the old path, skip it
|
|
i++;
|
|
} else if (index === "A" || worktree === "A") kind = "added";
|
|
else if (index === "D" || worktree === "D") kind = "deleted";
|
|
files.push({ path, index, worktree, kind });
|
|
}
|
|
return { isRepo: true, branch: br.out.trim() || undefined, files };
|
|
}
|
|
|
|
// Unified diff text; {tooLarge:true} when the diff is > 1MB
|
|
async diff(projectPath: string, p: string): Promise<{ diff?: string; tooLarge?: boolean; untracked?: boolean; error?: string }> {
|
|
const rel = insidePath(projectPath, p);
|
|
if (!rel) return { error: tr("fs.pathDenied") };
|
|
// Check untracked first (git diff prints nothing for untracked files)
|
|
const ls = await runGit(projectPath, ["ls-files", "--error-unmatch", "--", rel]);
|
|
if (ls.code !== 0) {
|
|
// Untracked/not in the index: treat the whole file as added
|
|
const full = join(projectPath, rel);
|
|
if (!existsSync(full)) return { error: tr("fs.fileMissing") };
|
|
const s = await stat(full);
|
|
if (!s.isFile()) return { error: tr("fs.notFile") };
|
|
if (s.size > DIFF_LIMIT) return { tooLarge: true };
|
|
const text = await readFile(full, "utf8").catch(() => null);
|
|
if (text === null) return { error: tr("git.binary") };
|
|
const lines = text.split("\n").map((l) => "+" + l).join("\n");
|
|
return { untracked: true, diff: `--- /dev/null\n+++ b/${rel}\n@@ -0,0 +1,${text.split("\n").length} @@\n${lines}` };
|
|
}
|
|
let d = await runGit(projectPath, ["diff", "--no-color", "--", rel]);
|
|
if (d.code !== 0) return { error: d.err.trim() || "diff 失败" };
|
|
if (!d.out.trim()) {
|
|
d = await runGit(projectPath, ["diff", "--cached", "--no-color", "--", rel]);
|
|
if (d.code !== 0) return { error: d.err.trim() || "diff 失败" };
|
|
}
|
|
if (d.out.length > DIFF_LIMIT) return { tooLarge: true };
|
|
return { diff: d.out };
|
|
}
|
|
|
|
// Tracked → git restore; untracked → move to trash/discarded/<ts>/<relpath>
|
|
async discard(projectPath: string, p: string, dataDir: string): Promise<{ ok: boolean; trashedTo?: string; error?: string }> {
|
|
const rel = insidePath(projectPath, p);
|
|
if (!rel) return { ok: false, error: tr("fs.pathDenied") };
|
|
// Only discard a single file: an empty path / "." would make git restore act on the whole repository
|
|
if (rel === "." || rel === "" || rel === "./") return { ok: false, error: tr("fs.pathDenied") };
|
|
const ls = await runGit(projectPath, ["ls-files", "--error-unmatch", "--", rel]);
|
|
if (ls.code === 0) {
|
|
let r = await runGit(projectPath, ["restore", "--staged", "--worktree", "--", rel]);
|
|
if (r.code !== 0) r = await runGit(projectPath, ["checkout", "--", rel]);
|
|
if (r.code !== 0) return { ok: false, error: r.err.trim() || "放弃失败" };
|
|
return { ok: true };
|
|
}
|
|
const full = join(projectPath, rel);
|
|
if (!existsSync(full)) return { ok: false, error: tr("fs.fileMissing") };
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
const dest = join(dataDir, "trash", "discarded", stamp, rel);
|
|
await mkdir(join(dest, ".."), { recursive: true });
|
|
try {
|
|
await moveAcrossDevices(full, dest);
|
|
} catch (e) {
|
|
return { ok: false, error: "移动失败:" + String((e as Error)?.message ?? e) };
|
|
}
|
|
return { ok: true, trashedTo: dest };
|
|
}
|
|
}
|