x7-code-line 0.3.0 → 0.4.0
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/README.md +412 -398
- package/bin/x7-code-line.js +8 -8
- package/hooks/git/commit-msg +4 -4
- package/hooks/git/pre-commit +4 -4
- package/package.json +30 -30
- package/src/agent-hooks.js +59 -28
- package/src/cache.js +128 -69
- package/src/cli.js +153 -108
- package/src/diff-lines.js +115 -115
- package/src/git-trailer.js +46 -46
- package/src/index.js +7 -7
- package/src/install.js +268 -267
- package/src/postinstall.js +13 -13
- package/templates/.codex/hooks.json +30 -14
- package/templates/.cursor/hooks.json +17 -14
package/README.md
CHANGED
|
@@ -1,407 +1,421 @@
|
|
|
1
|
-
# x7-code-line
|
|
2
|
-
|
|
3
|
-
`x7-code-line` 是一个用于安装 Cursor、Codex 和 Git hooks 的 npm 模块。它用于在 agent 工作流中区分普通用户已有改动和 AI 生成改动,并在提交信息中自动追加 AI 行数 trailer。
|
|
4
|
-
|
|
5
|
-
## 功能概览
|
|
6
|
-
|
|
7
|
-
- 安装 Cursor 项目 hooks:写入当前安装目录的 `.cursor/hooks.json`。
|
|
8
|
-
- 安装 Codex 项目 hooks:写入当前安装目录的 `.codex/hooks.json`。
|
|
9
|
-
- 安装 Git hooks:为目标项目写入 `.git/hooks/pre-commit` 和 `.git/hooks/commit-msg`。
|
|
10
|
-
- 初始化中心缓存目录:写入当前安装目录的 `.x7-code-line/`。
|
|
11
|
-
- 支持多项目:多项目配置只用于替换目标项目的 Git hooks,以及在 `prompt-submit` / `stop` 时遍历读取这些项目的 `git diff`。
|
|
12
|
-
- 记录普通 diff 行:prompt 提交时读取 `git diff`,把尚未进入 AI 缓存的 diff 行写入普通缓存。
|
|
13
|
-
- 记录 AI diff 行:agent 结束时再次读取 `git diff`,把不属于普通缓存的 diff 行写入 AI 缓存。
|
|
14
|
-
- 计算提交 AI 行数:`pre-commit` 对比当前仓库 staged diff 和中心 AI 缓存,计算本次提交真实包含的 AI diff 行数。
|
|
15
|
-
- 写入 commit trailer:`commit-msg` 追加或替换 `x7-ai-lines: <数量>`。
|
|
16
|
-
|
|
17
|
-
## 安装行为
|
|
18
|
-
|
|
19
|
-
作为 npm 依赖安装时,`postinstall` 脚本使用 `INIT_CWD` 作为当前安装目录,也就是执行 `npm install` 时所在的项目目录。
|
|
20
|
-
|
|
21
|
-
如果只是全局安装:
|
|
22
|
-
|
|
23
|
-
```sh
|
|
24
|
-
npm install -g x7-code-line
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
此时只安装 CLI,不会自动为任何项目写入 hooks。全局安装完成后,需要手动执行:
|
|
28
|
-
|
|
29
|
-
```sh
|
|
30
|
-
x7-code-line install --dir <absolute-path>
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
只有在 `X7_CODE_LINE_DIRS` 或 `X7_CODE_LINE_CONFIG` 明确提供目标目录时,`postinstall` 才会继续执行项目安装逻辑。
|
|
34
|
-
|
|
35
|
-
当前安装目录会创建或覆盖:
|
|
36
|
-
|
|
37
|
-
```text
|
|
38
|
-
.x7-code-line/install.json
|
|
39
|
-
.x7-code-line/projects.json
|
|
40
|
-
.x7-code-line/ai-diff-line-ids.json
|
|
41
|
-
.x7-code-line/normal-diff-line-ids.json
|
|
42
|
-
.cursor/hooks.json
|
|
43
|
-
.codex/hooks.json
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
如果目标项目存在 `.git`,会创建或覆盖:
|
|
47
|
-
|
|
48
|
-
```text
|
|
49
|
-
.git/hooks/pre-commit
|
|
50
|
-
.git/hooks/commit-msg
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
多项目配置只影响两类行为:
|
|
54
|
-
|
|
55
|
-
- 为配置中的项目目录替换 `.git/hooks/pre-commit` 和 `.git/hooks/commit-msg`。
|
|
56
|
-
- 执行 `prompt-submit` / `stop` 时遍历配置中的项目目录读取 `git diff`。
|
|
57
|
-
|
|
58
|
-
`.cursor/hooks.json`、`.codex/hooks.json` 和 `.x7-code-line/` 只会写入当前安装目录,不会写入每个多项目目标目录。
|
|
59
|
-
|
|
60
|
-
## Cursor Hooks
|
|
61
|
-
|
|
62
|
-
Cursor 模板写入 `.cursor/hooks.json`:
|
|
63
|
-
|
|
64
|
-
```json
|
|
65
|
-
{
|
|
66
|
-
"hooks": {
|
|
67
|
-
"beforeSubmitPrompt": [
|
|
68
|
-
{
|
|
69
|
-
"command": "npx --no-install x7-code-line prompt-submit"
|
|
70
|
-
}
|
|
71
|
-
],
|
|
72
|
-
"stop": [
|
|
73
|
-
{
|
|
74
|
-
"command": "npx --no-install x7-code-line stop"
|
|
75
|
-
}
|
|
76
|
-
]
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## Codex Hooks
|
|
82
|
-
|
|
83
|
-
Codex 模板写入 `.codex/hooks.json`:
|
|
84
|
-
|
|
85
|
-
```json
|
|
86
|
-
{
|
|
87
|
-
"hooks": {
|
|
88
|
-
"UserPromptSubmit": [
|
|
89
|
-
{
|
|
90
|
-
"command": "npx --no-install x7-code-line prompt-submit"
|
|
91
|
-
}
|
|
92
|
-
],
|
|
93
|
-
"Stop": [
|
|
94
|
-
{
|
|
95
|
-
"command": "npx --no-install x7-code-line stop"
|
|
96
|
-
}
|
|
97
|
-
]
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
## Diff 行缓存
|
|
103
|
-
|
|
104
|
-
`prompt-submit` 会进入每个已缓存的项目目录,执行:
|
|
105
|
-
|
|
106
|
-
```sh
|
|
107
|
-
git diff --no-ext-diff --unified=0 --no-color
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
然后对每个变更行计算 ID。ID 由以下信息生成:
|
|
111
|
-
|
|
112
|
-
- 项目绝对路径
|
|
113
|
-
- 文件路径
|
|
114
|
-
- 变更类型:`add` 或 `delete`
|
|
115
|
-
- 行号
|
|
116
|
-
- 行内容
|
|
117
|
-
- 该文件当前的修改时间
|
|
118
|
-
|
|
119
|
-
如果该 ID 不在 AI diff 缓存中,就写入普通 diff 缓存:
|
|
120
|
-
|
|
121
|
-
```text
|
|
122
|
-
.x7-code-line/normal-diff-line-ids.json
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
`stop` 会再次读取每个项目的 `git diff` 并计算行 ID。如果某个 ID 不在普通 diff 缓存中,就写入 AI diff 缓存:
|
|
126
|
-
|
|
127
|
-
```text
|
|
128
|
-
.x7-code-line/ai-diff-line-ids.json
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
缓存按项目绝对路径分组,结构类似:
|
|
132
|
-
|
|
133
|
-
```json
|
|
134
|
-
{
|
|
135
|
-
"projects": {
|
|
136
|
-
"/path/to/repo-a": ["line-id-1"],
|
|
137
|
-
"/path/to/repo-b": ["line-id-2"]
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
## Git Hooks
|
|
143
|
-
|
|
144
|
-
安装器会生成 shell hook,并写入中心缓存目录:
|
|
145
|
-
|
|
146
|
-
```sh
|
|
147
|
-
X7_CODE_LINE_BASE='<当前安装目录>'
|
|
148
|
-
export X7_CODE_LINE_BASE
|
|
149
|
-
npx --no-install x7-code-line git-pre-commit
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
`pre-commit` 会读取当前仓库的 staged diff:
|
|
153
|
-
|
|
154
|
-
```sh
|
|
155
|
-
git diff --cached --no-ext-diff --unified=0 --no-color
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
然后使用当前仓库绝对路径从中心缓存中取出对应项目的 AI diff 行 ID,计算本次提交真正包含的 AI diff 行数,并写入:
|
|
159
|
-
|
|
160
|
-
```text
|
|
161
|
-
.x7-code-line/pending-commit.json
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
计算完成后,会清除当前仓库在以下两份中心缓存中的条目:
|
|
165
|
-
|
|
166
|
-
```text
|
|
167
|
-
.x7-code-line/ai-diff-line-ids.json
|
|
168
|
-
.x7-code-line/normal-diff-line-ids.json
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
其他项目的缓存不会被修改。
|
|
172
|
-
|
|
173
|
-
`commit-msg` 会读取该结果,并追加或替换 trailer:
|
|
174
|
-
|
|
175
|
-
```text
|
|
176
|
-
x7-ai-lines: 3
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
如果没有命中 AI diff 行,则写入:
|
|
180
|
-
|
|
181
|
-
```text
|
|
182
|
-
x7-ai-lines: 0
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
## 多项目安装
|
|
186
|
-
|
|
187
|
-
`install` 至少需要传入一个目录;0 目录场景会直接失败。
|
|
188
|
-
|
|
189
|
-
默认要求传入绝对路径:
|
|
190
|
-
|
|
191
|
-
```sh
|
|
192
|
-
npm install -g x7-code-line --dir /abs/path/repo-a --dir /abs/path/repo-b
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
例如,将 `x7-code-line` 安装在 C 盘目录,但指定 D 盘的 Git 项目:
|
|
196
|
-
|
|
197
|
-
```powershell
|
|
198
|
-
cd C:\x7-tools\x7-code-line-host
|
|
199
|
-
npm install -g x7-code-line --dir D:\work\repo-a --dir D:\work\repo-b
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
如果确实需要解析相对路径,必须显式传 `--relative`:
|
|
203
|
-
|
|
204
|
-
```sh
|
|
205
|
-
npm install -g x7-code-line --relative --dir ../repo-a --dir ../repo-b
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
此时相对路径基于当前执行 `install` 命令的目录解析。
|
|
209
|
-
|
|
210
|
-
也可以在安装时通过环境变量指定多个目标目录。
|
|
211
|
-
|
|
212
|
-
如果通过环境变量或配置文件传相对路径,需要同时设置 `X7_CODE_LINE_RELATIVE_PATHS=1`。
|
|
213
|
-
|
|
214
|
-
macOS / Linux 示例:
|
|
215
|
-
|
|
216
|
-
```sh
|
|
217
|
-
X7_CODE_LINE_RELATIVE_PATHS=1 X7_CODE_LINE_DIRS="../repo-a:../repo-b" npm install -g x7-code-line
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Windows 使用 `;` 作为目录分隔符:
|
|
221
|
-
|
|
222
|
-
```powershell
|
|
223
|
-
$env:X7_CODE_LINE_RELATIVE_PATHS = "1"
|
|
224
|
-
$env:X7_CODE_LINE_DIRS = "../repo-a;../repo-b"
|
|
225
|
-
npm install -g x7-code-line
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
## 配置文件
|
|
229
|
-
|
|
230
|
-
可以通过 `--config` 或 `X7_CODE_LINE_CONFIG` 指定 JSON 配置文件:
|
|
231
|
-
|
|
232
|
-
```json
|
|
233
|
-
{
|
|
234
|
-
"dirs": ["../repo-a", "../repo-b"],
|
|
235
|
-
"gitDirs": [".git"]
|
|
236
|
-
}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
字段说明:
|
|
240
|
-
|
|
241
|
-
- `dirs`:目标项目目录列表。
|
|
242
|
-
- `gitDirs`:相对于每个目标项目目录的 Git 目录列表。
|
|
243
|
-
|
|
244
|
-
## macOS 适配说明
|
|
245
|
-
|
|
246
|
-
macOS 下整体可以直接使用,但需要注意以下几点:
|
|
247
|
-
|
|
248
|
-
- Git hooks 使用 `#!/bin/sh`,macOS 自带 `/bin/sh`,无需额外安装 shell。
|
|
249
|
-
- 安装器会对生成的 hook 文件执行 `chmod 755`。如果你手动复制 hook 文件,也需要执行:
|
|
250
|
-
|
|
251
|
-
```sh
|
|
252
|
-
chmod +x .git/hooks/pre-commit .git/hooks/commit-msg
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
- macOS / Linux 的多项目环境变量使用 `:` 分隔目录;不要使用 Windows 的 `;`。
|
|
256
|
-
- 如果 Cursor 或 Codex 不是从终端启动,GUI 进程可能拿不到你的 shell `PATH`,导致 hook 中的 `npx` 找不到。遇到这种情况,优先从终端启动 Cursor/Codex,或确保 Node.js / npm 的安装路径已经进入 GUI 应用可见的 `PATH`。
|
|
257
|
-
- 如果项目路径包含空格,安装器生成的 Git hook 会对 `X7_CODE_LINE_BASE` 做 shell quote,通常可以正常工作。
|
|
258
|
-
- 使用 nvm、fnm、asdf 等 Node 版本管理器时,macOS GUI 应用经常不会加载对应初始化脚本。建议在运行 hooks 前确认:
|
|
259
|
-
|
|
260
|
-
```sh
|
|
261
|
-
which node
|
|
262
|
-
which npm
|
|
263
|
-
which npx
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
如果这些命令在 Cursor/Codex hook 环境中不可用,需要调整启动方式或 PATH。
|
|
267
|
-
|
|
268
|
-
## Windows 适配说明
|
|
269
|
-
|
|
270
|
-
- Git for Windows 会用 Git Bash 执行 `.git/hooks/*`,因此 `#!/bin/sh` hook 可以运行。
|
|
271
|
-
- PowerShell 下设置多项目环境变量时使用 `$env:X7_CODE_LINE_DIRS`。
|
|
272
|
-
- Windows 目录分隔符使用 `;`,例如:
|
|
273
|
-
|
|
274
|
-
```powershell
|
|
275
|
-
$env:X7_CODE_LINE_DIRS = "D:\repo-a;D:\repo-b"
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
## 增量添加目录
|
|
279
|
-
|
|
280
|
-
在已经完成安装的当前安装目录下,可以使用 `addDir` 增量添加新的项目目录:
|
|
281
|
-
|
|
282
|
-
```sh
|
|
283
|
-
x7-code-line addDir --dir /abs/path/repo-c
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
这个命令只做两件事:
|
|
287
|
-
|
|
288
|
-
- 把新目录追加到当前安装目录 `.x7-code-line/projects.json`
|
|
289
|
-
- 为新目录安装 `.git/hooks/pre-commit` 和 `.git/hooks/commit-msg`
|
|
290
|
-
|
|
291
|
-
它不会重写当前安装目录的 `.cursor/hooks.json`、`.codex/hooks.json`,也不会重写已有项目集合。
|
|
292
|
-
|
|
1
|
+
# x7-code-line
|
|
2
|
+
|
|
3
|
+
`x7-code-line` 是一个用于安装 Cursor、Codex 和 Git hooks 的 npm 模块。它用于在 agent 工作流中区分普通用户已有改动和 AI 生成改动,并在提交信息中自动追加 AI 行数 trailer。
|
|
4
|
+
|
|
5
|
+
## 功能概览
|
|
6
|
+
|
|
7
|
+
- 安装 Cursor 项目 hooks:写入当前安装目录的 `.cursor/hooks.json`。
|
|
8
|
+
- 安装 Codex 项目 hooks:写入当前安装目录的 `.codex/hooks.json`。
|
|
9
|
+
- 安装 Git hooks:为目标项目写入 `.git/hooks/pre-commit` 和 `.git/hooks/commit-msg`。
|
|
10
|
+
- 初始化中心缓存目录:写入当前安装目录的 `.x7-code-line/`。
|
|
11
|
+
- 支持多项目:多项目配置只用于替换目标项目的 Git hooks,以及在 `prompt-submit` / `stop` 时遍历读取这些项目的 `git diff`。
|
|
12
|
+
- 记录普通 diff 行:prompt 提交时读取 `git diff`,把尚未进入 AI 缓存的 diff 行写入普通缓存。
|
|
13
|
+
- 记录 AI diff 行:agent 结束时再次读取 `git diff`,把不属于普通缓存的 diff 行写入 AI 缓存。
|
|
14
|
+
- 计算提交 AI 行数:`pre-commit` 对比当前仓库 staged diff 和中心 AI 缓存,计算本次提交真实包含的 AI diff 行数。
|
|
15
|
+
- 写入 commit trailer:`commit-msg` 追加或替换 `x7-ai-lines: <数量>`。
|
|
16
|
+
|
|
17
|
+
## 安装行为
|
|
18
|
+
|
|
19
|
+
作为 npm 依赖安装时,`postinstall` 脚本使用 `INIT_CWD` 作为当前安装目录,也就是执行 `npm install` 时所在的项目目录。
|
|
20
|
+
|
|
21
|
+
如果只是全局安装:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install -g x7-code-line
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
此时只安装 CLI,不会自动为任何项目写入 hooks。全局安装完成后,需要手动执行:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
x7-code-line install --dir <absolute-path>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
只有在 `X7_CODE_LINE_DIRS` 或 `X7_CODE_LINE_CONFIG` 明确提供目标目录时,`postinstall` 才会继续执行项目安装逻辑。
|
|
34
|
+
|
|
35
|
+
当前安装目录会创建或覆盖:
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
.x7-code-line/install.json
|
|
39
|
+
.x7-code-line/projects.json
|
|
40
|
+
.x7-code-line/ai-diff-line-ids.json
|
|
41
|
+
.x7-code-line/normal-diff-line-ids.json
|
|
42
|
+
.cursor/hooks.json
|
|
43
|
+
.codex/hooks.json
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
如果目标项目存在 `.git`,会创建或覆盖:
|
|
47
|
+
|
|
48
|
+
```text
|
|
49
|
+
.git/hooks/pre-commit
|
|
50
|
+
.git/hooks/commit-msg
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
多项目配置只影响两类行为:
|
|
54
|
+
|
|
55
|
+
- 为配置中的项目目录替换 `.git/hooks/pre-commit` 和 `.git/hooks/commit-msg`。
|
|
56
|
+
- 执行 `prompt-submit` / `stop` 时遍历配置中的项目目录读取 `git diff`。
|
|
57
|
+
|
|
58
|
+
`.cursor/hooks.json`、`.codex/hooks.json` 和 `.x7-code-line/` 只会写入当前安装目录,不会写入每个多项目目标目录。
|
|
59
|
+
|
|
60
|
+
## Cursor Hooks
|
|
61
|
+
|
|
62
|
+
Cursor 模板写入 `.cursor/hooks.json`:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"hooks": {
|
|
67
|
+
"beforeSubmitPrompt": [
|
|
68
|
+
{
|
|
69
|
+
"command": "npx --no-install x7-code-line prompt-submit"
|
|
70
|
+
}
|
|
71
|
+
],
|
|
72
|
+
"stop": [
|
|
73
|
+
{
|
|
74
|
+
"command": "npx --no-install x7-code-line stop"
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Codex Hooks
|
|
82
|
+
|
|
83
|
+
Codex 模板写入 `.codex/hooks.json`:
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"hooks": {
|
|
88
|
+
"UserPromptSubmit": [
|
|
89
|
+
{
|
|
90
|
+
"command": "npx --no-install x7-code-line prompt-submit"
|
|
91
|
+
}
|
|
92
|
+
],
|
|
93
|
+
"Stop": [
|
|
94
|
+
{
|
|
95
|
+
"command": "npx --no-install x7-code-line stop"
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Diff 行缓存
|
|
103
|
+
|
|
104
|
+
`prompt-submit` 会进入每个已缓存的项目目录,执行:
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
git diff --no-ext-diff --unified=0 --no-color
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
然后对每个变更行计算 ID。ID 由以下信息生成:
|
|
111
|
+
|
|
112
|
+
- 项目绝对路径
|
|
113
|
+
- 文件路径
|
|
114
|
+
- 变更类型:`add` 或 `delete`
|
|
115
|
+
- 行号
|
|
116
|
+
- 行内容
|
|
117
|
+
- 该文件当前的修改时间
|
|
118
|
+
|
|
119
|
+
如果该 ID 不在 AI diff 缓存中,就写入普通 diff 缓存:
|
|
120
|
+
|
|
121
|
+
```text
|
|
122
|
+
.x7-code-line/normal-diff-line-ids.json
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`stop` 会再次读取每个项目的 `git diff` 并计算行 ID。如果某个 ID 不在普通 diff 缓存中,就写入 AI diff 缓存:
|
|
126
|
+
|
|
127
|
+
```text
|
|
128
|
+
.x7-code-line/ai-diff-line-ids.json
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
缓存按项目绝对路径分组,结构类似:
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"projects": {
|
|
136
|
+
"/path/to/repo-a": ["line-id-1"],
|
|
137
|
+
"/path/to/repo-b": ["line-id-2"]
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Git Hooks
|
|
143
|
+
|
|
144
|
+
安装器会生成 shell hook,并写入中心缓存目录:
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
X7_CODE_LINE_BASE='<当前安装目录>'
|
|
148
|
+
export X7_CODE_LINE_BASE
|
|
149
|
+
npx --no-install x7-code-line git-pre-commit
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`pre-commit` 会读取当前仓库的 staged diff:
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
git diff --cached --no-ext-diff --unified=0 --no-color
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
然后使用当前仓库绝对路径从中心缓存中取出对应项目的 AI diff 行 ID,计算本次提交真正包含的 AI diff 行数,并写入:
|
|
159
|
+
|
|
160
|
+
```text
|
|
161
|
+
.x7-code-line/pending-commit.json
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
计算完成后,会清除当前仓库在以下两份中心缓存中的条目:
|
|
165
|
+
|
|
166
|
+
```text
|
|
167
|
+
.x7-code-line/ai-diff-line-ids.json
|
|
168
|
+
.x7-code-line/normal-diff-line-ids.json
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
其他项目的缓存不会被修改。
|
|
172
|
+
|
|
173
|
+
`commit-msg` 会读取该结果,并追加或替换 trailer:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
x7-ai-lines: 3
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
如果没有命中 AI diff 行,则写入:
|
|
180
|
+
|
|
181
|
+
```text
|
|
182
|
+
x7-ai-lines: 0
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## 多项目安装
|
|
186
|
+
|
|
187
|
+
`install` 至少需要传入一个目录;0 目录场景会直接失败。
|
|
188
|
+
|
|
189
|
+
默认要求传入绝对路径:
|
|
190
|
+
|
|
191
|
+
```sh
|
|
192
|
+
npm install -g x7-code-line --dir /abs/path/repo-a --dir /abs/path/repo-b
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
例如,将 `x7-code-line` 安装在 C 盘目录,但指定 D 盘的 Git 项目:
|
|
196
|
+
|
|
197
|
+
```powershell
|
|
198
|
+
cd C:\x7-tools\x7-code-line-host
|
|
199
|
+
npm install -g x7-code-line --dir D:\work\repo-a --dir D:\work\repo-b
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
如果确实需要解析相对路径,必须显式传 `--relative`:
|
|
203
|
+
|
|
204
|
+
```sh
|
|
205
|
+
npm install -g x7-code-line --relative --dir ../repo-a --dir ../repo-b
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
此时相对路径基于当前执行 `install` 命令的目录解析。
|
|
209
|
+
|
|
210
|
+
也可以在安装时通过环境变量指定多个目标目录。
|
|
211
|
+
|
|
212
|
+
如果通过环境变量或配置文件传相对路径,需要同时设置 `X7_CODE_LINE_RELATIVE_PATHS=1`。
|
|
213
|
+
|
|
214
|
+
macOS / Linux 示例:
|
|
215
|
+
|
|
216
|
+
```sh
|
|
217
|
+
X7_CODE_LINE_RELATIVE_PATHS=1 X7_CODE_LINE_DIRS="../repo-a:../repo-b" npm install -g x7-code-line
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Windows 使用 `;` 作为目录分隔符:
|
|
221
|
+
|
|
222
|
+
```powershell
|
|
223
|
+
$env:X7_CODE_LINE_RELATIVE_PATHS = "1"
|
|
224
|
+
$env:X7_CODE_LINE_DIRS = "../repo-a;../repo-b"
|
|
225
|
+
npm install -g x7-code-line
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## 配置文件
|
|
229
|
+
|
|
230
|
+
可以通过 `--config` 或 `X7_CODE_LINE_CONFIG` 指定 JSON 配置文件:
|
|
231
|
+
|
|
232
|
+
```json
|
|
233
|
+
{
|
|
234
|
+
"dirs": ["../repo-a", "../repo-b"],
|
|
235
|
+
"gitDirs": [".git"]
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
字段说明:
|
|
240
|
+
|
|
241
|
+
- `dirs`:目标项目目录列表。
|
|
242
|
+
- `gitDirs`:相对于每个目标项目目录的 Git 目录列表。
|
|
243
|
+
|
|
244
|
+
## macOS 适配说明
|
|
245
|
+
|
|
246
|
+
macOS 下整体可以直接使用,但需要注意以下几点:
|
|
247
|
+
|
|
248
|
+
- Git hooks 使用 `#!/bin/sh`,macOS 自带 `/bin/sh`,无需额外安装 shell。
|
|
249
|
+
- 安装器会对生成的 hook 文件执行 `chmod 755`。如果你手动复制 hook 文件,也需要执行:
|
|
250
|
+
|
|
251
|
+
```sh
|
|
252
|
+
chmod +x .git/hooks/pre-commit .git/hooks/commit-msg
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
- macOS / Linux 的多项目环境变量使用 `:` 分隔目录;不要使用 Windows 的 `;`。
|
|
256
|
+
- 如果 Cursor 或 Codex 不是从终端启动,GUI 进程可能拿不到你的 shell `PATH`,导致 hook 中的 `npx` 找不到。遇到这种情况,优先从终端启动 Cursor/Codex,或确保 Node.js / npm 的安装路径已经进入 GUI 应用可见的 `PATH`。
|
|
257
|
+
- 如果项目路径包含空格,安装器生成的 Git hook 会对 `X7_CODE_LINE_BASE` 做 shell quote,通常可以正常工作。
|
|
258
|
+
- 使用 nvm、fnm、asdf 等 Node 版本管理器时,macOS GUI 应用经常不会加载对应初始化脚本。建议在运行 hooks 前确认:
|
|
259
|
+
|
|
260
|
+
```sh
|
|
261
|
+
which node
|
|
262
|
+
which npm
|
|
263
|
+
which npx
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
如果这些命令在 Cursor/Codex hook 环境中不可用,需要调整启动方式或 PATH。
|
|
267
|
+
|
|
268
|
+
## Windows 适配说明
|
|
269
|
+
|
|
270
|
+
- Git for Windows 会用 Git Bash 执行 `.git/hooks/*`,因此 `#!/bin/sh` hook 可以运行。
|
|
271
|
+
- PowerShell 下设置多项目环境变量时使用 `$env:X7_CODE_LINE_DIRS`。
|
|
272
|
+
- Windows 目录分隔符使用 `;`,例如:
|
|
273
|
+
|
|
274
|
+
```powershell
|
|
275
|
+
$env:X7_CODE_LINE_DIRS = "D:\repo-a;D:\repo-b"
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## 增量添加目录
|
|
279
|
+
|
|
280
|
+
在已经完成安装的当前安装目录下,可以使用 `addDir` 增量添加新的项目目录:
|
|
281
|
+
|
|
282
|
+
```sh
|
|
283
|
+
x7-code-line addDir --dir /abs/path/repo-c
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
这个命令只做两件事:
|
|
287
|
+
|
|
288
|
+
- 把新目录追加到当前安装目录 `.x7-code-line/projects.json`
|
|
289
|
+
- 为新目录安装 `.git/hooks/pre-commit` 和 `.git/hooks/commit-msg`
|
|
290
|
+
|
|
291
|
+
它不会重写当前安装目录的 `.cursor/hooks.json`、`.codex/hooks.json`,也不会重写已有项目集合。
|
|
292
|
+
|
|
293
293
|
`addDir` 会沿用与 `install` 相同的严格校验:
|
|
294
|
-
|
|
295
|
-
- 目录必须真实存在
|
|
296
|
-
- 目录必须可访问
|
|
297
|
-
- 目录必须包含可用的 `.git` 目录
|
|
298
|
-
|
|
299
|
-
任一新增目录不满足条件时,命令直接失败,不会继续追加。
|
|
300
|
-
|
|
294
|
+
|
|
295
|
+
- 目录必须真实存在
|
|
296
|
+
- 目录必须可访问
|
|
297
|
+
- 目录必须包含可用的 `.git` 目录
|
|
298
|
+
|
|
299
|
+
任一新增目录不满足条件时,命令直接失败,不会继续追加。
|
|
300
|
+
|
|
301
301
|
默认只接受绝对路径;如果需要解析相对路径,需要显式传入:
|
|
302
|
-
|
|
303
|
-
```sh
|
|
302
|
+
|
|
303
|
+
```sh
|
|
304
304
|
x7-code-line addDir --relative --dir ../repo-c
|
|
305
305
|
```
|
|
306
306
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
```sh
|
|
310
|
-
x7-code-line install --dir <absolute-path> [--dir <absolute-path> ...] [--config path] [--relative]
|
|
311
|
-
x7-code-line addDir --dir <absolute-path> [--dir <absolute-path> ...] [--config path] [--relative]
|
|
312
|
-
x7-code-line prompt-submit
|
|
313
|
-
x7-code-line stop
|
|
314
|
-
x7-code-line git-pre-commit
|
|
315
|
-
x7-code-line git-commit-msg <commit-msg-file>
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
## 本地验证
|
|
319
|
-
|
|
320
|
-
```sh
|
|
321
|
-
npm test
|
|
322
|
-
npm pack --dry-run
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
## 发布到 npm
|
|
326
|
-
|
|
327
|
-
首次发布和后续更新都建议先在本地完成校验:
|
|
328
|
-
|
|
329
|
-
```sh
|
|
330
|
-
npm test
|
|
331
|
-
npm pack --dry-run
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
确认当前登录的 npm 账号:
|
|
335
|
-
|
|
336
|
-
```sh
|
|
337
|
-
npm whoami
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
如果还没有登录:
|
|
341
|
-
|
|
342
|
-
```sh
|
|
343
|
-
npm login
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
### 首次发布
|
|
347
|
-
|
|
348
|
-
确认 `package.json` 中的包名和版本号正确后,执行:
|
|
349
|
-
|
|
350
|
-
```sh
|
|
351
|
-
npm publish --access public
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
如果 npm 账号开启了 2FA,通常需要附带 OTP:
|
|
355
|
-
|
|
356
|
-
```sh
|
|
357
|
-
npm publish --otp <6位验证码>
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
如果将来改为 scope 包并需要公开发布,则使用:
|
|
361
|
-
|
|
362
|
-
```sh
|
|
363
|
-
npm publish --access public --otp <6位验证码>
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
### 更新已发布版本
|
|
367
|
-
|
|
368
|
-
每次发布新版本前,手动修改package.json版本号或命令提升版本号:
|
|
369
|
-
|
|
370
|
-
```sh
|
|
371
|
-
npm version patch
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
也可以按需要使用:
|
|
375
|
-
|
|
376
|
-
```sh
|
|
377
|
-
npm version minor
|
|
378
|
-
npm version major
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
然后重新发布:
|
|
307
|
+
`install` 和 `addDir` 成功后,会输出:
|
|
382
308
|
|
|
383
|
-
```
|
|
384
|
-
|
|
309
|
+
```text
|
|
310
|
+
[x7-code-line] D:\your-repo configured successfully.
|
|
385
311
|
```
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
```sh
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
312
|
+
|
|
313
|
+
## CLI 命令
|
|
314
|
+
|
|
315
|
+
```sh
|
|
316
|
+
x7-code-line install --dir <absolute-path> [--dir <absolute-path> ...] [--config path] [--relative]
|
|
317
|
+
x7-code-line addDir --dir <absolute-path> [--dir <absolute-path> ...] [--config path] [--relative]
|
|
318
|
+
x7-code-line prompt-submit
|
|
319
|
+
x7-code-line stop
|
|
320
|
+
x7-code-line git-pre-commit
|
|
321
|
+
x7-code-line git-commit-msg <commit-msg-file>
|
|
394
322
|
```
|
|
395
323
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
324
|
+
`prompt-submit` 和 `stop` 执行时,会优先按以下顺序定位主目录中的 `.x7-code-line` 缓存:
|
|
325
|
+
|
|
326
|
+
- `X7_CODE_LINE_BASE`
|
|
327
|
+
- 当前目录向上查找 `.x7-code-line/install.json`
|
|
328
|
+
- 安装时写入的全局 base 指针
|
|
329
|
+
|
|
330
|
+
因此即使在其他目录手动执行命令,也会优先读取主目录缓存,而不是把执行目录当作缓存目录。
|
|
331
|
+
|
|
332
|
+
## 本地验证
|
|
333
|
+
|
|
334
|
+
```sh
|
|
335
|
+
npm test
|
|
336
|
+
npm pack --dry-run
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
## 发布到 npm
|
|
340
|
+
|
|
341
|
+
首次发布和后续更新都建议先在本地完成校验:
|
|
342
|
+
|
|
343
|
+
```sh
|
|
344
|
+
npm test
|
|
345
|
+
npm pack --dry-run
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
确认当前登录的 npm 账号:
|
|
349
|
+
|
|
350
|
+
```sh
|
|
351
|
+
npm whoami
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
如果还没有登录:
|
|
355
|
+
|
|
356
|
+
```sh
|
|
357
|
+
npm login
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### 首次发布
|
|
361
|
+
|
|
362
|
+
确认 `package.json` 中的包名和版本号正确后,执行:
|
|
363
|
+
|
|
364
|
+
```sh
|
|
365
|
+
npm publish --access public
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
如果 npm 账号开启了 2FA,通常需要附带 OTP:
|
|
369
|
+
|
|
370
|
+
```sh
|
|
371
|
+
npm publish --otp <6位验证码>
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
如果将来改为 scope 包并需要公开发布,则使用:
|
|
375
|
+
|
|
376
|
+
```sh
|
|
377
|
+
npm publish --access public --otp <6位验证码>
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### 更新已发布版本
|
|
381
|
+
|
|
382
|
+
每次发布新版本前,手动修改package.json版本号或命令提升版本号:
|
|
383
|
+
|
|
384
|
+
```sh
|
|
385
|
+
npm version patch
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
也可以按需要使用:
|
|
389
|
+
|
|
390
|
+
```sh
|
|
391
|
+
npm version minor
|
|
392
|
+
npm version major
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
然后重新发布:
|
|
396
|
+
|
|
397
|
+
```sh
|
|
398
|
+
npm publish --otp <6位验证码>
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
### 常见发布流程
|
|
402
|
+
|
|
403
|
+
```sh
|
|
404
|
+
npm test
|
|
405
|
+
npm pack --dry-run
|
|
406
|
+
npm version patch
|
|
407
|
+
npm publish --otp <6位验证码>
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### 2FA 与 token
|
|
411
|
+
|
|
412
|
+
如果 npm 账号开启了两步验证,发布时需要满足以下任一条件:
|
|
413
|
+
|
|
414
|
+
- 直接在 `npm publish` 时传入 `--otp`。
|
|
415
|
+
- 使用具备 publish 权限并允许 bypass 2FA 的 npm access token。
|
|
416
|
+
|
|
417
|
+
手动在本机发布时,最直接的方式通常是:
|
|
418
|
+
|
|
419
|
+
```sh
|
|
420
|
+
npm publish --otp <当前验证码>
|
|
421
|
+
```
|