@aiti-ai/aiti-git-ops 0.1.9
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.
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/aiti-git-ops.test.mjs +471 -0
- package/deploy-commit.mjs +52 -0
- package/dev-pack.mjs +246 -0
- package/ext-icons-deploy.mjs +24 -0
- package/git-engine.ts +967 -0
- package/icons/git-branch.svg +1 -0
- package/index.ts +161 -0
- package/package.json +21 -0
- package/reconcile-deps.mjs +41 -0
- package/reload-instances.mjs +60 -0
- package/tools/deploy.mjs +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 aiti-ai contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @aiti-ai/aiti-git-ops
|
|
2
|
+
|
|
3
|
+
> 白名单 git 写引擎——checkout 预检 / stash / plan 断点 / 审计 / changed topic,供 folder 与 session-manager 消费
|
|
4
|
+
|
|
5
|
+
## 来源 · Origin
|
|
6
|
+
|
|
7
|
+
本包是 **aiti** 生态的组成部分——以 [pi](https://pi.dev) 为引擎的个人 AI 开发工作台与本地调试注入生态:**aiti-agent**(自托管 Web 工作台,基于开源项目 [pi-web](https://github.com/aiti-ai/pi-web) 构建,浏览器为主操作面)+ 各开发工具内的调试注入(aiti-devtools)+ 丰富的扩展体系。
|
|
8
|
+
|
|
9
|
+
This package is part of the **aiti** ecosystem — a personal AI development workbench powered by [pi](https://pi.dev): **aiti-agent** (a self-hosted web workbench built on the open-source [pi-web](https://github.com/aiti-ai/pi-web)) plus debug injection into mainstream dev tools (aiti-devtools) and a rich extension system.
|
|
10
|
+
|
|
11
|
+
- 工作台 / workbench: <https://github.com/aiti-ai/pi-web>
|
|
12
|
+
- 生态仓库 / monorepo: <https://github.com/aiti-ai/aiti-ai>
|
|
13
|
+
- 官网 / site: <https://aiti.pw>
|
|
14
|
+
|
|
15
|
+
## 功能定位(中文)
|
|
16
|
+
|
|
17
|
+
白名单 git 写引擎——checkout 预检 / stash / plan 断点 / 审计 / changed topic,供 folder 与 session-manager 消费
|
|
18
|
+
|
|
19
|
+
服务端引擎 · tags: `git` · `server-side` · `engine`
|
|
20
|
+
|
|
21
|
+
## Positioning (English)
|
|
22
|
+
|
|
23
|
+
Whitelisted git write engine — checkout precheck / stash / plan breakpoints / audit / changed topic, consumed by folder and session-manager
|
|
24
|
+
|
|
25
|
+
## 安装 / 部署
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# 部署到 ~/.pi/web/extensions/aiti-git-ops/ 后 reload
|
|
29
|
+
node pi-web-ext/aiti-git-ops/tools/deploy.mjs
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 文档 · Documentation
|
|
33
|
+
|
|
34
|
+
- 中文完整文档(aiti-docs): <https://aiti.pw/extensions/aiti-git-ops.html>
|
|
35
|
+
- English docs: <https://aiti.pw/en/extensions/aiti-git-ops.html>
|
|
36
|
+
- 扩展注册表 / registry: <https://aiti.pw/extensions.html> · machine-readable <https://aiti.pw/extensions.json>
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* aiti-git-ops · 引擎全链路测试(方案 §8:M1 验收面 + 深审回归面)
|
|
3
|
+
*
|
|
4
|
+
* 临时 fixture 仓上直调扩展 server.api(aiti-custom-agent 范式),覆盖:
|
|
5
|
+
* refs/log、checkout 预检(dirty∩affected 409 / HEAD 同点豁免 / 非冲突脏文件放行)、
|
|
6
|
+
* stash-then-switch、注入与路径穿越拒绝、unicode 分支/tag、**异常文件名全形态**
|
|
7
|
+
* (中文/空格/双引号/换行/箭头/目录——Windows 非法字符做平台守卫)、
|
|
8
|
+
* plan 断点、stash 部分暂存/提取(-z 路径)、discard(暂存丢弃/嵌套去重/隔离区)、
|
|
9
|
+
* log 分页(--graph 无 --skip 语义)、stash push 错误透传、审计 jsonl。
|
|
10
|
+
*/
|
|
11
|
+
import { test } from "node:test";
|
|
12
|
+
import assert from "node:assert/strict";
|
|
13
|
+
import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync } from "node:fs";
|
|
14
|
+
import { tmpdir } from "node:os";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { execFileSync } from "node:child_process";
|
|
17
|
+
|
|
18
|
+
const { default: ext } = await import("./index.ts");
|
|
19
|
+
const { parseStatusZ } = await import("./git-engine.ts");
|
|
20
|
+
|
|
21
|
+
const WIN = process.platform === "win32"; // Windows 文件名禁 \n 与 " 与 >——异常名套件降子集
|
|
22
|
+
const tmp = mkdtempSync(join(tmpdir(), "git-ops-"));
|
|
23
|
+
const repo = join(tmp, "repo");
|
|
24
|
+
mkdirSync(repo, { recursive: true });
|
|
25
|
+
const configDir = join(tmp, "ext", "config");
|
|
26
|
+
mkdirSync(configDir, { recursive: true });
|
|
27
|
+
|
|
28
|
+
const NL_FILE = WIN ? "nl-file.txt" : "new\nline.txt"; // 换行文件名(win 无此形态)
|
|
29
|
+
const Q_FILE = WIN ? "quote-file.txt" : 'quo"te.txt'; // 双引号文件名
|
|
30
|
+
const ARROW_FILE = WIN ? "arrow-b.txt" : "a -> b.txt"; // 含 " -> "(v1 行解析会被误切)
|
|
31
|
+
const CN_DIR = "中文书";
|
|
32
|
+
const CN_FILE = CN_DIR + "/文件.txt";
|
|
33
|
+
const SP_FILE = "sp ace.txt";
|
|
34
|
+
const WEIRD = [NL_FILE, Q_FILE, ARROW_FILE, CN_FILE, SP_FILE];
|
|
35
|
+
|
|
36
|
+
const git = (args, opts = {}) => execFileSync("git", args, { cwd: repo, encoding: "utf8", ...opts });
|
|
37
|
+
const write = (rel, content) => writeFileSync(join(repo, rel), content);
|
|
38
|
+
|
|
39
|
+
// fixture:main 一提交(普通 + 异常名全集)
|
|
40
|
+
// 注:git init -b 需 git≥2.28(Apple Git 2.24 无)——用 symbolic-ref 设 unborn 分支名全版本兼容
|
|
41
|
+
git(["init"]);
|
|
42
|
+
git(["symbolic-ref", "HEAD", "refs/heads/main"]);
|
|
43
|
+
git(["config", "user.email", "t@t.local"]);
|
|
44
|
+
git(["config", "user.name", "t"]);
|
|
45
|
+
write("a.txt", "A1\n");
|
|
46
|
+
write("b.txt", "B1\n");
|
|
47
|
+
write("y.txt", "Y1\n");
|
|
48
|
+
mkdirSync(join(repo, CN_DIR), { recursive: true });
|
|
49
|
+
write(CN_FILE, "中1\n");
|
|
50
|
+
write(ARROW_FILE, "arrow1\n");
|
|
51
|
+
write(Q_FILE, "q1\n");
|
|
52
|
+
write(SP_FILE, "s1\n");
|
|
53
|
+
write(NL_FILE, "n1\n");
|
|
54
|
+
git(["add", "-A"]);
|
|
55
|
+
git(["commit", "-m", "init"]);
|
|
56
|
+
|
|
57
|
+
await ext.server.startup({ id: "aiti-git-ops", dir: repo, configDir });
|
|
58
|
+
|
|
59
|
+
async function call(segs, body) {
|
|
60
|
+
const res = await ext.server.api(new Request("http://local/" + segs.join("/"), {
|
|
61
|
+
method: "POST",
|
|
62
|
+
body: JSON.stringify(body),
|
|
63
|
+
headers: { "content-type": "application/json" },
|
|
64
|
+
}), { path: segs, method: "POST", searchParams: new URLSearchParams() });
|
|
65
|
+
return { status: res.status, body: res.body ? await res.json() : null };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
test("parseStatusZ 单元:rename TO/FROM 顺序、NUL 记录、未跟踪目录", () => {
|
|
69
|
+
const entries = parseStatusZ(" M a.txt\0R new.txt\0old.txt\0?? dir/\0");
|
|
70
|
+
assert.deepEqual(entries, [
|
|
71
|
+
{ xy: " M", path: "a.txt" },
|
|
72
|
+
{ xy: "R ", path: "new.txt" }, // -z 下 TO 在前,FROM token 被正确消费
|
|
73
|
+
{ xy: "??", path: "dir/" },
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("refs:当前分支/分支列表/脏文件数", async () => {
|
|
78
|
+
const r = await call(["git", "refs"], { cwd: repo });
|
|
79
|
+
assert.equal(r.status, 200);
|
|
80
|
+
assert.equal(r.body.current, "main");
|
|
81
|
+
assert.ok(r.body.branches.includes("main"));
|
|
82
|
+
assert.equal(r.body.dirtyCount, 0);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("branch-create:from=HEAD 建并检出预检豁免(脏文件不阻断)", async () => {
|
|
86
|
+
write("a.txt", "A2\n"); // 脏 a.txt
|
|
87
|
+
const r = await call(["git", "branch-create"], { cwd: repo, name: "dev" });
|
|
88
|
+
assert.equal(r.status, 200);
|
|
89
|
+
assert.equal(git(["symbolic-ref", "--short", "HEAD"]).trim(), "dev");
|
|
90
|
+
assert.equal(readFileSync(join(repo, "a.txt"), "utf8"), "A2\n"); // 同 commit 检出不改文件
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("checkout 预检:dirty∩affected → 409(异常文件名原样回传)", async () => {
|
|
94
|
+
write(CN_FILE, "中2\n");
|
|
95
|
+
write(NL_FILE, "n2\n");
|
|
96
|
+
git(["add", "-A"]);
|
|
97
|
+
git(["commit", "-m", "dev change weird files"]); // dev 改了 CN_FILE 与 NL_FILE
|
|
98
|
+
write(CN_FILE, "中3\n");
|
|
99
|
+
write(NL_FILE, "n3\n");
|
|
100
|
+
write("b.txt", "B1-dirty\n"); // 非冲突脏文件不进清单
|
|
101
|
+
const r = await call(["git", "checkout"], { cwd: repo, ref: "main" });
|
|
102
|
+
assert.equal(r.status, 409);
|
|
103
|
+
assert.equal(r.body.conflict, true);
|
|
104
|
+
assert.deepEqual([...r.body.files].sort(), [CN_FILE, NL_FILE].sort());
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("checkout:非冲突脏文件放行(dirty ≠ 不能切)", async () => {
|
|
108
|
+
git(["checkout", "--", CN_FILE, NL_FILE]); // 恢复为 dev 提交内容
|
|
109
|
+
const r = await call(["git", "checkout"], { cwd: repo, ref: "main" });
|
|
110
|
+
assert.equal(r.status, 200);
|
|
111
|
+
assert.equal(git(["symbolic-ref", "--short", "HEAD"]).trim(), "main");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("checkout:stash-then-switch 冲突路径打通 + stash 往返", async () => {
|
|
115
|
+
write("a.txt", "A-main-dirty\n");
|
|
116
|
+
const r = await call(["git", "checkout"], { cwd: repo, ref: "dev", mode: "stash-then-switch" });
|
|
117
|
+
assert.equal(r.status, 200);
|
|
118
|
+
assert.equal(git(["symbolic-ref", "--short", "HEAD"]).trim(), "dev");
|
|
119
|
+
const list = await call(["git", "stash", "list"], { cwd: repo });
|
|
120
|
+
assert.equal(list.body.items.length, 1);
|
|
121
|
+
// pop 干净应用须回到 stash 基底分支(main)——dev 上 a.txt 内容不同会冲突
|
|
122
|
+
assert.equal((await call(["git", "checkout"], { cwd: repo, ref: "main" })).status, 200);
|
|
123
|
+
const apply = await call(["git", "stash", "apply"], { cwd: repo, index: 0, pop: true });
|
|
124
|
+
assert.equal(apply.status, 200);
|
|
125
|
+
assert.equal(readFileSync(join(repo, "a.txt"), "utf8"), "A-main-dirty\n");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("ref/分支名注入与白名单拒绝", async () => {
|
|
129
|
+
assert.equal((await call(["git", "checkout"], { cwd: repo, ref: "--upload-pack=evil" })).status, 400);
|
|
130
|
+
assert.equal((await call(["git", "branch-create"], { cwd: repo, name: "--exec=rm -rf" })).status, 400);
|
|
131
|
+
assert.equal((await call(["git", "branch-create"], { cwd: repo, name: "a b;c" })).status, 400);
|
|
132
|
+
assert.equal((await call(["git", "checkout"], { cwd: repo, ref: "no-such-branch" })).status, 404);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("unicode 分支与 tag(合法 ref 放行)", async () => {
|
|
136
|
+
const r = await call(["git", "branch-create"], { cwd: repo, name: "分支-甲" });
|
|
137
|
+
assert.equal(r.status, 200);
|
|
138
|
+
assert.equal(git(["symbolic-ref", "--short", "HEAD"]).trim(), "分支-甲");
|
|
139
|
+
assert.equal((await call(["git", "checkout"], { cwd: repo, ref: "main" })).status, 200);
|
|
140
|
+
const refs = await call(["git", "refs"], { cwd: repo });
|
|
141
|
+
assert.ok(refs.body.branches.includes("分支-甲"));
|
|
142
|
+
assert.equal((await call(["git", "tag-create"], { cwd: repo, name: "标签-壹", annotate: true, message: "中文注解" })).status, 200);
|
|
143
|
+
const tags = await call(["git", "tags"], { cwd: repo });
|
|
144
|
+
const t = tags.body.tags.find((x) => x.name === "标签-壹");
|
|
145
|
+
assert.ok(t && t.type === "tag");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("路径穿越拒绝", async () => {
|
|
149
|
+
const r = await call(["git", "stage"], { cwd: repo, paths: ["../../etc/passwd"] });
|
|
150
|
+
assert.equal(r.status, 400);
|
|
151
|
+
assert.match(r.body.error, /越界|outside/);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("stage/commit 往返 + log", async () => {
|
|
155
|
+
write("a.txt", "A-dev-final\n");
|
|
156
|
+
assert.equal((await call(["git", "stage"], { cwd: repo, paths: ["a.txt"] })).status, 200);
|
|
157
|
+
const c = await call(["git", "commit"], { cwd: repo, message: "final a" });
|
|
158
|
+
assert.equal(c.status, 200);
|
|
159
|
+
assert.match(c.body.sha, /^[0-9a-f]{40}$/);
|
|
160
|
+
const log = await call(["git", "log"], { cwd: repo, cursor: 0 });
|
|
161
|
+
assert.ok(log.body.lines.some((l) => l.includes("final a")));
|
|
162
|
+
assert.equal(log.body.nextCursor, null);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("plan/execute:失败断点(第 2 组非法文件停,第 1 组已提交)", async () => {
|
|
166
|
+
const before = git(["rev-parse", "HEAD"]).trim();
|
|
167
|
+
write("p1.txt", "P1\n");
|
|
168
|
+
write("p2.txt", "P2\n");
|
|
169
|
+
const r = await call(["git", "plan", "execute"], {
|
|
170
|
+
cwd: repo,
|
|
171
|
+
groups: [
|
|
172
|
+
{ files: ["p1.txt"], message: "plan g1" },
|
|
173
|
+
{ files: ["nonexistent-file.txt"], message: "plan g2" },
|
|
174
|
+
],
|
|
175
|
+
});
|
|
176
|
+
assert.equal(r.body.ok, false);
|
|
177
|
+
assert.equal(r.body.failedGroup, 1);
|
|
178
|
+
assert.equal(r.body.completed, 1);
|
|
179
|
+
assert.notEqual(git(["rev-parse", "HEAD"]).trim(), before);
|
|
180
|
+
assert.ok(git(["log", "--name-only", "-1"]).includes("p1.txt"));
|
|
181
|
+
|
|
182
|
+
const rb = await call(["git", "plan", "rollback"], { cwd: repo, baseHEAD: before });
|
|
183
|
+
assert.equal(rb.status, 200);
|
|
184
|
+
assert.equal(git(["rev-parse", "HEAD"]).trim(), before);
|
|
185
|
+
assert.equal(existsSync(join(repo, "p1.txt")), true); // --mixed 保留工作区
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("stash 部分暂存(-z 路径:只存勾选文件,未勾选留工作区)", async () => {
|
|
189
|
+
write("x.txt", "X\n");
|
|
190
|
+
write("y.txt", "Y2\n");
|
|
191
|
+
const r = await call(["git", "stash", "push"], { cwd: repo, message: "partial 中文", includeUntracked: true, paths: ["x.txt"] });
|
|
192
|
+
assert.equal(r.status, 200);
|
|
193
|
+
assert.equal(existsSync(join(repo, "x.txt")), false); // 勾选的未跟踪文件已入 stash
|
|
194
|
+
assert.equal(readFileSync(join(repo, "y.txt"), "utf8"), "Y2\n"); // 未勾选留在工作区
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("stash list:unicode 消息与字段齐整", async () => {
|
|
198
|
+
const list = await call(["git", "stash", "list"], { cwd: repo });
|
|
199
|
+
assert.equal(list.status, 200);
|
|
200
|
+
assert.equal(list.body.items.length, 1);
|
|
201
|
+
assert.match(list.body.items[0].message, /partial 中文/);
|
|
202
|
+
assert.match(list.body.items[0].sha, /^[0-9a-f]{40}$/);
|
|
203
|
+
assert.ok(list.body.items[0].date.length > 10);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("stash 部分提取(条目保留)", async () => {
|
|
207
|
+
const r = await call(["git", "stash", "apply"], { cwd: repo, index: 0, paths: ["x.txt"] });
|
|
208
|
+
assert.equal(r.status, 200);
|
|
209
|
+
assert.equal(readFileSync(join(repo, "x.txt"), "utf8"), "X\n");
|
|
210
|
+
const list = await call(["git", "stash", "list"], { cwd: repo });
|
|
211
|
+
assert.equal(list.body.items.length, 1); // 提取不 drop
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("stash drop 返回 sha(gc 前可找回)", async () => {
|
|
215
|
+
const r = await call(["git", "stash", "drop"], { cwd: repo, index: 0 });
|
|
216
|
+
assert.equal(r.status, 200);
|
|
217
|
+
assert.match(r.body.sha, /^[0-9a-f]{7,40}$/);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("refs.dirtyFiles + stash/files(部分暂存/提取数据面)", async () => {
|
|
221
|
+
write("df1.txt", "D1\n");
|
|
222
|
+
write(CN_FILE, "中-df\n");
|
|
223
|
+
mkdirSync(join(repo, "dfdir"), { recursive: true });
|
|
224
|
+
writeFileSync(join(repo, "dfdir", "u.txt"), "U\n");
|
|
225
|
+
const refs = await call(["git", "refs"], { cwd: repo });
|
|
226
|
+
assert.equal(refs.status, 200);
|
|
227
|
+
const paths = refs.body.dirtyFiles.map((f) => f.path);
|
|
228
|
+
assert.ok(paths.includes("df1.txt"));
|
|
229
|
+
assert.ok(paths.includes(CN_FILE), "中文路径 -z 原样");
|
|
230
|
+
assert.ok(paths.includes("dfdir/")); // 未跟踪目录尾斜杠形态
|
|
231
|
+
// 建 stash 带 untracked → stash/files 应分别给出 tracked/untracked 清单
|
|
232
|
+
assert.equal((await call(["git", "stash", "push"], { cwd: repo, message: "files-probe", includeUntracked: true, paths: ["df1.txt", "dfdir"] })).status, 200);
|
|
233
|
+
const sf = await call(["git", "stash", "files"], { cwd: repo, index: 0 });
|
|
234
|
+
assert.equal(sf.status, 200);
|
|
235
|
+
// df1.txt 与 dfdir 均为未跟踪(-u 路径进第三父)。fixture 有前序测试残留,用包含性断言
|
|
236
|
+
assert.ok(sf.body.untracked.includes("df1.txt"));
|
|
237
|
+
assert.ok(sf.body.untracked.includes("dfdir/u.txt"));
|
|
238
|
+
assert.ok(!sf.body.untracked.includes(CN_FILE));
|
|
239
|
+
assert.equal((await call(["git", "stash", "drop"], { cwd: repo, index: 0 })).status, 200);
|
|
240
|
+
git(["checkout", "--", CN_FILE]);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("stash push 失败透传(pathspec 不匹配不再假 ok)", async () => {
|
|
244
|
+
const r = await call(["git", "stash", "push"], { cwd: repo, paths: ["nonexistent-xyz.txt"] });
|
|
245
|
+
assert.ok(r.status >= 400, "期望错误态,实际 " + r.status);
|
|
246
|
+
assert.equal(r.body.ok, false);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("discard:暂存中的新文件连 index 一起丢", async () => {
|
|
250
|
+
write("u1.txt", "U\n");
|
|
251
|
+
git(["add", "u1.txt"]); // staged new
|
|
252
|
+
const r = await call(["git", "discard"], { cwd: repo, paths: ["u1.txt"] });
|
|
253
|
+
assert.equal(r.status, 200);
|
|
254
|
+
assert.equal(existsSync(join(repo, "u1.txt")), false);
|
|
255
|
+
// fixture 有其他测试遗留的脏文件,只断言 u1 彻底消失(工作区+index)
|
|
256
|
+
assert.equal(git(["status", "--porcelain"]).includes("u1.txt"), false);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("discard:untracked 嵌套去重 + 目录整树隔离(可找回)", async () => {
|
|
260
|
+
mkdirSync(join(repo, "nd", "子目录"), { recursive: true });
|
|
261
|
+
writeFileSync(join(repo, "nd", "f.txt"), "N\n");
|
|
262
|
+
writeFileSync(join(repo, "nd", "子目录", "g.txt"), "G\n");
|
|
263
|
+
const r = await call(["git", "discard"], { cwd: repo, paths: ["nd", "nd/f.txt"] }); // 嵌套选择去重
|
|
264
|
+
assert.equal(r.status, 200);
|
|
265
|
+
assert.equal(r.body.quarantined.length, 1); // 只搬一次(nd 整树)
|
|
266
|
+
assert.equal(existsSync(join(repo, "nd")), false);
|
|
267
|
+
assert.equal(existsSync(join(r.body.quarantined[0], "子目录", "g.txt")), true);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("discard:rename 后新路径 + 异常文件名内容复原", async () => {
|
|
271
|
+
git(["mv", SP_FILE, "sp2 ace.txt"]);
|
|
272
|
+
git(["commit", "-m", "rename sp"]);
|
|
273
|
+
write("sp2 ace.txt", "s2-dirty\n");
|
|
274
|
+
write(ARROW_FILE, "arrow2-dirty\n");
|
|
275
|
+
const r = await call(["git", "discard"], { cwd: repo, paths: ["sp2 ace.txt", ARROW_FILE] });
|
|
276
|
+
assert.equal(r.status, 200);
|
|
277
|
+
assert.equal(readFileSync(join(repo, "sp2 ace.txt"), "utf8"), "s1\n");
|
|
278
|
+
assert.equal(readFileSync(join(repo, ARROW_FILE), "utf8"), "arrow1\n");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("branch-delete:-d 安全删(未合并拒绝 / 合并后可删)", async () => {
|
|
282
|
+
git(["checkout", "main"]);
|
|
283
|
+
assert.equal((await call(["git", "branch-create"], { cwd: repo, name: "tmp" })).status, 200);
|
|
284
|
+
write("tmpfile.txt", "T\n");
|
|
285
|
+
git(["add", "-A"]);
|
|
286
|
+
git(["commit", "-m", "tmp ahead"]);
|
|
287
|
+
const failR = await call(["git", "branch-delete"], { cwd: repo, name: "tmp" });
|
|
288
|
+
assert.equal(failR.status, 500);
|
|
289
|
+
assert.match(failR.body.error, /删除失败/);
|
|
290
|
+
git(["checkout", "main"]);
|
|
291
|
+
git(["merge", "tmp"]);
|
|
292
|
+
assert.equal((await call(["git", "branch-delete"], { cwd: repo, name: "tmp" })).status, 200);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("log 分页:--graph 无 --skip 语义下翻页完整(55 空提交)", async () => {
|
|
296
|
+
for (let i = 1; i <= 55; i++) {
|
|
297
|
+
git(["commit", "--allow-empty", "-m", "c" + String(i).padStart(2, "0")]);
|
|
298
|
+
}
|
|
299
|
+
const total = Number(git(["rev-list", "--count", "HEAD"]).trim());
|
|
300
|
+
const p1 = await call(["git", "log"], { cwd: repo, cursor: 0 });
|
|
301
|
+
assert.equal(p1.status, 200);
|
|
302
|
+
assert.equal(p1.body.lines.length, 50);
|
|
303
|
+
assert.equal(p1.body.nextCursor, 50);
|
|
304
|
+
assert.ok(p1.body.lines.some((l) => l.includes("c55"))); // date-order 最新在前
|
|
305
|
+
const p2 = await call(["git", "log"], { cwd: repo, cursor: 50 });
|
|
306
|
+
assert.equal(p2.body.lines.length, total - 50);
|
|
307
|
+
assert.equal(p2.body.nextCursor, null);
|
|
308
|
+
assert.ok(p2.body.lines.some((l) => l.includes("c01"))); // 最老一页包含最早提交
|
|
309
|
+
const tail = await call(["git", "log"], { cwd: repo, cursor: total + 100 });
|
|
310
|
+
assert.equal(tail.body.lines.length, 0); // 越界空页不炸
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("审计 jsonl 已写入(state 分层路径)", async () => {
|
|
314
|
+
const auditPath = join(tmp, "ext", "config", "state", "aiti-git-ops", "git-ops.jsonl");
|
|
315
|
+
assert.equal(existsSync(auditPath), true);
|
|
316
|
+
const lines = readFileSync(auditPath, "utf8").trim().split("\n");
|
|
317
|
+
assert.ok(lines.length >= 5);
|
|
318
|
+
for (const l of lines) {
|
|
319
|
+
const e = JSON.parse(l);
|
|
320
|
+
assert.ok(e.ts && e.cmd);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
test("非 git 目录 / 非法 cwd 结构化错误", async () => {
|
|
325
|
+
const notRepo = mkdtempSync(join(tmpdir(), "git-ops-norepo-"));
|
|
326
|
+
const r = await call(["git", "refs"], { cwd: notRepo });
|
|
327
|
+
assert.equal(r.status, 400);
|
|
328
|
+
assert.match(r.body.error, /不是 git 仓库/);
|
|
329
|
+
const rel = await call(["git", "refs"], { cwd: "relative/path" });
|
|
330
|
+
assert.equal(rel.status, 400);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("refs.head:plan baseHEAD 数据面", async () => {
|
|
334
|
+
const r = await call(["git", "refs"], { cwd: repo });
|
|
335
|
+
assert.equal(r.status, 200);
|
|
336
|
+
assert.match(r.body.head, /^[0-9a-f]{40}$/);
|
|
337
|
+
assert.equal(r.body.head, git(["rev-parse", "HEAD"]).trim());
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// ── commit 详情卡(docs/aiti-git-ops-commit-card-plan.md):commit-show / commit-diff / cherry-pick ──
|
|
341
|
+
|
|
342
|
+
test("commit-show:普通 commit 元信息 + 文件清单(状态/增删行数,异常文件名原样)", async () => {
|
|
343
|
+
write(CN_FILE, "中1\n中2\n中3\n");
|
|
344
|
+
write("new-shown.txt", "N1\nN2\n");
|
|
345
|
+
git(["add", "-A"]);
|
|
346
|
+
git(["commit", "-m", "show-me: 元信息与清单"]);
|
|
347
|
+
const r = await call(["git", "commit-show"], { cwd: repo, sha: "HEAD" });
|
|
348
|
+
assert.equal(r.status, 200);
|
|
349
|
+
assert.match(r.body.sha, /^[0-9a-f]{40}$/);
|
|
350
|
+
assert.equal(r.body.subject, "show-me: 元信息与清单");
|
|
351
|
+
assert.equal(r.body.author, "t");
|
|
352
|
+
assert.equal(r.body.merge, false);
|
|
353
|
+
assert.equal(r.body.parents.length, 1);
|
|
354
|
+
const cn = r.body.files.find((f) => f.path === CN_FILE);
|
|
355
|
+
assert.equal(cn.status, "M");
|
|
356
|
+
assert.equal(cn.additions, 2); // 中1 → 中1/中2/中3 净 +2
|
|
357
|
+
const nf = r.body.files.find((f) => f.path === "new-shown.txt");
|
|
358
|
+
assert.equal(nf.status, "A");
|
|
359
|
+
assert.deepEqual([nf.additions, nf.deletions], [2, 0]);
|
|
360
|
+
assert.equal(r.body.additions, 4);
|
|
361
|
+
// 短 sha 也可作 ref(图谱行即短 sha)
|
|
362
|
+
const short = await call(["git", "commit-show"], { cwd: repo, sha: r.body.sha.slice(0, 7) });
|
|
363
|
+
assert.equal(short.status, 200);
|
|
364
|
+
assert.equal(short.body.sha, r.body.sha);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test("commit-show:rename(-z token 序钉死:old 在前 new 在后)", async () => {
|
|
368
|
+
write("r1.txt", "R\n");
|
|
369
|
+
git(["add", "-A"]);
|
|
370
|
+
git(["commit", "-m", "rename-src"]);
|
|
371
|
+
git(["mv", "r1.txt", "r2 后.txt"]);
|
|
372
|
+
git(["commit", "-m", "rename-dst"]);
|
|
373
|
+
const r = await call(["git", "commit-show"], { cwd: repo, sha: "HEAD" });
|
|
374
|
+
assert.equal(r.status, 200);
|
|
375
|
+
assert.equal(r.body.files.length, 1);
|
|
376
|
+
assert.equal(r.body.files[0].path, "r2 后.txt");
|
|
377
|
+
assert.equal(r.body.files[0].oldPath, "r1.txt");
|
|
378
|
+
assert.equal(r.body.files[0].status, "R");
|
|
379
|
+
assert.deepEqual([r.body.files[0].additions, r.body.files[0].deletions], [0, 0]);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test("commit-show:root commit 对空树(全文件 status=A)+ merge 取第一父 diff", async () => {
|
|
383
|
+
const root = git(["rev-list", "--max-parents=0", "HEAD"]).trim().split("\n")[0];
|
|
384
|
+
const rr = await call(["git", "commit-show"], { cwd: repo, sha: root });
|
|
385
|
+
assert.equal(rr.status, 200);
|
|
386
|
+
assert.ok(rr.body.files.some((f) => f.path === "a.txt" && f.status === "A"));
|
|
387
|
+
// merge:side 分支加 side-cp.txt,主干加 main-cp.txt,--no-ff 合并 → 详情只含 main-cp.txt
|
|
388
|
+
git(["checkout", "-b", "show-side"]);
|
|
389
|
+
write("side-cp.txt", "S\n");
|
|
390
|
+
git(["add", "-A"]);
|
|
391
|
+
git(["commit", "-m", "side commit"]);
|
|
392
|
+
git(["checkout", "main"]);
|
|
393
|
+
write("main-cp.txt", "M\n");
|
|
394
|
+
git(["add", "-A"]);
|
|
395
|
+
git(["commit", "-m", "main commit"]);
|
|
396
|
+
git(["merge", "--no-ff", "show-side", "-m", "merge show-side"]);
|
|
397
|
+
const mr = await call(["git", "commit-show"], { cwd: repo, sha: "HEAD" });
|
|
398
|
+
assert.equal(mr.status, 200);
|
|
399
|
+
assert.equal(mr.body.merge, true);
|
|
400
|
+
assert.equal(mr.body.parents.length, 2);
|
|
401
|
+
// 第一父 diff = 相对 main 前一点(已含 main-cp.txt)的净变化 = 被并入的 side-cp.txt(GitHub 同语义)
|
|
402
|
+
assert.deepEqual(mr.body.files.map((f) => f.path), ["side-cp.txt"]);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
test("commit-diff:单文件 patch + rename 双路径", async () => {
|
|
406
|
+
const sha = git(["rev-parse", "HEAD~1"]).trim(); // main commit
|
|
407
|
+
const r = await call(["git", "commit-diff"], { cwd: repo, sha, paths: ["main-cp.txt"] });
|
|
408
|
+
assert.equal(r.status, 200);
|
|
409
|
+
assert.equal(r.body.truncated, false);
|
|
410
|
+
assert.ok(r.body.patch.includes("+M\n"));
|
|
411
|
+
assert.ok(r.body.patch.startsWith("diff --git"));
|
|
412
|
+
// rename commit:传 [新, 旧] 双路径(首父链 merge→main→rename-dst→rename-src,HEAD~2=rename-dst)
|
|
413
|
+
const rsha = git(["rev-parse", "HEAD~2"]).trim(); // rename-dst
|
|
414
|
+
const rr = await call(["git", "commit-diff"], { cwd: repo, sha: rsha, paths: ["r2 后.txt", "r1.txt"] });
|
|
415
|
+
assert.equal(rr.status, 200);
|
|
416
|
+
assert.ok(rr.body.patch.includes("rename from"));
|
|
417
|
+
assert.ok(rr.body.patch.includes("r1.txt"));
|
|
418
|
+
// 路径越界拒绝
|
|
419
|
+
const bad = await call(["git", "commit-diff"], { cwd: repo, sha, paths: ["../../etc/passwd"] });
|
|
420
|
+
assert.equal(bad.status, 400);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
test("commit-diff:大 diff 行边界截断 + truncated 标志", async () => {
|
|
424
|
+
const big = Array.from({ length: 40_000 }, (_, i) => `line-${String(i).padStart(5, "0")}\n`).join("");
|
|
425
|
+
write("big-diff.txt", big);
|
|
426
|
+
git(["add", "-A"]);
|
|
427
|
+
git(["commit", "-m", "big diff"]);
|
|
428
|
+
const r = await call(["git", "commit-diff"], { cwd: repo, sha: "HEAD", paths: ["big-diff.txt"] });
|
|
429
|
+
assert.equal(r.status, 200);
|
|
430
|
+
assert.equal(r.body.truncated, true);
|
|
431
|
+
assert.ok(r.body.patch.includes("(已截断)"));
|
|
432
|
+
assert.ok(r.body.patch.length < 500 * 1024);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
test("cherry-pick:跨分支摘取成功", async () => {
|
|
436
|
+
git(["checkout", "-b", "cp-src"]);
|
|
437
|
+
write("a.txt", "CP-1\n");
|
|
438
|
+
git(["add", "-A"]);
|
|
439
|
+
git(["commit", "-m", "cp: pick me"]);
|
|
440
|
+
const pickSha = git(["rev-parse", "HEAD"]).trim();
|
|
441
|
+
git(["checkout", "main"]);
|
|
442
|
+
const r = await call(["git", "cherry-pick"], { cwd: repo, sha: pickSha });
|
|
443
|
+
assert.equal(r.status, 200);
|
|
444
|
+
assert.equal(r.body.sha, pickSha);
|
|
445
|
+
assert.equal(readFileSync(join(repo, "a.txt"), "utf8"), "CP-1\n");
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
test("cherry-pick:冲突 409 + 中间态文件清单 + abort 复原", async () => {
|
|
449
|
+
// 两分支从同一点各自改 a.txt 首行 → 摘取必冲突
|
|
450
|
+
git(["checkout", "-b", "cp-a", "main"]);
|
|
451
|
+
write("a.txt", "A-side\nshared\n");
|
|
452
|
+
git(["add", "-A"]);
|
|
453
|
+
git(["commit", "-m", "cp-a side"]);
|
|
454
|
+
const aSha = git(["rev-parse", "HEAD"]).trim();
|
|
455
|
+
git(["checkout", "-b", "cp-b", "main"]);
|
|
456
|
+
write("a.txt", "B-side\nshared\n");
|
|
457
|
+
git(["add", "-A"]);
|
|
458
|
+
git(["commit", "-m", "cp-b side"]);
|
|
459
|
+
git(["checkout", "cp-a"]);
|
|
460
|
+
const r = await call(["git", "cherry-pick"], { cwd: repo, sha: aSha === git(["rev-parse", "HEAD"]).trim() ? "HEAD~1" : "cp-b" });
|
|
461
|
+
assert.equal(r.status, 409);
|
|
462
|
+
assert.equal(r.body.conflict, true);
|
|
463
|
+
assert.ok(r.body.files.includes("a.txt"));
|
|
464
|
+
assert.equal(existsSync(join(repo, ".git", "CHERRY_PICK_HEAD")), true); // 中间态在位
|
|
465
|
+
const ab = await call(["git", "cherry-pick"], { cwd: repo, sha: "", abort: true });
|
|
466
|
+
assert.equal(ab.status, 200);
|
|
467
|
+
assert.equal(existsSync(join(repo, ".git", "CHERRY_PICK_HEAD")), false);
|
|
468
|
+
assert.equal(readFileSync(join(repo, "a.txt"), "utf8"), "A-side\nshared\n"); // 内容复原
|
|
469
|
+
const st = await call(["git", "refs"], { cwd: repo });
|
|
470
|
+
assert.equal(st.body.dirtyCount, 0); // 工作区干净(走出中间态)
|
|
471
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* deploy 脚本共享「部署状态 commit」助手(pi-web-ext 各扩展 tools/deploy.mjs 统一用)。
|
|
3
|
+
*
|
|
4
|
+
* 背景(2026-08-30 排障,session 0cc6ad47 op264):重复部署同内容时
|
|
5
|
+
* `git add -A && git commit` 以 "nothing to commit" exit 1,而 stdio:"ignore" 吞掉
|
|
6
|
+
* stderr,输出「提交失败(忽略):Command failed: …」假报警(msg-smart 实证);
|
|
7
|
+
* 另有 --allow-empty 族每次部署都造空 commit 噪音。本助手统一三态语义:
|
|
8
|
+
* committed → 有暂存变更,已提交
|
|
9
|
+
* clean → 无变更,跳过(不算失败)
|
|
10
|
+
* failed → git 真失败(带 stderr 首行,不阻断部署——部署本体永远优先)
|
|
11
|
+
*
|
|
12
|
+
* 用法:
|
|
13
|
+
* import { deployCommit } from "../../_shared/deploy-commit.mjs";
|
|
14
|
+
* deployCommit({ repo: extRepo, name: "aiti-msg-smart", paths: ["aiti-msg-smart"],
|
|
15
|
+
* log: (m) => console.log("[deploy] " + m) });
|
|
16
|
+
*
|
|
17
|
+
* 红线:本助手永不 throw、不触碰部署产物路径之外的任何东西;只对 <repo> 做
|
|
18
|
+
* add/diff/commit(默认 --no-verify,extRepo 无 hooks,防未来误挂)。
|
|
19
|
+
*/
|
|
20
|
+
import { execFileSync } from "node:child_process";
|
|
21
|
+
|
|
22
|
+
export function deployCommit({ repo, name, paths = null, extraMsg = "", ident = false, log = console.log }) {
|
|
23
|
+
const git = (args) =>
|
|
24
|
+
execFileSync("git", args, { cwd: repo, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
25
|
+
const firstLine = (s) => String(s || "").trim().split("\n")[0];
|
|
26
|
+
try {
|
|
27
|
+
// 暂存:默认只收本扩展子目录(与 add <ext> 行为对齐;避免把别的扩展的
|
|
28
|
+
// 未提交改动卷进本扩展的部署 commit);paths=null 时退回 add -A 全仓。
|
|
29
|
+
git(["add", ...(paths && paths.length ? ["--", ...paths] : ["-A"])]);
|
|
30
|
+
let dirty;
|
|
31
|
+
try {
|
|
32
|
+
git(["diff", "--cached", "--quiet"]);
|
|
33
|
+
dirty = false; // exit 0 = 暂存区与 HEAD 一致
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e.status !== 1) throw e; // 1 = 有变更;其他才是真错误
|
|
36
|
+
dirty = true;
|
|
37
|
+
}
|
|
38
|
+
if (!dirty) {
|
|
39
|
+
log("扩展仓库无变化(跳过提交)");
|
|
40
|
+
return { status: "clean" };
|
|
41
|
+
}
|
|
42
|
+
const stamp = new Date().toISOString().replace("T", " ").slice(0, 19);
|
|
43
|
+
const msg = `[deploy] ${name} · ${stamp}${extraMsg ? " · " + extraMsg : ""}`;
|
|
44
|
+
git([...(ident ? ["-c", "user.name=deploy", "-c", "user.email=deploy@local"] : []), "commit", "--no-verify", "-m", msg]);
|
|
45
|
+
log("扩展仓库已提交:" + msg);
|
|
46
|
+
return { status: "committed", msg };
|
|
47
|
+
} catch (e) {
|
|
48
|
+
const reason = firstLine(e.stderr) || firstLine(e.message) || "git error";
|
|
49
|
+
log("提交失败(忽略,不影响部署):" + reason);
|
|
50
|
+
return { status: "failed", reason };
|
|
51
|
+
}
|
|
52
|
+
}
|