dsh-git-push 2.9.4 → 2.10.1

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 (80) hide show
  1. package/README.md +42 -39
  2. package/lib/app/apply.js +8 -1
  3. package/lib/app/audit-api.js +4 -1
  4. package/lib/app/command-registry.js +18 -11
  5. package/lib/app/handlers/meta.js +38 -0
  6. package/lib/app/handlers/repo-actions.js +3 -1
  7. package/lib/app/handlers/repos.js +18 -2
  8. package/lib/app/handlers/settings.js +0 -0
  9. package/lib/app/http-handlers.js +3 -1
  10. package/lib/app/schema.js +0 -0
  11. package/lib/app/settings-bridge.js +0 -0
  12. package/lib/app/tool-call.js +32 -23
  13. package/lib/ast/brace.js +15 -0
  14. package/lib/ast/control-flow.js +12 -3
  15. package/lib/ast/size.js +73 -3
  16. package/lib/audit/audit-file.js +4 -2
  17. package/lib/audit/collector.js +13 -4
  18. package/lib/audit/file-context.js +89 -0
  19. package/lib/audit/finding.js +16 -2
  20. package/lib/audit/ignore-blind.js +5 -4
  21. package/lib/audit/index.js +2 -2
  22. package/lib/audit/orchestrate.js +2 -1
  23. package/lib/audit-rules/audit-rules-frontend.yml +4 -4
  24. package/lib/checks/button-bind.js +84 -13
  25. package/lib/checks/dispatch.js +1 -1
  26. package/lib/checks/private.js +2 -1
  27. package/lib/checks/structural.js +10 -3
  28. package/lib/client-parts/{10-account.js → account.js} +1 -1
  29. package/lib/client-parts/{41-actions.js → actions.js} +3 -2
  30. package/lib/client-parts/{99-apply.js → apply.js} +2 -1
  31. package/lib/client-parts/{21-audit.js → audit.js} +5 -5
  32. package/lib/client-parts/{11-browser.js → browser.js} +15 -4
  33. package/lib/client-parts/{01-const.js → const.js} +11 -5
  34. package/lib/client-parts/{40-controller.js → controller.js} +4 -4
  35. package/lib/client-parts/{03-icons.js → icons.js} +1 -1
  36. package/lib/client-parts/{00-loader.js → loader.js} +1 -0
  37. package/lib/client-parts/{23-npm.js → npm.js} +3 -2
  38. package/lib/client-parts/{30-page.js → page.js} +1 -1
  39. package/lib/client-parts/{20-repo.js → repo.js} +2 -2
  40. package/lib/client-parts/{22-settings.js → settings.js} +1 -1
  41. package/lib/client-parts/{02-utils.js → utils.js} +1 -1
  42. package/lib/client.js +51 -30
  43. package/lib/commit-push.js +20 -2
  44. package/lib/context/index.js +2 -1
  45. package/lib/core/atomic-json.js +1 -1
  46. package/lib/core/exec.js +6 -4
  47. package/lib/exempt/index.js +7 -2
  48. package/lib/exempt/tool.js +198 -0
  49. package/lib/git/clone-history-mode.js +24 -5
  50. package/lib/git/index-format.js +182 -0
  51. package/lib/git/index.js +1 -0
  52. package/lib/git/push.js +28 -13
  53. package/lib/git/repo-index.js +41 -30
  54. package/lib/git/scan-runner.js +1 -1
  55. package/lib/git/transport.js +49 -17
  56. package/lib/git/visibility.js +93 -9
  57. package/lib/npm/publish.js +30 -1
  58. package/lib/npm/registry.js +5 -2
  59. package/lib/self/index.js +1 -1
  60. package/package.json +2 -4
  61. package/scripts/build-client.mjs +19 -14
  62. package/scripts/picker-repro.mjs +115 -0
  63. package/skills/dev/dsh-git-push.md +1 -2
  64. package/skills/dsh-repo-index.md +1 -1
  65. package/skills/guide/guide-audit.md +77 -3
  66. package/skills/guide/guide-cli-scripts.md +4 -4
  67. package/skills/guide/guide-clone-repos.md +2 -2
  68. package/skills/guide/guide-inject-ui.md +6 -6
  69. package/skills/workflow/README.md +1 -1
  70. package/assets/minihost.sh +0 -35
  71. package/assets/preview.html +0 -36703
  72. package/lib/fs/edit-after-read.js +0 -142
  73. package/scripts/frontend-preview-check.mjs +0 -124
  74. package/scripts/preview-gen.mjs +0 -489
  75. package/scripts/preview-local-runner.mjs +0 -55
  76. package/scripts/preview-server-direct.mjs +0 -116
  77. package/scripts/preview-server.mjs +0 -305
  78. package/scripts/preview-slot-data.mjs +0 -123
  79. package/scripts/sync-plugin-install.mjs +0 -286
  80. package/scripts/sync-plugin.mjs +0 -284
@@ -30,7 +30,7 @@ import { resolveRepoVisibility } from './git/index.js';
30
30
  * (README「④ 私有库豁免」);缺省按 origin 探测,探测失败按 unknown(保守拦截)。
31
31
  * 显式传入可免网络探测(测试/已知场景)。
32
32
  */
33
- export async function runAudit({ repoPath = '', audit, rulesetDir, slots, cfg: extCfg, visibility: extVisibility } = {}) {
33
+ export async function runAudit({ repoPath = '', audit, rulesetDir, slots, cfg: extCfg, visibility: extVisibility, exemptPaths = [], exemptReason = '' } = {}) {
34
34
  const cfg = extCfg || defaultConfig();
35
35
  const doAudit = audit ?? cfg.auditEnabled;
36
36
  if (!doAudit) return { ok: true, audit: null };
@@ -58,9 +58,27 @@ export async function runAudit({ repoPath = '', audit, rulesetDir, slots, cfg: e
58
58
  // 审计输出改为「级别数量 + 评分 + API 查询用法」——审计明细由聚合 API
59
59
  // (/api/git-push/audit)查询,不再内联 findings 全文;blocked 摘要保留(拦截必需)。
60
60
  auditOut.apiGuide = buildAuditApiGuide(repoPath, { scope: auditRes.scope, summary: auditRes.summary, quality: auditOut.quality });
61
+ // 本次提交的按文件临时豁免(auditExemptPaths):列入的文件 finding 一律不参与拦截判定(含 blocker),
62
+ // 仅本次有效、不落盘、不改 .auditignore;评分仍用全量 findings(豁免不影响打分基数)。
63
+ // 语义按需求方定稿:豁免即豁免,不设「只允许 warning/notice」的限制,也不按规则类别过滤。
64
+ const exemptSet = new Set((Array.isArray(exemptPaths) ? exemptPaths : [])
65
+ .map((p) => String(p || '').replace(/\\/g, '/').replace(/^\.\//, '').trim())
66
+ .filter(Boolean));
67
+ const isExemptFile = (f) => exemptSet.size > 0 && exemptSet.has(String(f.file || '').replace(/\\/g, '/').replace(/^\.\//, ''));
68
+ const exemptFindings = exemptSet.size ? auditRes.findings.filter((f) => isExemptFile(f)) : [];
61
69
  // 只算非示例目录的 blocker(示例目录内的 blocker 是规则演示,不拦真实提交)
62
- const blocked = auditRes.findings.filter((f) => f.severity === 'blocker' && !isSampleExemptDir(repoPath || '.', f.file));
70
+ const blocked = auditRes.findings.filter((f) => f.severity === 'blocker' && !isSampleExemptDir(repoPath || '.', f.file) && !isExemptFile(f));
63
71
  auditOut.blocked = blocked.map((f) => ({ file: f.file, line: f.line, rule: f.rule, message: (f.message || '').slice(0, MAX_MSG_PREVIEW) }));
72
+ if (exemptSet.size) {
73
+ // 回显(记录而非判断):本次豁免了哪些文件、命中哪些规则、共几条 finding
74
+ auditOut.exempt = {
75
+ files: [...exemptSet],
76
+ count: exemptFindings.length,
77
+ rules: [...new Set(exemptFindings.map((f) => f.rule).filter(Boolean))],
78
+ ...(exemptReason ? { reason: String(exemptReason) } : {}),
79
+ note: '本次提交临时豁免(不落盘、不影响后续提交);评分仍按全量 findings 计算',
80
+ };
81
+ }
64
82
  if (blocked.length > 0) {
65
83
  // 【修复】私有库豁免(README「④ 私有库豁免」):远端可见性 = private → 审计只报告不拦截。
66
84
  // 修复背景:私有库被拦的回归——
@@ -15,6 +15,7 @@ import { spawnSync } from 'node:child_process';
15
15
  import { hostname as osHostname, networkInterfaces } from 'node:os';
16
16
  import { fileURLToPath } from 'node:url';
17
17
  import { writeJsonAtomic } from '../git/atomic-json.js';
18
+ import { GIT_GLOBAL_ARGS } from '../core/exec.js';
18
19
 
19
20
  /* ───────── 工具探测常量 ───────── */
20
21
  const PROBE_TIMEOUT_MS = 8000; // 工具路径/版本探测子进程超时
@@ -210,7 +211,7 @@ export function mapWorkspaceDirs({ workspaceRoot = '', cwd = process.cwd(), maxC
210
211
  const root = workspaceRoot ? resolve(workspaceRoot) : cur;
211
212
  let projectDir = root;
212
213
  try {
213
- const gitProbe = spawnSync('git', ['rev-parse', '--show-toplevel'], {
214
+ const gitProbe = spawnSync('git', [...GIT_GLOBAL_ARGS, 'rev-parse', '--show-toplevel'], {
214
215
  cwd: cur, encoding: 'utf8', timeout: PROBE_TIMEOUT_MS, stdio: ['pipe', 'pipe', 'ignore'],
215
216
  });
216
217
  const top = String(gitProbe.stdout || '').trim();
@@ -2,7 +2,7 @@
2
2
  * 统一 JSON 文件原子读写(收敛:多处重复的「读 JSON → 改 → 原子写回」抽成一份)。
3
3
  *
4
4
  * 消费方(原各自实现):settings-bridge(config.json)、account-status(account-status.json)、
5
- * scan-runner(scan-live.json)、repo-index(dsh-repo-index.json)、credentials 等。
5
+ * scan-runner(scan-live.json)、repo-index(dsh-repo-index.jsonl,JSONL 逐行原子写)、credentials 等。
6
6
  *
7
7
  * 原子写语义:写同目录 `.tmp` 再 rename(同卷原子替换,避免写一半崩溃留坏文件);
8
8
  * CIFS 可能改不了 mode,chmod 失败静默(与各原实现一致)。
package/lib/core/exec.js CHANGED
@@ -84,14 +84,15 @@ export const GIT_READ_VERBS = new Set(['fetch', 'clone', 'pull', 'ls-remote', 's
84
84
  export const GIT_GLOBAL_ARGS = ['-c', 'safe.directory=*', '-c', 'core.filemode=false', '-c', 'core.quotepath=false'];
85
85
 
86
86
  /** 统一 git 执行(数组参数,零注入面;stderr 保留供排障)。 */
87
- export function runGit(args, { cwd = '', timeoutMs = 120_000, env = {} } = {}) {
87
+ export function runGit(args, { cwd = '', timeoutMs = 120_000, env = {}, noMirror = false } = {}) {
88
88
  const base = GIT_GLOBAL_ARGS;
89
89
  const bin = resolveGitBin();
90
90
  if (!bin) return { ok: false, stdout: '', stderr: GIT_MISSING_MSG };
91
91
  // 只读的网络动词注入镜像(与 http 层同口径:默认走 gh-proxy);
92
92
  // 写操作(push 等)不在集合里 ⇒ 不注入 ⇒ 推送一律直连(凭据与写入不经第三方)。
93
+ // noMirror:调用方已判定目标仓库是私有/未知 ⇒ 不注入——镜像看不到私有库,注入必失败。
93
94
  const mirrorEnv = {};
94
- if (GIT_READ_VERBS.has(String(args[0] || ''))) mirrorGitConfigEnv(mirrorEnv, { mode: 'read' });
95
+ if (!noMirror && GIT_READ_VERBS.has(String(args[0] || ''))) mirrorGitConfigEnv(mirrorEnv, { mode: 'read' });
95
96
  try {
96
97
  const out = execFileSync(bin, cwd ? base.concat(['-C', cwd], args) : base.concat(args), {
97
98
  encoding: 'utf8',
@@ -139,14 +140,15 @@ export function gitRaw(args, { cwd = '', timeoutMs = 600_000 } = {}) {
139
140
  * 实测 6 仓「并发」仍要 13s(≈ 串行)。改用本函数后各 worker 的子进程才能真正并行等待。
140
141
  * 语义与 runGit 保持一致:stdout 同样 trim,失败返回 {ok:false, error} 而不抛。
141
142
  */
142
- export function runGitAsync(args, { cwd = '', timeoutMs = 120_000, env = {} } = {}) {
143
+ export function runGitAsync(args, { cwd = '', timeoutMs = 120_000, env = {}, noMirror = false } = {}) {
143
144
  const base = GIT_GLOBAL_ARGS;
144
145
  const bin = resolveGitBin();
145
146
  if (!bin) return Promise.resolve({ ok: false, stdout: '', stderr: GIT_MISSING_MSG });
146
147
  // 与 runGit 同口径:只读网络动词注入镜像,push 等写操作不注入(见 GIT_READ_VERBS)。
147
148
  // 此前只有同步版注入,异步版仍直连 ⇒ 并发探测远端那类调用绕过镜像。
149
+ // noMirror:目标仓库私有/未知时不注入(镜像看不到私有库)。
148
150
  const mirrorEnv = {};
149
- if (GIT_READ_VERBS.has(String(args[0] || ''))) mirrorGitConfigEnv(mirrorEnv, { mode: 'read' });
151
+ if (!noMirror && GIT_READ_VERBS.has(String(args[0] || ''))) mirrorGitConfigEnv(mirrorEnv, { mode: 'read' });
150
152
  return new Promise((resolve) => {
151
153
  execFile(bin, cwd ? base.concat(['-C', cwd], args) : base.concat(args), {
152
154
  encoding: 'utf8',
@@ -75,13 +75,18 @@ export const EXEMPT_MARKERS = {
75
75
  // 能豁免这些 kind,而规则自身却提示用 dsh-skip-quality ⇒ 写了不生效;
76
76
  // ② regex —— 兜底覆盖**非安全类**的 regex finding(安全类由 excludeRules 排除,
77
77
  // 仍必须写 dsh-skip-sensitive,避免用质量标记顺手静默掉安全问题 ✗)。
78
+ // 2026-10-24 补齐(外部项目实测反馈):button-bind 此前**不在任何标记的 blocked 里**,
79
+ // 而 button/unbound 与 button/create-element-binding 两条规则都提示 dsh-skip-quality
80
+ // ⇒ 用户按提示写文件头标记**完全不生效**,且找不到别的标记可用(同一类「提示写了
81
+ // 不生效」缺陷)。按钮绑定判定是启发式的(事件委托/封装函数/原生 label 触发都可能
82
+ // 合法),必须留一条可豁免路径。
78
83
  blocked: ['func-lines', 'empty-catch', 'sync-fs', 'magic-number-smart', 'blacklist',
79
- 'comment-density', 'file-health',
84
+ 'comment-density', 'file-health', 'button-bind',
80
85
  'semantic', 'dataflow', 'duplicate-const', 'repeated-string', 'regex'],
81
86
  // regex 兜底时**排除**安全类规则名(这些必须走 dsh-skip-sensitive,不能靠质量标记吞掉)
82
87
  excludeRules: ['security', 'credential', 'credref', 'credfile', 'secret', 'password', 'hardcoded'],
83
88
  lineLevel: false,
84
- hint: '文件头=整文件免质量评分(func-lines/empty-catch/sync-fs/magic-number-smart/blacklist 措辞/comment-density/file-health)',
89
+ hint: '文件头=整文件免质量评分(func-lines/empty-catch/sync-fs/magic-number-smart/blacklist 措辞/comment-density/file-health/button-bind 按钮绑定)',
85
90
  },
86
91
  'dsh-skip-residue': {
87
92
  dimensions: ['可读性', '可维护性'],
@@ -0,0 +1,198 @@
1
+ /**
2
+ * 注释豁免工具(exempt):把「手写 dsh-skip-* 注释」变成工具操作。
3
+ *
4
+ * 解决的问题(历史痛点):
5
+ * ① 记不住哪个 finding 该写哪个标记、写文件头还是行尾;
6
+ * ② 写了标记却不生效(提示与实际行为不符,曾一次查出 24 种组合);
7
+ * ③ 标记散落各文件,无法盘点/清理。
8
+ *
9
+ * 动作:add(加)/ remove(删)/ list(盘点)/ check(校验真生效)。
10
+ * 标记映射单一真源:直接读同进程 lib/exempt/index.js 的 EXEMPT_MARKERS,不复制。
11
+ *
12
+ * 写入格式与手写完全一致(审计侧零改动):
13
+ * header → 文件头前 3 行内追加 `// dsh-skip-<x>: <reason>`
14
+ * line → 该行行尾追加 ` // dsh-skip-<x>`
15
+ */
16
+ import { existsSync, readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';
17
+ import { join, relative, resolve, sep, dirname } from 'node:path';
18
+
19
+ import { EXEMPT_MARKERS, exemptHintForFinding, exemptMarkerCovers } from './index.js';
20
+ import { auditFull } from '../audit/orchestrate.js';
21
+
22
+ /** 跑一次审计找 rule 对应的 finding kind(豁免判定按 kind,如 max-nesting-depth → max-depth)。 */
23
+ async function findRuleKind(repoPath, ruleId) {
24
+ try {
25
+ const full = await auditFull(repoPath, { scope: 'full', maxScanFiles: 2000 });
26
+ const hit = (full.findings || []).find((f) => f.rule === ruleId);
27
+ return hit ? String(hit.kind || '') : '';
28
+ } catch {
29
+ return '';
30
+ }
31
+ }
32
+
33
+ /** 读文件(UTF-8);不存在返回 null。 */
34
+ function readText(file) {
35
+ try { return existsSync(file) ? readFileSync(file, 'utf8') : null; } catch { return null; }
36
+ }
37
+
38
+ /** 路径归一:相对仓库根、去 ./、防越界。返回 { ok, abs?, rel?, error? }。 */
39
+ function resolveTarget(repoPath, p) {
40
+ const root = resolve(repoPath);
41
+ const rel = String(p || '').replace(/\\/g, '/').replace(/^\.\//, '').trim();
42
+ if (!rel) return { ok: false, error: '缺少 path' };
43
+ if (rel.startsWith('/') || /^[A-Za-z]:/.test(rel)) return { ok: false, error: `path 必须是相对仓库根(收到绝对路径:${rel})` };
44
+ const abs = resolve(root, rel);
45
+ if (abs !== root && !abs.startsWith(root + sep)) return { ok: false, error: `path 越界(解析后不在仓库内:${rel})` };
46
+ return { ok: true, abs, rel };
47
+ }
48
+
49
+ /** 由 rule/marker 推导写入参数;返回 { ok, marker?, scope?, reason?, error? }。 */
50
+ function resolveMarker({ rule = '', marker = '', scope = '', kind = '' }) {
51
+ const wantMarker = String(marker || '').trim();
52
+ const wantRule = String(rule || '').trim();
53
+ if (!wantMarker && !wantRule) return { ok: false, error: '需要 rule(用于推导标记)或显式 marker' };
54
+ // 反查「真能豁免该 rule 的标记」(与豁免判定同一口径)
55
+ // 豁免判定按 finding 的 kind(如 max-nesting-depth → max-depth)——kind 由 addExempt 跑审计拿到;
56
+ // 拿不到时退回 id 最后一段(仅兜底,可能对不上,会如实报「没有标记能豁免」)。
57
+ const kindUsed = kind || (wantRule ? String(wantRule).split('/').pop() : '');
58
+ const hint = wantRule ? exemptHintForFinding({ rule: wantRule, kind: kindUsed }) : null;
59
+ const resolved = wantMarker || (hint && hint.marker) || '';
60
+ if (!resolved) return { ok: false, error: `没有任何标记能豁免 rule「${wantRule}」(应改代码,或走 .auditignore / 豁免槽位)` };
61
+ const meta = EXEMPT_MARKERS[resolved];
62
+ if (!meta) return { ok: false, error: `未知标记「${resolved}」;可用:${Object.keys(EXEMPT_MARKERS).join(', ')}` };
63
+ // 显式 marker 与 rule 推导冲突 ⇒ 以推导为准并报错(防写错标记)
64
+ if (wantMarker && hint && hint.marker !== wantMarker) {
65
+ return { ok: false, error: `标记「${wantMarker}」不能豁免 rule「${wantRule}」,正确标记是「${hint.marker}」` };
66
+ }
67
+ // 显式 marker 但没给 rule:用 marker 的元数据判定范围
68
+ const finalScope = String(scope || '').trim() || (meta.lineLevel ? 'header' : 'header');
69
+ if (finalScope === 'line' && meta.lineLevel !== true) {
70
+ return { ok: false, error: `标记「${resolved}」不支持行级(lineLevel=false),只能用 scope=header` };
71
+ }
72
+ if (finalScope !== 'header' && finalScope !== 'line') {
73
+ return { ok: false, error: `scope 只能是 header 或 line(收到 ${finalScope})` };
74
+ }
75
+ return { ok: true, marker: resolved, scope: finalScope, hint: hint ? hint.hint : (meta.hint || '') };
76
+ }
77
+
78
+ /** 是否已有同一标记(header:前 3 行;line:指定行)。 */
79
+ function hasMarker(lines, marker, scope, line) {
80
+ const re = new RegExp(`dsh-skip-${marker.replace(/^dsh-skip-/, '')}\\b`);
81
+ if (scope === 'header') return lines.slice(0, 3).some((l) => re.test(l));
82
+ const idx = Number(line) - 1;
83
+ return idx >= 0 && idx < lines.length && re.test(lines[idx]);
84
+ }
85
+
86
+ /** 生成 header 注释行。 */
87
+ function headerComment(marker, reason) {
88
+ return `// ${marker}${reason ? `: ${String(reason).replace(/\s+/g, ' ').trim()}` : ''}`;
89
+ }
90
+
91
+ /**
92
+ * add:给指定文件(或行)加豁免注释。
93
+ * @returns {{ok, results?:Array, error?}}
94
+ */
95
+ export async function addExempt({ repoPath = '', path: p, line, scope = '', rule = '', marker = '', reason = '', dryRun = false } = {}) {
96
+ const paths = Array.isArray(p) ? p : [p];
97
+ if (!paths.length) return { ok: false, error: '缺少 path' };
98
+ if (paths.length > 1 && line !== undefined) return { ok: false, error: '多文件时不允许 line(行号无法跨文件对齐)——改用 scope=header 或逐个调用' };
99
+ // 先跑一次审计拿 rule 的 finding kind(豁免判定按 kind,如 max-nesting-depth → max-depth)
100
+ const kind = String(rule || '').trim() ? await findRuleKind(repoPath, String(rule).trim()) : '';
101
+ const m = resolveMarker({ rule, marker, scope, kind });
102
+ if (!m.ok) return { ok: false, error: m.error };
103
+ if (m.scope === 'line' && line === undefined) return { ok: false, error: 'scope=line 时必须给 line(1-based 行号)' };
104
+
105
+ // ① 先全部校验(原子:任一失败则一个都不写)
106
+ const plans = [];
107
+ const results = [];
108
+ for (const one of paths) {
109
+ const t = resolveTarget(repoPath, one);
110
+ if (!t.ok) { results.push({ path: String(one), ok: false, error: t.error }); continue; }
111
+ const text = readText(t.abs);
112
+ if (text === null) { results.push({ path: t.rel, ok: false, error: '文件不存在' }); continue; }
113
+ const lines = text.split('\n');
114
+ if (m.scope === 'line') {
115
+ const idx = Number(line) - 1;
116
+ if (!Number.isInteger(idx) || idx < 0 || idx >= lines.length) { results.push({ path: t.rel, ok: false, error: `line ${line} 越界(文件共 ${lines.length} 行)` }); continue; }
117
+ const row = lines[idx];
118
+ if (/\/\*/.test(row) && !/\*\//.test(row)) { results.push({ path: t.rel, ok: false, error: '该行在块注释内,建议 scope=header' }); continue; }
119
+ if (/^\s*(\/\/|\*)/.test(row)) { results.push({ path: t.rel, ok: false, error: '该行是注释行,无法行尾追加,建议 scope=header' }); continue; }
120
+ if (hasMarker(lines, m.marker, 'line', line)) { results.push({ path: t.rel, ok: true, unchanged: true }); continue; }
121
+ const next = [...lines];
122
+ next[idx] = `${row} // ${m.marker}`;
123
+ plans.push({ abs: t.abs, rel: t.rel, next: next.join('\n') });
124
+ results.push({ path: t.rel, ok: true, changed: true, line: Number(line), marker: m.marker });
125
+ } else {
126
+ if (hasMarker(lines, m.marker, 'header')) { results.push({ path: t.rel, ok: true, unchanged: true }); continue; }
127
+ const next = [headerComment(m.marker, reason), ...lines];
128
+ plans.push({ abs: t.abs, rel: t.rel, next: next.join('\n') });
129
+ results.push({ path: t.rel, ok: true, changed: true, marker: m.marker, scope: 'header' });
130
+ }
131
+ }
132
+ const failed = results.filter((r) => r.ok === false);
133
+ if (failed.length) return { ok: false, error: `校验失败(${failed.length} 个文件),未写入任何文件`, results };
134
+ // ② 统一写盘
135
+ if (dryRun) return { ok: true, dryRun: true, results };
136
+ for (const plan of plans) writeFileSync(plan.abs, plan.next, 'utf8');
137
+ return { ok: true, results };
138
+ }
139
+
140
+ /** remove:移除指定文件的豁免(指定 marker 或该文件全部 dsh-skip)。 */
141
+ export function removeExempt({ repoPath = '', path: p, marker = '', dryRun = false } = {}) {
142
+ const paths = Array.isArray(p) ? p : [p];
143
+ const results = [];
144
+ for (const one of paths) {
145
+ const t = resolveTarget(repoPath, one);
146
+ if (!t.ok) { results.push({ path: String(one), ok: false, error: t.error }); continue; }
147
+ const text = readText(t.abs);
148
+ if (text === null) { results.push({ path: t.rel, ok: false, error: '文件不存在' }); continue; }
149
+ const re = marker ? new RegExp(`\\s*//\\s*dsh-skip-${marker.replace(/^dsh-skip-/, '')}(?::[^\\n]*)?`, 'g') : /\s*\/\/\s*dsh-skip-[a-z]+(?::[^\n]*)?/g;
150
+ const next = text.replace(re, '');
151
+ if (next === text) { results.push({ path: t.rel, ok: true, unchanged: true }); continue; }
152
+ if (!dryRun) writeFileSync(t.abs, next, 'utf8');
153
+ results.push({ path: t.rel, ok: true, changed: true });
154
+ }
155
+ const failed = results.filter((r) => r.ok === false);
156
+ if (failed.length) return { ok: false, error: `校验失败(${failed.length} 个文件)`, results };
157
+ return { ok: true, dryRun, results };
158
+ }
159
+
160
+ /** list:盘点仓库内所有 dsh-skip 标记(按文件 × 标记 × 位置)。 */
161
+ export function listExempt({ repoPath = '', root = '' } = {}) {
162
+ const base = resolve(root || repoPath);
163
+ const out = [];
164
+ const walk = (dir, depth) => {
165
+ if (depth > 8) return;
166
+ let entries = [];
167
+ try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }
168
+ for (const e of entries) {
169
+ if (e.name === 'node_modules' || e.name === '.git' || e.name === '.trash') continue;
170
+ const full = join(dir, e.name);
171
+ if (e.isDirectory()) { walk(full, depth + 1); continue; }
172
+ if (!/\.(js|mjs|cjs|ts|tsx|jsx|py|md)$/.test(e.name)) continue;
173
+ const text = readText(full);
174
+ if (!text) continue;
175
+ const lines = text.split('\n');
176
+ lines.forEach((row, i) => {
177
+ const m = row.match(/dsh-skip-([a-z]+)/);
178
+ if (!m) return;
179
+ out.push({ file: relative(base, full).split(sep).join('/'), line: i + 1, marker: `dsh-skip-${m[1]}`, scope: i < 3 ? 'header' : 'line' });
180
+ });
181
+ }
182
+ };
183
+ walk(base, 0);
184
+ return { ok: true, total: out.length, items: out };
185
+ }
186
+
187
+ /** check:校验每条标记是否真的能豁免(用同一判定器对 rule 反查,识别死标记/写错标记)。 */
188
+ export function checkExempt({ repoPath = '', root = '' } = {}) {
189
+ const listed = listExempt({ repoPath, root });
190
+ const issues = [];
191
+ for (const it of listed.items) {
192
+ const meta = EXEMPT_MARKERS[it.marker];
193
+ if (!meta) { issues.push({ ...it, issue: 'unknown-marker', hint: '标记不在注册表里(拼写错误?)' }); continue; }
194
+ // 用标记的 blocked 反查:该标记能豁免哪些 rule 前缀——至少应有一个(否则标记形同虚设)
195
+ if (!Array.isArray(meta.blocked) || !meta.blocked.length) issues.push({ ...it, issue: 'dead-marker', hint: '该标记没有可豁免的 blocked 类型' });
196
+ }
197
+ return { ok: issues.length === 0, total: listed.total, issues };
198
+ }
@@ -16,6 +16,8 @@ import { fetchCommitChain } from './clone-history.js';
16
16
  import { replayHistory } from './clone-replay.js';
17
17
  import { createReplayIo } from './clone-replay-io.js';
18
18
  import { runGit } from './exec.js';
19
+ import { resolveOwnerVisibility, accessPlan, sshGitUrl } from './visibility.js';
20
+ import { resolveSshKeys } from './credentials.js';
19
21
 
20
22
  /**
21
23
  * 取某分支的 HEAD sha。
@@ -105,12 +107,26 @@ export async function cloneWithHistory({
105
107
  * @returns {Promise<{ok:boolean,headSha:string,depth:number,truncated:boolean,steps:Array<object>,error?:string}>}
106
108
  */
107
109
  export async function cloneWithGitFetch({
108
- owner = '', repo = '', branch = '', repoPath = '', depth, deps = {},
110
+ owner = '', repo = '', branch = '', repoPath = '', depth, deps = {}, token = '',
109
111
  } = {}) {
110
112
  const git = deps.runGit || runGit;
111
- const gitOpts = { cwd: repoPath };
113
+ // 统一前置判断:私有/未知仓库不走第三方镜像——镜像看不到私有库,经它 fetch 必然认证失败。
114
+ // deps.visibility 供离线测试直接指定结果('public' | 'private' | 'unknown')。
115
+ const vis = deps.visibility ? { visibility: deps.visibility } : await resolveOwnerVisibility({ owner, repo, token });
116
+ const plan = accessPlan(vis.visibility);
117
+ // 私有/未知:远端用 SSH 形态(ssh.github.com:443),并注入插件 SSH 密钥;
118
+ // 公开:维持 https + 镜像加速(本机 github.com:443 直连不通,公开库靠镜像)。
119
+ const remoteUrl = plan.transport === 'ssh' ? sshGitUrl(owner, repo) : `https://github.com/${owner}/${repo}.git`;
120
+ const sshEnv = {};
121
+ if (plan.transport === 'ssh') {
122
+ const keyPath = (deps.sshKeyPath || resolveSshKeys()[0]?.keyPath || '');
123
+ if (keyPath) {
124
+ sshEnv.GIT_SSH_COMMAND = `ssh -i "${keyPath}" -p 443 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -o BatchMode=yes -o ConnectTimeout=8`;
125
+ }
126
+ }
127
+ const gitOpts = { cwd: repoPath, noMirror: !plan.useMirror, env: sshEnv };
112
128
  const steps = [];
113
- const fail = (error) => ({ ok: false, headSha: '', depth: Number(depth) || 0, truncated: false, steps, error });
129
+ const fail = (error) => ({ ok: false, headSha: '', depth: Number(depth) || 0, truncated: false, steps, error, visibility: vis.visibility, transport: plan.transport });
114
130
  const run = (args) => {
115
131
  const r = git(args, gitOpts);
116
132
  steps.push({ cmd: args.join(' '), ok: !!r?.ok });
@@ -123,7 +139,10 @@ export async function cloneWithGitFetch({
123
139
  // 不支持 mode 的卷(CIFS)上抑制权限噪音——与 API 路径同口径
124
140
  run(['config', 'core.fileMode', 'false']);
125
141
  if (!git(['remote', 'get-url', 'origin'], gitOpts)?.ok) {
126
- run(['remote', 'add', 'origin', `https://github.com/${owner}/${repo}.git`]);
142
+ run(['remote', 'add', 'origin', remoteUrl]);
143
+ } else {
144
+ // 已存在 origin(续跑):按本次判断纠正,避免上次留下的 https 形态继续走镜像
145
+ run(['remote', 'set-url', 'origin', remoteUrl]);
127
146
  }
128
147
 
129
148
  // 显式 refspec:同时写入 refs/remotes/origin/<branch>,后续可据此建本地分支并设上游
@@ -139,5 +158,5 @@ export async function cloneWithGitFetch({
139
158
  if (!co?.ok) return fail(`checkout 失败:${co?.stderr || '未知'}`);
140
159
  run(['branch', `--set-upstream-to=origin/${branch}`, branch]);
141
160
 
142
- return { ok: true, headSha: sha, depth: n, truncated: n > 0, steps };
161
+ return { ok: true, headSha: sha, depth: n, truncated: n > 0, steps, visibility: vis.visibility, transport: plan.transport };
143
162
  }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * dsh-repo-index 的 JSONL 存储格式(读写 + 旧 JSON 兼容)。
3
+ *
4
+ * 为什么改 JSONL(原为单份 pretty-print JSON 对象):
5
+ * ① **流式读写**:JSONL 每行一个独立对象,写入可以按条目逐行生成、读取可以逐行解析,
6
+ * 不必把整份文档拼成一个结构再 parse/stringify——索引随仓库数增长时这一点更重要;
7
+ * ② **损坏容错**:单份 JSON 只要有一处截断就整份不可用(parse 直接抛),
8
+ * 而 JSONL 可以跳过坏行、保留其余条目,索引不会因为一次半截写而全废;
9
+ * ③ **可增量**:单仓远端状态更新只需重写它自己那一行,语义上不再绑定「整份文档」。
10
+ *
11
+ * 文件形态(每行一个 JSON 对象,UTF-8,LF 结尾):
12
+ * {"_t":"meta","name":…,"description":…,"version":…,"generatedAt":…,"owner":…,"note":…}
13
+ * {"_t":"repo","name":…,"path":…,"repoUrl":…,"visibility":…,…} ← 每个仓库一行
14
+ * {"_t":"localOnly","path":"…"} ← 顶层无远端的目录
15
+ * {"_t":"recovery","text":"多行恢复说明"} ← 收尾(文本整体占一行)
16
+ *
17
+ * 兼容策略:
18
+ * - 读:优先 `dsh-repo-index.jsonl`;不存在则回退旧 `dsh-repo-index.json`(整份 JSON);
19
+ * 两种都能解析成同一个内存对象,调用方无感;
20
+ * - 写:一律写 `.jsonl`;首次写入时把旧的 `.json` 移入同级 `.trash/`(保留可恢复,
21
+ * 避免两份同源索引并存造成漂移)。
22
+ *
23
+ * 不变量:解析结果的对象形状与旧格式**完全一致**(name/description/version/generatedAt/
24
+ * owner/note/repos/localOnly/recovery),保证前端与其它读取方零改动。
25
+ */
26
+ import { existsSync, readFileSync, mkdirSync, renameSync, readdirSync } from 'node:fs';
27
+ import { join, dirname } from 'node:path';
28
+
29
+ import { writeTextAtomic } from '../core/atomic-json.js';
30
+
31
+ /** 新格式文件名(JSONL)。 */
32
+ export const INDEX_FILE = 'dsh-repo-index.jsonl';
33
+ /** 旧格式文件名(整份 JSON,仅读与迁移用)。 */
34
+ export const LEGACY_INDEX_FILE = 'dsh-repo-index.json';
35
+
36
+ /** 把索引对象序列化成 JSONL 文本(meta → repos → localOnly → recovery)。 */
37
+ export function serializeIndex(index) {
38
+ const o = index && typeof index === 'object' ? index : {};
39
+ const lines = [];
40
+ const meta = {
41
+ _t: 'meta',
42
+ name: o.name || '',
43
+ description: o.description || '',
44
+ version: o.version || '',
45
+ generatedAt: o.generatedAt || '',
46
+ owner: o.owner || '',
47
+ note: o.note || '',
48
+ };
49
+ lines.push(JSON.stringify(meta));
50
+ for (const r of Array.isArray(o.repos) ? o.repos : []) {
51
+ if (!r || typeof r !== 'object') continue;
52
+ lines.push(JSON.stringify({ _t: 'repo', ...r }));
53
+ }
54
+ for (const p of Array.isArray(o.localOnly) ? o.localOnly : []) {
55
+ lines.push(JSON.stringify({ _t: 'localOnly', path: String(p || '') }));
56
+ }
57
+ // recovery 是给人看的多行命令说明:整体作为一个字段,换行由 JSON 转义承载,仍只占一行
58
+ if (o.recovery !== undefined && o.recovery !== null && o.recovery !== '') {
59
+ lines.push(JSON.stringify({ _t: 'recovery', text: String(o.recovery) }));
60
+ }
61
+ return lines.join('\n') + '\n';
62
+ }
63
+
64
+ /** JSONL 文本 → 索引对象;坏行跳过并计数(不因单行损坏丢弃整份)。 */
65
+ export function parseIndex(text) {
66
+ const s = String(text || '');
67
+ const trimmed = s.trim();
68
+ if (!trimmed) return null;
69
+ // 旧格式(整份 JSON):以 { 或 [ 开头且能整体 parse —— 其中的行不是独立对象
70
+ if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
71
+ try {
72
+ const whole = JSON.parse(trimmed);
73
+ // 带 _t 的顶层对象不是旧格式(极少见),继续走 JSONL 分支
74
+ if (!(whole && typeof whole === 'object' && whole._t)) {
75
+ return Array.isArray(whole) ? { repos: whole } : whole;
76
+ }
77
+ } catch { /* 不是整份 JSON → 按 JSONL 解析 */ }
78
+ }
79
+ const out = { name: '', description: '', version: '', generatedAt: '', owner: '', note: '', repos: [], localOnly: [], recovery: '' };
80
+ let skipped = 0;
81
+ for (const raw of s.split('\n')) {
82
+ const line = raw.trim();
83
+ if (!line) continue;
84
+ let obj;
85
+ try { obj = JSON.parse(line); } catch { skipped += 1; continue; }
86
+ if (!obj || typeof obj !== 'object') { skipped += 1; continue; }
87
+ const t = obj._t;
88
+ if (t === 'meta') {
89
+ out.name = obj.name || out.name;
90
+ out.description = obj.description || out.description;
91
+ out.version = obj.version || out.version;
92
+ out.generatedAt = obj.generatedAt || out.generatedAt;
93
+ out.owner = obj.owner || out.owner;
94
+ out.note = obj.note || out.note;
95
+ } else if (t === 'repo') {
96
+ const { _t, ...rest } = obj;
97
+ out.repos.push(rest);
98
+ } else if (t === 'localOnly') {
99
+ if (obj.path) out.localOnly.push(String(obj.path));
100
+ } else if (t === 'recovery') {
101
+ out.recovery = String(obj.text || '');
102
+ } else {
103
+ skipped += 1;
104
+ }
105
+ }
106
+ if (skipped) out._skippedLines = skipped;
107
+ return out;
108
+ }
109
+
110
+ /**
111
+ * 读索引文件(JSONL 优先,旧 JSON 回退)。
112
+ * @returns {{index: object|null, path: string, format: 'jsonl'|'json'|'none'}}
113
+ */
114
+ export function readIndexFile(dir) {
115
+ const jsonlPath = join(dir, INDEX_FILE);
116
+ if (existsSync(jsonlPath)) {
117
+ try { return { index: parseIndex(readFileSync(jsonlPath, 'utf8')), path: jsonlPath, format: 'jsonl' }; } catch { /* 落到旧格式 */ }
118
+ }
119
+ const legacyPath = join(dir, LEGACY_INDEX_FILE);
120
+ if (existsSync(legacyPath)) {
121
+ try { return { index: parseIndex(readFileSync(legacyPath, 'utf8')), path: legacyPath, format: 'json' }; } catch { /* 无可用索引 */ }
122
+ }
123
+ return { index: null, path: '', format: 'none' };
124
+ }
125
+
126
+ /**
127
+ * 写入索引(一律 JSONL)。
128
+ *
129
+ * 旧 `.json` 的处理:首次成功写入 `.jsonl` 后,把旧文件移入同级 `.trash/<时间戳>-<名>`
130
+ * (可恢复),避免「两份同源索引并存」——两者不一致时无法判断哪份是真源。
131
+ * @returns {{ok:boolean, path:string, migratedFrom?:string}}
132
+ */
133
+ export function writeIndexFile(dir, index) {
134
+ const target = join(dir, INDEX_FILE);
135
+ const wr = writeTextAtomic(target, serializeIndex(index));
136
+ if (!wr.ok) return { ok: false, path: target };
137
+ const legacyPath = join(dir, LEGACY_INDEX_FILE);
138
+ let migratedFrom = '';
139
+ if (existsSync(legacyPath)) {
140
+ try {
141
+ const trashDir = join(dir, '.trash');
142
+ mkdirSync(trashDir, { recursive: true });
143
+ const stamp = new Date().toISOString().replace(/[:.]/g, '-');
144
+ let dest = join(trashDir, `${stamp}-${LEGACY_INDEX_FILE}`);
145
+ if (existsSync(dest)) dest = join(trashDir, `${stamp}-${process.pid}-${LEGACY_INDEX_FILE}`);
146
+ renameSync(legacyPath, dest);
147
+ migratedFrom = dest;
148
+ } catch { /* 移不动就留着(不影响 JSONL 读取优先级) */ }
149
+ }
150
+ return { ok: true, path: target, ...(migratedFrom ? { migratedFrom } : {}) };
151
+ }
152
+
153
+ /** 目录里是否存在任一格式的索引文件。 */
154
+ export function indexFileExists(dir) {
155
+ return existsSync(join(dir, INDEX_FILE)) || existsSync(join(dir, LEGACY_INDEX_FILE));
156
+ }
157
+
158
+ /** 找目录下的索引文件路径(JSONL 优先);都没有返回 null。 */
159
+ export function findIndexPath(dir) {
160
+ const jsonlPath = join(dir, INDEX_FILE);
161
+ if (existsSync(jsonlPath)) return jsonlPath;
162
+ const legacyPath = join(dir, LEGACY_INDEX_FILE);
163
+ if (existsSync(legacyPath)) return legacyPath;
164
+ return null;
165
+ }
166
+
167
+ /** 兼容旧调用:整份 JSON 文本(部分调用方只做正则扫描,JSONL 每行也是合法 JSON 片段)。 */
168
+ export function indexAsJsonText(index) {
169
+ return serializeIndex(index);
170
+ }
171
+
172
+ /** 列出目录里残留的索引文件(自检用:正常只有一个)。 */
173
+ export function listIndexFiles(dir) {
174
+ try {
175
+ return readdirSync(dir).filter((f) => f === INDEX_FILE || f === LEGACY_INDEX_FILE);
176
+ } catch { return []; }
177
+ }
178
+
179
+ /** 索引文件所在目录(便于测试与调试定位)。 */
180
+ export function indexDirOf(filePath) {
181
+ return dirname(String(filePath || ''));
182
+ }
package/lib/git/index.js CHANGED
@@ -38,4 +38,5 @@ export { listCloudRepos } from './cloud.js';
38
38
  export { checkGithubAccount, formatGithubAccountBlock, fetchApiQuota } from './account.js';
39
39
  export { writeAccountStatusFromResult, accountStatusFile, readAccountStatus, refreshAccountStatus, buildAccountStatus, updateAccountStatusQuota } from './account-status.js';
40
40
  export { locateRepoIndex, readRepoIndexMap, indexEntryForRepo, buildRepoIndex, maintainRepoIndex, updateRepoIndex, mergeCloudReposIntoIndex, updateRepoRemoteStateInIndex } from './repo-index.js';
41
+ export { INDEX_FILE, LEGACY_INDEX_FILE, serializeIndex, parseIndex, readIndexFile, writeIndexFile, indexFileExists, findIndexPath, listIndexFiles } from './index-format.js';
41
42
  export { scanRunner, runScan, waitScanDelta, readScanLive, resetScanLive, scanLiveFile, stopScanWatch } from './scan-runner.js';