@evandu/super-pi 0.1.231 → 0.1.232

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/client/.super-pi-client-sources.json +4 -4
  2. package/dist/client/assets/{CodeMirrorEditor-DID93847.js → CodeMirrorEditor-UhCYm5Wx.js} +2 -2
  3. package/dist/client/assets/{dist-Bv7cRIVR.js → dist-B9BbvWot.js} +1 -1
  4. package/dist/client/assets/{dist-C1xMWmS1.js → dist-BFeunbEX.js} +1 -1
  5. package/dist/client/assets/{dist-hbrdp5Te.js → dist-BIy5crB3.js} +1 -1
  6. package/dist/client/assets/{dist-BV3vRZ2y.js → dist-BJ6foecv.js} +1 -1
  7. package/dist/client/assets/{dist-DCp9UGuI.js → dist-BKp3q2wn.js} +1 -1
  8. package/dist/client/assets/{dist-gxPEZLSf.js → dist-BpINRZym.js} +1 -1
  9. package/dist/client/assets/{dist-DAOEPk-0.js → dist-Bv4wCENt.js} +1 -1
  10. package/dist/client/assets/{dist-Df4Kt0qp.js → dist-C0RopB4q.js} +1 -1
  11. package/dist/client/assets/{dist-C5DwUAF0.js → dist-C31-s_h7.js} +1 -1
  12. package/dist/client/assets/{dist-B6rCaeRi.js → dist-C6wg16BA.js} +1 -1
  13. package/dist/client/assets/{dist-BKrl_5gD.js → dist-C7VEKkD-.js} +1 -1
  14. package/dist/client/assets/{dist-C_rYZ4Ky.js → dist-D8RY2jNq.js} +1 -1
  15. package/dist/client/assets/{dist-DZ2c3_-S.js → dist-DEKenQm-.js} +1 -1
  16. package/dist/client/assets/{dist-3Pg6D38j.js → dist-DXa_Ik6q.js} +1 -1
  17. package/dist/client/assets/{dist-CP9KnSJ0.js → dist-Dfq0xUba.js} +1 -1
  18. package/dist/client/assets/{dist-DIxMpUkq.js → dist-H6BCSlev.js} +1 -1
  19. package/dist/client/assets/{dist-LTYjIRRC.js → dist-IEeNkSQA.js} +1 -1
  20. package/dist/client/assets/{dist-CbrabxHt.js → dist-Opwy6jSg.js} +1 -1
  21. package/dist/client/assets/{dist-DOAMUZ7W.js → dist-UKx13tHO.js} +1 -1
  22. package/dist/client/assets/{dist-Cs3DwIND.js → dist-Ydfp2kM0.js} +1 -1
  23. package/dist/client/assets/{dist-BTSVYElY.js → dist-a2NOYHv3.js} +1 -1
  24. package/dist/client/assets/{dist-IwOdQEYa.js → dist-znfz-BcE.js} +1 -1
  25. package/dist/client/assets/{index-pgCioXy9.js → index-DREwHzYy.js} +56 -56
  26. package/dist/client/assets/{index-ZZn2vRR_.css → index-P5ei_l2b.css} +1 -1
  27. package/dist/client/index.html +2 -2
  28. package/dist/server/git.d.ts +50 -0
  29. package/dist/server/git.js +90 -3
  30. package/dist/server/http-server.js +32 -2
  31. package/package.json +1 -1
@@ -8,8 +8,8 @@
8
8
  <link rel="alternate icon" href="/favicon.ico" />
9
9
  <link rel="apple-touch-icon" href="/favicon.svg" />
10
10
  <title>Super Pi</title>
11
- <script type="module" crossorigin src="/assets/index-pgCioXy9.js"></script>
12
- <link rel="stylesheet" crossorigin href="/assets/index-ZZn2vRR_.css">
11
+ <script type="module" crossorigin src="/assets/index-DREwHzYy.js"></script>
12
+ <link rel="stylesheet" crossorigin href="/assets/index-P5ei_l2b.css">
13
13
  </head>
14
14
  <body>
15
15
  <div id="root"></div>
@@ -24,6 +24,56 @@ export declare function gitLog(root: string, limit: number, cwd?: string, query?
24
24
  }[];
25
25
  truncated: boolean;
26
26
  }>;
27
+ export type GitWorktree = {
28
+ id: string;
29
+ path: string;
30
+ name: string;
31
+ branch: string | null;
32
+ head: string;
33
+ current: boolean;
34
+ primary: boolean;
35
+ locked: boolean;
36
+ missing: boolean;
37
+ };
38
+ export declare function gitWorktrees(root: string): Promise<{
39
+ worktrees: GitWorktree[];
40
+ commonDir: string;
41
+ }>;
42
+ export declare function resolveGitWorktree(root: string, id: string): Promise<{
43
+ path: string;
44
+ base: GitWorktree | undefined;
45
+ }>;
46
+ export declare function inspectGitWorktree(path: string, base: GitWorktree | undefined): Promise<{
47
+ status: GitWorkspace;
48
+ comparison: null;
49
+ } | {
50
+ status: GitWorkspace;
51
+ comparison: {
52
+ base: string;
53
+ ahead: number;
54
+ behind: number;
55
+ commits: {
56
+ hash: string;
57
+ short: string;
58
+ subject: string;
59
+ author: string;
60
+ date: string;
61
+ }[];
62
+ truncated: boolean;
63
+ files: GitFile[];
64
+ };
65
+ }>;
66
+ export declare function gitWorktreeCommittedDiff(root: string, base: GitWorktree | undefined, path: string): Promise<{
67
+ path: string;
68
+ staged: boolean;
69
+ kind: "text";
70
+ diff: string;
71
+ } | {
72
+ path: string;
73
+ staged: boolean;
74
+ kind: "binary";
75
+ diff: null;
76
+ }>;
27
77
  export declare function gitBranches(root: string, cwd: string, query: string, offset: number): Promise<{
28
78
  branches: {
29
79
  name: string;
@@ -1,8 +1,9 @@
1
1
  import { execFile, spawn } from "node:child_process";
2
+ import { createHash } from "node:crypto";
2
3
  import { promisify } from "node:util";
3
4
  import { basename, relative, resolve, sep } from "node:path";
4
5
  import { existsSync } from "node:fs";
5
- import { readFile, stat } from "node:fs/promises";
6
+ import { readFile, realpath, stat } from "node:fs/promises";
6
7
  const exec = promisify(execFile);
7
8
  async function git(cwd, args, options) { return exec("git", ["--literal-pathspecs", "-c", "core.quotepath=false", ...args], { cwd, windowsHide: true, maxBuffer: options?.maxBuffer ?? 8 * 1024 * 1024, timeout: options?.timeout }); }
8
9
  function files(raw, cwd = "") {
@@ -70,12 +71,16 @@ export async function gitLog(root, limit, cwd = "", query = "", offset = 0) {
70
71
  if (!matchingPaths.stdout)
71
72
  return { commits: [], truncated: false };
72
73
  }
73
- const parts = (await git(root, args)).stdout.split("\0");
74
+ const commits = parseGitCommits((await git(root, args)).stdout);
75
+ return { commits: commits.slice(0, size), truncated: commits.length > size };
76
+ }
77
+ function parseGitCommits(raw) {
78
+ const parts = raw.split("\0");
74
79
  const commits = [];
75
80
  for (let i = 0; i + 4 < parts.length; i += 5) {
76
81
  commits.push({ hash: parts[i], short: parts[i + 1], subject: parts[i + 2], author: parts[i + 3], date: new Date(parts[i + 4]).toISOString() });
77
82
  }
78
- return { commits: commits.slice(0, size), truncated: commits.length > size };
83
+ return commits;
79
84
  }
80
85
  const normalizedPath = (path) => process.platform === "win32" ? resolve(path).toLowerCase() : resolve(path);
81
86
  async function worktreeRoot(root) {
@@ -84,6 +89,88 @@ async function worktreeRoot(root) {
84
89
  async function canChangeWorktree(root, cwd) {
85
90
  return normalizedPath(resolve(root, cwd)) === normalizedPath(await worktreeRoot(root));
86
91
  }
92
+ async function gitCommonDir(root) {
93
+ const { stdout } = await git(root, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
94
+ return realpath(stdout.trimEnd());
95
+ }
96
+ export async function gitWorktrees(root) {
97
+ const rootPath = await realpath(root);
98
+ const actualTop = await realpath(await worktreeRoot(root));
99
+ if (normalizedPath(rootPath) !== normalizedPath(actualTop)) {
100
+ throw gitFailure("在仓库根目录工作区中查看工作树", "outside_scope");
101
+ }
102
+ const [commonDir, result] = await Promise.all([
103
+ gitCommonDir(rootPath), git(rootPath, ["worktree", "list", "--porcelain", "-z"]),
104
+ ]);
105
+ const worktrees = [];
106
+ for (const [index, record] of result.stdout.split("\0\0").entries()) {
107
+ const fields = new Map(record.split("\0").map(field => {
108
+ const delimiter = field.indexOf(" ");
109
+ return delimiter < 0 ? [field, ""] : [field.slice(0, delimiter), field.slice(delimiter + 1)];
110
+ }));
111
+ const path = fields.get("worktree");
112
+ if (!path || fields.has("bare"))
113
+ continue;
114
+ const pathKey = normalizedPath(path);
115
+ const id = createHash("sha256").update(normalizedPath(commonDir)).update("\0").update(pathKey).digest("hex");
116
+ worktrees.push({
117
+ id, path, name: basename(path), branch: fields.get("branch")?.replace(/^refs\/heads\//, "") ?? null,
118
+ head: fields.get("HEAD") ?? "", primary: index === 0, current: pathKey === normalizedPath(rootPath),
119
+ locked: fields.has("locked"), missing: fields.has("prunable") || !existsSync(path),
120
+ });
121
+ }
122
+ return { worktrees, commonDir };
123
+ }
124
+ export async function resolveGitWorktree(root, id) {
125
+ const { worktrees, commonDir } = await gitWorktrees(root);
126
+ const entry = worktrees.find(worktree => worktree.id === id);
127
+ if (!entry || entry.missing)
128
+ throw gitFailure("此工作树已不存在,请刷新工作树列表", "not_found");
129
+ const checked = await Promise.all([realpath(entry.path), worktreeRoot(entry.path).then(path => realpath(path)), gitCommonDir(entry.path)]).catch(() => null);
130
+ if (!checked || normalizedPath(checked[0]) !== normalizedPath(checked[1]) || normalizedPath(checked[2]) !== normalizedPath(commonDir)) {
131
+ throw gitFailure("该目录已失效或不属于当前仓库", "not_found");
132
+ }
133
+ return { path: checked[0], base: worktrees.find(item => item.primary && !item.missing) ?? worktrees.find(item => item.current) };
134
+ }
135
+ const WORKTREE_COMMIT_LIMIT = 20;
136
+ async function worktreeComparisonBase(path, base) {
137
+ if (!base || !/^[0-9a-f]{40}([0-9a-f]{24})?$/.test(base.head))
138
+ return null;
139
+ const target = await git(path, ["rev-parse", "--verify", "HEAD"]).then(result => result.stdout.trim()).catch(() => "");
140
+ if (!target)
141
+ return null;
142
+ const mergeBase = await git(path, ["merge-base", base.head, target]).then(result => result.stdout.trim()).catch(() => "");
143
+ return mergeBase ? { label: base.branch ?? base.head.slice(0, 8), head: base.head, target, mergeBase } : null;
144
+ }
145
+ export async function inspectGitWorktree(path, base) {
146
+ const [status, reference] = await Promise.all([gitStatus(path), worktreeComparisonBase(path, base)]);
147
+ if (!reference)
148
+ return { status, comparison: null };
149
+ const [counts, history, changes] = await Promise.all([
150
+ git(path, ["rev-list", "--left-right", "--count", `${reference.head}...${reference.target}`]),
151
+ git(path, ["log", "-z", `-${WORKTREE_COMMIT_LIMIT + 1}`, "--format=%H%x00%h%x00%s%x00%an%x00%aI", `${reference.head}..${reference.target}`, "--"]),
152
+ git(path, ["diff", "--name-status", "--no-renames", "-z", reference.mergeBase, reference.target, "--"]),
153
+ ]);
154
+ const [behind, ahead] = counts.stdout.trim().split(/\s+/).map(Number);
155
+ const commits = parseGitCommits(history.stdout);
156
+ const records = changes.stdout.split("\0");
157
+ const files = [];
158
+ for (let index = 0; index + 1 < records.length; index += 2) {
159
+ const [xy, file] = records.slice(index, index + 2);
160
+ files.push({ xy, path: file, name: basename(file) });
161
+ }
162
+ return { status, comparison: { base: reference.label, ahead, behind, commits: commits.slice(0, WORKTREE_COMMIT_LIMIT), truncated: commits.length > WORKTREE_COMMIT_LIMIT, files } };
163
+ }
164
+ export async function gitWorktreeCommittedDiff(root, base, path) {
165
+ safePath(root, path);
166
+ const reference = await worktreeComparisonBase(root, base);
167
+ if (!reference)
168
+ return { path, staged: false, kind: "text", diff: "" };
169
+ const { stdout } = await git(root, ["diff", "--no-color", "--no-ext-diff", "--no-textconv", reference.mergeBase, reference.target, "--", path]);
170
+ if (/^Binary files .* differ$/m.test(stdout))
171
+ return { path, staged: false, kind: "binary", diff: null };
172
+ return { path, staged: false, kind: "text", diff: stdout };
173
+ }
87
174
  export async function gitBranches(root, cwd, query, offset) {
88
175
  const canWrite = await canChangeWorktree(root, cwd);
89
176
  const filter = query.trim().slice(0, 160).toLowerCase();
@@ -12,7 +12,7 @@ import { describe as describeFilesError, FilesError, fileMeta, listDirectory, MA
12
12
  import { findUploadFile, MAX_UPLOAD_BODY_BYTES, saveUpload } from "./upload.js";
13
13
  import { describeThrown } from "./failure-text.js";
14
14
  import { checkDiskVersion } from "./version.js";
15
- import { gitBlame, gitBranches, gitDiff, gitLog, gitMutate, gitStagedDraftInput, gitStashes, gitStatus } from "./git.js";
15
+ import { gitBlame, gitBranches, gitDiff, gitLog, gitMutate, gitStagedDraftInput, gitStashes, gitStatus, gitWorktrees, resolveGitWorktree, inspectGitWorktree, gitWorktreeCommittedDiff } from "./git.js";
16
16
  import { computeFileDiff } from "./file-diff.js";
17
17
  import { verifyCapability } from "./capability.js";
18
18
  import { conversationWorkspace } from "./model-filter.js";
@@ -1272,9 +1272,39 @@ export function createHttpServer(options) {
1272
1272
  json(404, { error: `工作区 ${workspaceId ?? "(missing)"} 不存在`, code: "no_workspace" });
1273
1273
  return;
1274
1274
  }
1275
+ const op = url.searchParams.get("op") ?? "status";
1276
+ if (op === "worktrees" || op === "worktree-inspect" || op === "worktree-diff") {
1277
+ if (request.method !== "GET") {
1278
+ json(405, { error: "查看工作树需要 GET 请求", code: "method" });
1279
+ return;
1280
+ }
1281
+ if (op === "worktrees") {
1282
+ json(200, { worktrees: (await gitWorktrees(workspace.path)).worktrees });
1283
+ return;
1284
+ }
1285
+ const id = url.searchParams.get("worktreeId") ?? "";
1286
+ if (!/^[a-f0-9]{64}$/.test(id)) {
1287
+ json(400, { error: "请先选择有效工作树", code: "bad_worktree" });
1288
+ return;
1289
+ }
1290
+ const checkout = await resolveGitWorktree(workspace.path, id);
1291
+ if (op === "worktree-inspect") {
1292
+ json(200, await inspectGitWorktree(checkout.path, checkout.base));
1293
+ return;
1294
+ }
1295
+ const file = url.searchParams.get("path");
1296
+ if (!file) {
1297
+ json(400, { error: "工作树差异需要文件路径", code: "bad_path" });
1298
+ return;
1299
+ }
1300
+ const path = await workspacePathFromSession(checkout.path, checkout.path, file);
1301
+ json(200, url.searchParams.get("comparison") === "committed"
1302
+ ? await gitWorktreeCommittedDiff(checkout.path, checkout.base, path)
1303
+ : await gitDiff(checkout.path, path, url.searchParams.get("staged") === "1"));
1304
+ return;
1305
+ }
1275
1306
  const sessionRoot = await resolveFileCwd(workspace.path, url.searchParams.get("cwd"));
1276
1307
  const cwd = relative(resolve(workspace.path), sessionRoot).replaceAll("\\", "/");
1277
- const op = url.searchParams.get("op") ?? "status";
1278
1308
  // Blame is a read with a GET shape: every other verb is refused HERE, before the POST branch
1279
1309
  // could ever route the op into gitMutate and before any body is read.
1280
1310
  if (op === "blame" && request.method !== "GET") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.231",
3
+ "version": "0.1.232",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",