dsh-git-push 0.0.0-stage → 2.8.4
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/.auditignore +30 -0
- package/.gitignore +39 -0
- package/LICENSE +21 -0
- package/README.md +1482 -2
- package/cli.mjs +360 -0
- package/cordis.patch.yml +14 -0
- package/lib/ARCHITECTURE.md +34 -0
- package/lib/BUGFIX-NOTES-2026-09-14.md +278 -0
- package/lib/app/apply.js +275 -0
- package/lib/app/audit-api.js +158 -0
- package/lib/app/command-registry.js +262 -0
- package/lib/app/constants.js +16 -0
- package/lib/app/handlers/account.js +166 -0
- package/lib/app/handlers/clone.js +168 -0
- package/lib/app/handlers/http-constants.js +9 -0
- package/lib/app/handlers/identity-rewrite.js +94 -0
- package/lib/app/handlers/meta.js +175 -0
- package/lib/app/handlers/npm.js +169 -0
- package/lib/app/handlers/repo-actions.js +143 -0
- package/lib/app/handlers/repos.js +231 -0
- package/lib/app/handlers/settings.js +62 -0
- package/lib/app/host-doc-checks.js +25 -0
- package/lib/app/http-handlers.js +131 -0
- package/lib/app/index.js +30 -0
- package/lib/app/inject-text.js +191 -0
- package/lib/app/scan-root.js +23 -0
- package/lib/app/schema.js +150 -0
- package/lib/app/settings-bridge.js +167 -0
- package/lib/app/slash-commands.js +395 -0
- package/lib/app/slot-stats.js +35 -0
- package/lib/app/tool-call.js +662 -0
- package/lib/app/tools.js +40 -0
- package/lib/ast/brace.js +105 -0
- package/lib/ast/callgraph.js +135 -0
- package/lib/ast/code-lines.js +114 -0
- package/lib/ast/consts.js +12 -0
- package/lib/ast/control-flow.js +461 -0
- package/lib/ast/credential.js +199 -0
- package/lib/ast/dataflow.js +274 -0
- package/lib/ast/dup-code.js +165 -0
- package/lib/ast/index.js +36 -0
- package/lib/ast/io-risk-const.js +95 -0
- package/lib/ast/io-risk-fn.js +208 -0
- package/lib/ast/io-risk-loop.js +308 -0
- package/lib/ast/io-risk.js +314 -0
- package/lib/ast/lang.js +156 -0
- package/lib/ast/line-count.js +52 -0
- package/lib/ast/magic-number.js +272 -0
- package/lib/ast/naming.js +284 -0
- package/lib/ast/perf.js +472 -0
- package/lib/ast/scope.js +241 -0
- package/lib/ast/shell.js +131 -0
- package/lib/ast/size.js +378 -0
- package/lib/ast/symbol-index.js +399 -0
- package/lib/ast/tokenizer.js +341 -0
- package/lib/audit/analysis-coverage.js +98 -0
- package/lib/audit/audit-file.js +182 -0
- package/lib/audit/checks.js +17 -0
- package/lib/audit/client-entry.js +93 -0
- package/lib/audit/collector.js +477 -0
- package/lib/audit/ext-runner.js +80 -0
- package/lib/audit/file-context.js +138 -0
- package/lib/audit/finding.js +50 -0
- package/lib/audit/gitignore-match.js +113 -0
- package/lib/audit/glob.js +63 -0
- package/lib/audit/history-report.js +100 -0
- package/lib/audit/history.js +126 -0
- package/lib/audit/ignore-blind.js +101 -0
- package/lib/audit/index.js +33 -0
- package/lib/audit/orchestrate.js +476 -0
- package/lib/audit/public.js +24 -0
- package/lib/audit/repo-level.js +30 -0
- package/lib/audit/report-yaml.js +86 -0
- package/lib/audit/slot.js +65 -0
- package/lib/audit-defaults.js +18 -0
- package/lib/audit-rules/audit-rules-comment.yml +247 -0
- package/lib/audit-rules/audit-rules-cpp.yml +22 -0
- package/lib/audit-rules/audit-rules-docs.yml +32 -0
- package/lib/audit-rules/audit-rules-dsh.yml +162 -0
- package/lib/audit-rules/audit-rules-filehealth.yml +42 -0
- package/lib/audit-rules/audit-rules-folder.yml +99 -0
- package/lib/audit-rules/audit-rules-frontend.yml +335 -0
- package/lib/audit-rules/audit-rules-go.yml +22 -0
- package/lib/audit-rules/audit-rules-i18n.yml +45 -0
- package/lib/audit-rules/audit-rules-java.yml +105 -0
- package/lib/audit-rules/audit-rules-nodejs.yml +647 -0
- package/lib/audit-rules/audit-rules-npm.yml +187 -0
- package/lib/audit-rules/audit-rules-paths.yml +92 -0
- package/lib/audit-rules/audit-rules-performance.yml +207 -0
- package/lib/audit-rules/audit-rules-php.yml +22 -0
- package/lib/audit-rules/audit-rules-private.yml +24 -0
- package/lib/audit-rules/audit-rules-python.yml +106 -0
- package/lib/audit-rules/audit-rules-robustness.yml +27 -0
- package/lib/audit-rules/audit-rules-rust.yml +22 -0
- package/lib/audit-rules/audit-rules-structure.yml +159 -0
- package/lib/audit-rules/audit-rules-swift.yml +22 -0
- package/lib/audit-rules/audit-rules-template.yml +108 -0
- package/lib/checks/button-bind.js +276 -0
- package/lib/checks/common.js +170 -0
- package/lib/checks/credential-file.js +43 -0
- package/lib/checks/dataflow.js +40 -0
- package/lib/checks/dispatch.js +112 -0
- package/lib/checks/dup-code.js +69 -0
- package/lib/checks/dup-const.js +232 -0
- package/lib/checks/file-health.js +150 -0
- package/lib/checks/filter.js +131 -0
- package/lib/checks/folder.js +173 -0
- package/lib/checks/hardcoded-path.js +98 -0
- package/lib/checks/index.js +46 -0
- package/lib/checks/io.js +66 -0
- package/lib/checks/magic-number.js +42 -0
- package/lib/checks/npm-json.js +273 -0
- package/lib/checks/private.js +63 -0
- package/lib/checks/regex.js +256 -0
- package/lib/checks/semantic.js +134 -0
- package/lib/checks/structural.js +274 -0
- package/lib/cli/commands-account.mjs +107 -0
- package/lib/cli/commands-audit.mjs +144 -0
- package/lib/cli/commands-doc.mjs +127 -0
- package/lib/cli/commands-vcs.mjs +175 -0
- package/lib/client/index.js +162 -0
- package/lib/client-parts/00-loader.js +33 -0
- package/lib/client-parts/01-const.js +207 -0
- package/lib/client-parts/02-utils.js +62 -0
- package/lib/client-parts/03-icons.js +12 -0
- package/lib/client-parts/10-account.js +83 -0
- package/lib/client-parts/11-browser.js +95 -0
- package/lib/client-parts/20-repo.js +374 -0
- package/lib/client-parts/21-audit.js +301 -0
- package/lib/client-parts/22-settings.js +146 -0
- package/lib/client-parts/23-npm.js +383 -0
- package/lib/client-parts/30-page.js +56 -0
- package/lib/client-parts/40-controller.js +1092 -0
- package/lib/client-parts/41-actions.js +279 -0
- package/lib/client-parts/99-apply.js +72 -0
- package/lib/client.js +3195 -0
- package/lib/commit-push.js +144 -0
- package/lib/context/index.js +363 -0
- package/lib/core/atomic-json.js +98 -0
- package/lib/core/config.js +40 -0
- package/lib/core/credentials.js +274 -0
- package/lib/core/endpoints.js +109 -0
- package/lib/core/exec.js +162 -0
- package/lib/exempt/index.js +292 -0
- package/lib/fs/edit-after-read.js +103 -0
- package/lib/fsx.js +87 -0
- package/lib/git/account-status.js +140 -0
- package/lib/git/account.js +233 -0
- package/lib/git/api.js +136 -0
- package/lib/git/atomic-json.js +6 -0
- package/lib/git/browse.js +38 -0
- package/lib/git/clone-download.js +397 -0
- package/lib/git/clone-history-mode.js +143 -0
- package/lib/git/clone-history.js +175 -0
- package/lib/git/clone-jobs.js +250 -0
- package/lib/git/clone-replay-io.js +172 -0
- package/lib/git/clone-replay.js +275 -0
- package/lib/git/clone.js +445 -0
- package/lib/git/cloud.js +46 -0
- package/lib/git/commit-message.js +141 -0
- package/lib/git/config.js +6 -0
- package/lib/git/cred-env.js +88 -0
- package/lib/git/credentials.js +6 -0
- package/lib/git/endpoints.js +6 -0
- package/lib/git/exec.js +6 -0
- package/lib/git/identity-rewrite.js +297 -0
- package/lib/git/identity.js +150 -0
- package/lib/git/ignore.js +69 -0
- package/lib/git/index.js +41 -0
- package/lib/git/module-splitter.js +47 -0
- package/lib/git/post-push.js +101 -0
- package/lib/git/push-diagnose.js +142 -0
- package/lib/git/push.js +470 -0
- package/lib/git/remote-resolve.js +146 -0
- package/lib/git/remote.js +128 -0
- package/lib/git/repo-index.js +512 -0
- package/lib/git/repos.js +235 -0
- package/lib/git/scan-runner.js +159 -0
- package/lib/git/sensitive.js +134 -0
- package/lib/git/transport.js +430 -0
- package/lib/git/visibility.js +35 -0
- package/lib/git/wrapped-git.js +68 -0
- package/lib/http/index.js +170 -0
- package/lib/index.js +24 -0
- package/lib/link-check/index.js +187 -0
- package/lib/npm/account.js +64 -0
- package/lib/npm/install.js +211 -0
- package/lib/npm/npmrc.js +56 -0
- package/lib/npm/publish.js +167 -0
- package/lib/npm/registry.js +147 -0
- package/lib/plugin/auto-detect.js +92 -0
- package/lib/plugin/auto-push.js +154 -0
- package/lib/plugin/index.js +250 -0
- package/lib/readme-templates/readme.yml +38 -0
- package/lib/rule/compilers/credential.js +62 -0
- package/lib/rule/compilers/dataflow.js +38 -0
- package/lib/rule/compilers/file-health.js +75 -0
- package/lib/rule/compilers/frontend.js +86 -0
- package/lib/rule/compilers/func.js +35 -0
- package/lib/rule/compilers/helpers.js +71 -0
- package/lib/rule/compilers/index.js +29 -0
- package/lib/rule/compilers/link-semantic.js +45 -0
- package/lib/rule/compilers/list-kinds.js +12 -0
- package/lib/rule/compilers/numeric-kinds.js +66 -0
- package/lib/rule/compilers/regex.js +78 -0
- package/lib/rule/compilers/structure.js +80 -0
- package/lib/rule/compilers.js +17 -0
- package/lib/rule/homoglyph.js +45 -0
- package/lib/rule/loader.js +270 -0
- package/lib/rule/registry.js +115 -0
- package/lib/rule/scope.js +67 -0
- package/lib/score/docs-score.js +166 -0
- package/lib/score/index.js +140 -0
- package/lib/self/index.js +193 -0
- package/lib/skip-dirs.js +64 -0
- package/lib/tool-probes.json +50 -0
- package/lib/user-requirements.json +14 -0
- package/lib/vendor/js-yaml/LICENSE +21 -0
- package/lib/vendor/js-yaml/js-yaml.mjs +3073 -0
- package/package.json +95 -4
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +34 -0
- package/scripts/audit-ext/variable-min-length.mjs +102 -0
- package/scripts/audit-runner.mjs +35 -0
- package/scripts/audit-runtime-check.mjs +129 -0
- package/scripts/browser-page-probe.mjs +271 -0
- package/scripts/build-client.mjs +119 -0
- package/scripts/check.mjs +88 -0
- package/scripts/clean-date-comments.py +102 -0
- package/scripts/doc-drift.mjs +200 -0
- package/scripts/doc-func.mjs +212 -0
- package/scripts/doc-tree.mjs +456 -0
- package/scripts/doc-version.mjs +220 -0
- package/scripts/frontend-preview-check.mjs +124 -0
- package/scripts/hot-path-probe-cjs.cjs +71 -0
- package/scripts/hot-path-probe-fs-shim.mjs +28 -0
- package/scripts/hot-path-probe-fsp-shim.mjs +31 -0
- package/scripts/hot-path-probe.mjs +153 -0
- package/scripts/module-splitter.py +800 -0
- package/scripts/platform-scan.mjs +168 -0
- package/scripts/preview-gen.mjs +489 -0
- package/scripts/preview-local-runner.mjs +55 -0
- package/scripts/preview-server-direct.mjs +116 -0
- package/scripts/preview-server.mjs +305 -0
- package/scripts/preview-slot-data.mjs +123 -0
- package/scripts/probe-recheck.mjs +47 -0
- package/scripts/readme-gen.mjs +268 -0
- package/scripts/rename-locator.mjs +97 -0
- package/scripts/rule-switch.mjs +97 -0
- package/scripts/rules-solo-audit.mjs +173 -0
- package/scripts/scan-file-io.mjs +713 -0
- package/scripts/scan-hardcoded-paths.mjs +125 -0
- package/scripts/scan-repos.mjs +167 -0
- package/scripts/scan-version.mjs +122 -0
- package/scripts/scrub-user-wording.mjs +420 -0
- package/scripts/symbol-index.mjs +85 -0
- package/scripts/sync-plugin-install.mjs +223 -0
- package/scripts/sync-plugin.mjs +279 -0
- package/scripts/verify-prestep.mjs +100 -0
- package/skills/dev/audit-rule-authoring.md +58 -0
- package/skills/dev/dsh-git-push.md +323 -0
- package/skills/dev/git-push-live-fix.md +45 -0
- package/skills/dsh-repo-index.md +35 -0
- package/skills/guide/guide-audit.md +167 -0
- package/skills/guide/guide-cli-scripts.md +104 -0
- package/skills/guide/guide-clone-repos.md +173 -0
- package/skills/guide/guide-commit-push.md +156 -0
- package/skills/guide/guide-inject-ui.md +166 -0
- package/skills/guide/guide-slash-completion.md +118 -0
- package/skills/workflow/README.md +21 -0
- package/skills/workflow/git-commit-discipline.md +68 -0
- package/skills/workflow/git-project-startup.md +85 -0
- package/skills/workflow/git-rebuild-process.md +57 -0
- package/skills/workflow/github-operations.md +91 -0
- package/skills/workflow/readme-sync-git-md.md +28 -0
- package/skills/workflow/task-completion-report.md +73 -0
- package/skills/workflow/tool-json-add-ask.md +35 -0
- package/skills/workflow/versioning-rule.md +211 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件入口层 · 用户输入框斜杠命令
|
|
3
|
+
*
|
|
4
|
+
* 官方通道:ctx.inject(['commands']) → commands.register({ name, description, input, handler })。
|
|
5
|
+
* 人在输入框打 /git-audit 等命令,直接跑代码、不进模型。
|
|
6
|
+
*
|
|
7
|
+
* ── 只注册「只读」命令 ──
|
|
8
|
+
* 这里挂的每条命令都零副作用:不写文件、不改远端、不发提交。写类操作
|
|
9
|
+
* (git_commit_push / git_remote_create / git_set_visibility)
|
|
10
|
+
* 刻意不挂——输入框一条命令就改远端太危险,仍走 agent 工具(有审计与门禁)。
|
|
11
|
+
* 唯一的例外是 /git-clone-preview:它只读远端树并回报「将下载什么」,不落盘。
|
|
12
|
+
*
|
|
13
|
+
* 默认仓库 = 当前会话工作区(invocation.agent.session.header.cwd),
|
|
14
|
+
* 与 dsh-session-conductor 分组同一字段:cwd 空 = 未分类,必须写路径。
|
|
15
|
+
* 禁止回落到 DSH 家根(无 .git 会走 auditFull,扫家根会把进程打爆)。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
19
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
20
|
+
import { callTool } from './tool-call.js';
|
|
21
|
+
|
|
22
|
+
/* ───────────────────────── 通用工具 ───────────────────────── */
|
|
23
|
+
|
|
24
|
+
/** 会话工作区路径。空 = 指挥家「未分类」(header.cwd 缺失)。 */
|
|
25
|
+
export function sessionCwdOf(invocation) {
|
|
26
|
+
const cwd = invocation?.agent?.session?.header?.cwd;
|
|
27
|
+
return typeof cwd === 'string' && cwd.trim() ? cwd.trim() : '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 从 dir 向上找含 .git 的仓库根。找不到返回空串。
|
|
32
|
+
* @param {string} dir
|
|
33
|
+
* @returns {string}
|
|
34
|
+
*/
|
|
35
|
+
/** git 仓库向上探测的最大深度(超过即放弃,防深目录逐级 existsSync 拖慢)。 */
|
|
36
|
+
const GIT_ROOT_MAX_DEPTH = 20;
|
|
37
|
+
const BLOCKERS_LIST_MAX = 20; // /git-audit 拦截文件列表上限
|
|
38
|
+
const PREVIEW_LIST_MAX = 15; // /git-clone-preview 预览列表条数上限(防刷屏)
|
|
39
|
+
const LIST_PRINT_MAX = 20; // 通用列表打印上限(拦截文件/坏链接等,防刷屏)
|
|
40
|
+
|
|
41
|
+
export function findGitRoot(dir) {
|
|
42
|
+
if (!dir) return '';
|
|
43
|
+
let cur = resolve(dir);
|
|
44
|
+
for (let i = 0; i < GIT_ROOT_MAX_DEPTH; i++) {
|
|
45
|
+
if (existsSync(join(cur, '.git'))) return cur;
|
|
46
|
+
const parent = dirname(cur);
|
|
47
|
+
if (parent === cur) return '';
|
|
48
|
+
cur = parent;
|
|
49
|
+
}
|
|
50
|
+
return '';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 解析目标路径。空路径 = 会话 cwd(不是 DSH 家根)。
|
|
55
|
+
* 相对路径接到会话 cwd;无会话 cwd 时相对路径无法解析。
|
|
56
|
+
* @returns {string} 绝对路径或空串
|
|
57
|
+
*/
|
|
58
|
+
export function resolveTargetPath(rawPath, { sessionCwd = '' } = {}) {
|
|
59
|
+
const trimmed = String(rawPath || '').trim();
|
|
60
|
+
if (!trimmed) return sessionCwd || '';
|
|
61
|
+
if (isAbsolute(trimmed)) return trimmed;
|
|
62
|
+
if (!sessionCwd) return '';
|
|
63
|
+
return resolve(sessionCwd, trimmed);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 通用参数解析:抽出路径与已知 flag。
|
|
68
|
+
* 非 flag 的连续 token 拼成路径(允许带空格未加引号)。
|
|
69
|
+
* flag 兼容单横线:`-full`/`--full` 等价(用户常敲单横线,缺路径时避免被当路径报「路径不存在」)。
|
|
70
|
+
* @param {string} raw
|
|
71
|
+
* @param {string[]} flags 允许的 flag(如 ['--full','--json'])
|
|
72
|
+
* @param {string} usage
|
|
73
|
+
*/
|
|
74
|
+
export function parseCommandInput(raw = '', flags = [], usage = '') {
|
|
75
|
+
const tokens = String(raw || '').trim().split(/\s+/).filter(Boolean);
|
|
76
|
+
const opts = {};
|
|
77
|
+
const pathParts = [];
|
|
78
|
+
for (const t of tokens) {
|
|
79
|
+
// 横线参数(--full / -full 均识别;未知横线参数报用法,不当路径)
|
|
80
|
+
if (/^--?[a-zA-Z]/.test(t)) {
|
|
81
|
+
const canon = (t.startsWith('--') ? t : `-${t}`);
|
|
82
|
+
if (!flags.includes(canon) && !flags.includes(t)) return { path: '', opts, error: `未知参数 ${t}。${usage}` };
|
|
83
|
+
const flagName = canon.replace(/^-+/, ''); // --full / -full → full
|
|
84
|
+
opts[flagName] = true;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
pathParts.push(t);
|
|
88
|
+
}
|
|
89
|
+
return { path: pathParts.join(' '), opts };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 把「不是 git 仓库」这类拒绝理由统一成带用法的错误。 */
|
|
93
|
+
function rejectRepo(usage, example, detail) {
|
|
94
|
+
return { kind: 'error', text: `${detail}\n${usage}${example ? '\n' + example : ''}` };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* ───────────────────────── /git-audit ───────────────────────── */
|
|
98
|
+
|
|
99
|
+
const AUDIT_USAGE = '用法: /git-audit [路径] [--full] [--force]';
|
|
100
|
+
const AUDIT_EXAMPLE = '示例: /git-audit /path/to/repo --full;/git-audit /path/to/dir --force(非 git 目录强制全量扫描)';
|
|
101
|
+
/** 路径不存在错误前缀(4 个命令共用,统一文案防漂移)。 */
|
|
102
|
+
const ERR_PATH_NOT_FOUND = '路径不存在: ';
|
|
103
|
+
|
|
104
|
+
/** 兼容旧名(外部/测试可能仍在用)。 */
|
|
105
|
+
export function parseGitAuditInput(raw = '') {
|
|
106
|
+
const parsed = parseCommandInput(raw, ['--full', '--force'], AUDIT_USAGE);
|
|
107
|
+
if (parsed.error) return { path: '', scope: 'diff', error: parsed.error };
|
|
108
|
+
return { path: parsed.path, scope: parsed.opts.full ? 'full' : 'diff', force: parsed.opts.force === true };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 兼容旧名。 */
|
|
112
|
+
export function resolveAuditRepo(rawPath, opts) {
|
|
113
|
+
return resolveTargetPath(rawPath, opts);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 把 code_audit 结果收成输入框可显示的短文本(含 blocker 文件列表)。
|
|
118
|
+
* @param {object} r callTool('code_audit') 返回值
|
|
119
|
+
*/
|
|
120
|
+
export function formatGitAuditCommandText(r = {}) {
|
|
121
|
+
if (!r || r.ok === false) return r?.error || '审计失败';
|
|
122
|
+
// 修(回归):code_audit 1.11.5 后返回改版(无 block/findings——默认精简)——
|
|
123
|
+
// 旧实现依赖 r.block/r.findings → lines 空 → 输出「审计完成(无摘要)」。改用新返回
|
|
124
|
+
// summary(计数)+ quality(评分)+ blocked(拦截列表,含 2.0.4 includeFindings 前的结构)。
|
|
125
|
+
const lines = [];
|
|
126
|
+
const s = r.summary || {};
|
|
127
|
+
const quality = r.quality || {};
|
|
128
|
+
lines.push(`审计 ${r.repo || ''}(${r.scope === 'full' ? '全量' : '变动'})`);
|
|
129
|
+
lines.push(`评分 ${quality.score ?? '?'}/${quality.level ?? '?'} · ${s.blocker ?? 0} 拦截 / ${s.warning ?? 0} 警告 / ${s.notice ?? 0} 提示`);
|
|
130
|
+
const blockers = Array.isArray(r.blocked) ? r.blocked : [];
|
|
131
|
+
if (blockers.length) {
|
|
132
|
+
lines.push(`拦截 ${blockers.length} 个:`);
|
|
133
|
+
for (const b of blockers.slice(0, BLOCKERS_LIST_MAX)) {
|
|
134
|
+
const loc = b.line ? `${b.file}:${b.line}` : String(b.file || '');
|
|
135
|
+
lines.push(` ${loc}(${b.rule || ''})`);
|
|
136
|
+
}
|
|
137
|
+
if (blockers.length > BLOCKERS_LIST_MAX) lines.push(` …另有 ${blockers.length - BLOCKERS_LIST_MAX} 条`);
|
|
138
|
+
}
|
|
139
|
+
return lines.join('\n') || '审计完成';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** /git-audit handler 主体(可单测,不依赖 cordis)。 */
|
|
143
|
+
export async function runGitAuditCommand({ rawInput = '', invocation = null, env = {}, cfg = {}, log = null } = {}) {
|
|
144
|
+
const parsed = parseGitAuditInput(rawInput);
|
|
145
|
+
if (parsed.error) return { kind: 'error', text: parsed.error };
|
|
146
|
+
const sessionCwd = sessionCwdOf(invocation);
|
|
147
|
+
if (!parsed.path && !sessionCwd) {
|
|
148
|
+
return { kind: 'error', text: `当前会话未绑定工作区(未分类)。请指定 git 仓库路径。\n${AUDIT_USAGE}\n${AUDIT_EXAMPLE}` };
|
|
149
|
+
}
|
|
150
|
+
if (parsed.path && !isAbsolute(parsed.path) && !sessionCwd) {
|
|
151
|
+
return { kind: 'error', text: `相对路径需要会话工作区。请给绝对路径。\n${AUDIT_USAGE}\n${AUDIT_EXAMPLE}` };
|
|
152
|
+
}
|
|
153
|
+
const target = resolveTargetPath(parsed.path, { sessionCwd });
|
|
154
|
+
if (!target || !existsSync(target)) return { kind: 'error', text: `${ERR_PATH_NOT_FOUND}${target || '(空)'}` };
|
|
155
|
+
let repo = findGitRoot(target);
|
|
156
|
+
if (!repo) {
|
|
157
|
+
// 非 git 目录默认拒绝(全量扫非仓库目录可能打爆进程);--force 显式放行——
|
|
158
|
+
// code_audit 工具对非 git 目录自动走 auditFull(scope 强制 full,不依赖 git 元数据)
|
|
159
|
+
if (!parsed.force) {
|
|
160
|
+
return rejectRepo(AUDIT_USAGE, AUDIT_EXAMPLE, `不是 git 仓库: ${target}。拒绝全量扫非仓库目录(会把进程打爆);加 --force 强制扫描。`);
|
|
161
|
+
}
|
|
162
|
+
repo = target;
|
|
163
|
+
}
|
|
164
|
+
const r = await callTool('code_audit', { repo, scope: parsed.scope }, env, cfg, log);
|
|
165
|
+
if (!r || r.ok === false) return { kind: 'error', text: r?.error || '审计失败' };
|
|
166
|
+
return { kind: 'success', text: formatGitAuditCommandText(r) };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/* ───────────────────────── /git-scan ───────────────────────── */
|
|
170
|
+
|
|
171
|
+
const SCAN_USAGE = '用法: /git-scan [路径]';
|
|
172
|
+
const SCAN_EXAMPLE = '示例: /git-scan /volume1/.../工作区';
|
|
173
|
+
|
|
174
|
+
/** 把扫描结果收成输入框可显示的短文本。 */
|
|
175
|
+
export function formatGitScanCommandText(r = {}) {
|
|
176
|
+
if (!r || r.ok === false) return r?.error || '扫描失败';
|
|
177
|
+
const repos = Array.isArray(r.repos) ? r.repos : [];
|
|
178
|
+
if (!repos.length) return `扫描根: ${r.root}\n未发现 git 仓库。`;
|
|
179
|
+
const lines = [`扫描根: ${r.root}`, `共 ${repos.length} 个仓库:`, ''];
|
|
180
|
+
for (const x of repos) {
|
|
181
|
+
const dirty = Number(x.changed) || 0;
|
|
182
|
+
const ahead = Number(x.ahead) || 0;
|
|
183
|
+
const mark = dirty || ahead ? '●' : '○';
|
|
184
|
+
const parts = [`${mark} ${x.name}`, `[${x.branch || '(空仓)'}]`];
|
|
185
|
+
if (dirty) parts.push(`未提交 ${dirty}`);
|
|
186
|
+
if (ahead) parts.push(`未推送 ${ahead}`);
|
|
187
|
+
lines.push(' ' + parts.join(' '));
|
|
188
|
+
}
|
|
189
|
+
const dirtyCount = repos.filter((x) => Number(x.changed) > 0).length;
|
|
190
|
+
if (dirtyCount) lines.push('', `其中 ${dirtyCount} 个仓库有未提交改动。`);
|
|
191
|
+
return lines.join('\n');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export async function runGitScanCommand({ rawInput = '', invocation = null, env = {}, cfg = {}, log = null } = {}) {
|
|
195
|
+
const parsed = parseCommandInput(rawInput, [], SCAN_USAGE);
|
|
196
|
+
if (parsed.error) return { kind: 'error', text: parsed.error };
|
|
197
|
+
const sessionCwd = sessionCwdOf(invocation);
|
|
198
|
+
const target = resolveTargetPath(parsed.path, { sessionCwd });
|
|
199
|
+
if (parsed.path && !target) {
|
|
200
|
+
return { kind: 'error', text: `相对路径需要会话工作区。请给绝对路径。\n${SCAN_USAGE}` };
|
|
201
|
+
}
|
|
202
|
+
if (target && !existsSync(target)) return { kind: 'error', text: `${ERR_PATH_NOT_FOUND}${target}` };
|
|
203
|
+
// 不传 root → 用插件配置的默认扫描根(与 git_scan 工具同源)
|
|
204
|
+
const args = target ? { root: target } : {};
|
|
205
|
+
const r = await callTool('git_scan', args, env, cfg, log);
|
|
206
|
+
if (!r || r.ok === false) return { kind: 'error', text: r?.error || '扫描失败' };
|
|
207
|
+
return { kind: 'success', text: formatGitScanCommandText(r) };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/* ───────────────────────── /git-io-scan ───────────────────────── */
|
|
211
|
+
|
|
212
|
+
const IOSCAN_USAGE = '用法: /git-io-scan [路径] [--write]';
|
|
213
|
+
const IOSCAN_EXAMPLE = '示例: /git-io-scan --write';
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 把 I/O 扫描结果收成输入框可显示的短文本。
|
|
217
|
+
* 数据形态:{ total, sync, byRisk:{high,medium,low,safe}, files:[{file,count}], items:[...] }
|
|
218
|
+
*/
|
|
219
|
+
export function formatIoScanCommandText(r = {}) {
|
|
220
|
+
if (!r || r.ok === false) return r?.error || 'I/O 扫描失败';
|
|
221
|
+
const total = Number(r.total) || 0;
|
|
222
|
+
if (!total) return `扫描根: ${r.root || '(会话工作区)'}\n未发现文件 I/O 调用。`;
|
|
223
|
+
const b = r.byRisk || {};
|
|
224
|
+
const lines = [
|
|
225
|
+
`扫描根: ${r.root || '(会话工作区)'}`,
|
|
226
|
+
`文件 I/O 调用 ${total} 处(同步 ${r.sync || 0} / 异步 ${total - (r.sync || 0)})`,
|
|
227
|
+
'',
|
|
228
|
+
'风险分布:',
|
|
229
|
+
` 🔴 高 ${b.high || 0} 🟠 中 ${b.medium || 0} 🟡 低 ${b.low || 0} 🟢 安全 ${b.safe || 0}`,
|
|
230
|
+
];
|
|
231
|
+
const items = Array.isArray(r.items) ? r.items : [];
|
|
232
|
+
const top = items.filter((hit) => hit.risk === 'high').slice(0, 10);
|
|
233
|
+
if (top.length) {
|
|
234
|
+
lines.push('', '高风险(改造优先级):');
|
|
235
|
+
for (const hit of top) {
|
|
236
|
+
const ctxs = [];
|
|
237
|
+
if (hit.inAsync) ctxs.push('异步路径');
|
|
238
|
+
if (hit.inLoop) ctxs.push('循环内');
|
|
239
|
+
if (hit.inRequest) ctxs.push('请求路径');
|
|
240
|
+
lines.push(` ${hit.file}:${hit.line} ${hit.call || hit.op || ''} ${ctxs.join('+') || '—'}`);
|
|
241
|
+
}
|
|
242
|
+
const highTotal = b.high || 0;
|
|
243
|
+
if (highTotal > top.length) lines.push(` …另有 ${highTotal - top.length} 处高危`);
|
|
244
|
+
}
|
|
245
|
+
return lines.join('\n');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* /git-io-scan handler:扫描脚本里读写文件的调用与路径。
|
|
250
|
+
* 复用 scripts/scan-file-io.mjs(AST 四级分级,与审计 io-risk 同标准),
|
|
251
|
+
* 不另写一套扫描逻辑。
|
|
252
|
+
*/
|
|
253
|
+
export async function runGitIoScanCommand({ rawInput = '', invocation = null, env = {}, cfg = {}, log = null } = {}) {
|
|
254
|
+
const parsed = parseCommandInput(rawInput, ['--write'], IOSCAN_USAGE);
|
|
255
|
+
if (parsed.error) return { kind: 'error', text: parsed.error };
|
|
256
|
+
const sessionCwd = sessionCwdOf(invocation);
|
|
257
|
+
const target = resolveTargetPath(parsed.path, { sessionCwd });
|
|
258
|
+
if (parsed.path && !target) {
|
|
259
|
+
return { kind: 'error', text: `相对路径需要会话工作区。请给绝对路径。\n${IOSCAN_USAGE}` };
|
|
260
|
+
}
|
|
261
|
+
if (target && !existsSync(target)) return { kind: 'error', text: `${ERR_PATH_NOT_FOUND}${target}` };
|
|
262
|
+
const r = await callTool('io_scan', { repo: target || sessionCwd, writeOnly: parsed.opts.write === true }, env, cfg, log);
|
|
263
|
+
if (!r || r.ok === false) return { kind: 'error', text: r?.error || 'I/O 扫描失败' };
|
|
264
|
+
return { kind: 'success', text: formatIoScanCommandText(r) };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/* ───────────────────────── /link-check ───────────────────────── */
|
|
268
|
+
|
|
269
|
+
const LINK_USAGE = '用法: /link-check [文件或目录]';
|
|
270
|
+
const LINK_EXAMPLE = '示例: /link-check README.md';
|
|
271
|
+
|
|
272
|
+
/** 把链接检查结果收成短文本。 */
|
|
273
|
+
export function formatLinkCheckCommandText(r = {}) {
|
|
274
|
+
if (!r || r.ok === false) return r?.error || '链接检查失败';
|
|
275
|
+
const findings = Array.isArray(r.findings) ? r.findings : [];
|
|
276
|
+
if (!findings.length) return `共检查链接 ${r.count || 0} 个,全部有效。`;
|
|
277
|
+
const bad = findings.filter((f) => f && f.ok === false);
|
|
278
|
+
const lines = [`共检查 ${r.count || findings.length} 个链接,其中 ${bad.length} 个无效:`, ''];
|
|
279
|
+
for (const f of bad.slice(0, LIST_PRINT_MAX)) {
|
|
280
|
+
lines.push(` ✗ ${f.url || f.link || '(空)'}${f.error ? ` — ${f.error}` : ''}`);
|
|
281
|
+
}
|
|
282
|
+
if (bad.length > LIST_PRINT_MAX) lines.push(` …另有 ${bad.length - LIST_PRINT_MAX} 个`);
|
|
283
|
+
return lines.join('\n');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export async function runLinkCheckCommand({ rawInput = '', invocation = null, env = {}, cfg = {}, log = null } = {}) {
|
|
287
|
+
const parsed = parseCommandInput(rawInput, [], LINK_USAGE);
|
|
288
|
+
if (parsed.error) return { kind: 'error', text: parsed.error };
|
|
289
|
+
const sessionCwd = sessionCwdOf(invocation);
|
|
290
|
+
const target = resolveTargetPath(parsed.path, { sessionCwd }) || env.workspaceRoot || '';
|
|
291
|
+
if (target && !existsSync(target)) return { kind: 'error', text: `${ERR_PATH_NOT_FOUND}${target}` };
|
|
292
|
+
const r = await callTool('link_check', { path: target }, env, cfg, log);
|
|
293
|
+
if (!r || r.ok === false) return { kind: 'error', text: r?.error || '链接检查失败' };
|
|
294
|
+
return { kind: 'success', text: formatLinkCheckCommandText(r) };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/* ───────────────────────── /git-account ───────────────────────── */
|
|
298
|
+
|
|
299
|
+
const ACCOUNT_USAGE = '用法: /git-account';
|
|
300
|
+
|
|
301
|
+
export async function runGitAccountCommand({ env = {}, cfg = {}, log = null } = {}) {
|
|
302
|
+
const r = await callTool('git_account_check', {}, env, cfg, log);
|
|
303
|
+
if (!r || r.ok === false) return { kind: 'error', text: r?.error || '账号校验失败' };
|
|
304
|
+
return { kind: 'success', text: String(r.block || '账号校验完成(无摘要)') };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/* ───────────────────────── /git-clone-preview ───────────────────────── */
|
|
308
|
+
|
|
309
|
+
const PREVIEW_USAGE = '用法: /git-clone-preview <owner/repo 或 URL> [--branch 名]';
|
|
310
|
+
const PREVIEW_EXAMPLE = '示例: /git-clone-preview EIGHTfs/dsh-git-push';
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 把 clone 预览收成短文本(只读:不下载、不落盘)。
|
|
314
|
+
*/
|
|
315
|
+
export function formatClonePreviewCommandText(r = {}) {
|
|
316
|
+
if (!r || r.ok === false) return r?.error || '预览失败';
|
|
317
|
+
const willDownload = Array.isArray(r.willDownload) ? r.willDownload : (Array.isArray(r.download) ? r.download : []);
|
|
318
|
+
const skipped = Array.isArray(r.skipped) ? r.skipped : [];
|
|
319
|
+
const lines = [
|
|
320
|
+
`${r.owner || ''}/${r.repo || ''}${r.branch ? ` @ ${r.branch}` : ''}`,
|
|
321
|
+
`将下载 ${willDownload.length} 个文件 · 约 ${r.downloadMB != null ? r.downloadMB + ' MB' : '(未知)'}`,
|
|
322
|
+
];
|
|
323
|
+
for (const f of willDownload.slice(0, PREVIEW_LIST_MAX)) { // 预览列表条数上限(防刷屏)
|
|
324
|
+
const size = f.size != null ? `${(f.size / 1024 / 1024).toFixed(1)}MB` : '';
|
|
325
|
+
lines.push(` 下载 ${f.path || f} ${size}`);
|
|
326
|
+
}
|
|
327
|
+
if (willDownload.length > PREVIEW_LIST_MAX) lines.push(` …另有 ${willDownload.length - PREVIEW_LIST_MAX} 个`);
|
|
328
|
+
if (skipped.length) {
|
|
329
|
+
lines.push(`跳过 ${skipped.length} 个(超体积上限):`);
|
|
330
|
+
for (const f of skipped.slice(0, 10)) {
|
|
331
|
+
const size = f.size != null ? `${(f.size / 1024 / 1024).toFixed(1)}MB` : '';
|
|
332
|
+
lines.push(` 跳过 ${f.path || f} ${size}`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (!willDownload.length) lines.push('', '⚠ 所有文件都会被跳过——继续克隆将得到空仓库。');
|
|
336
|
+
return lines.join('\n');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export async function runGitClonePreviewCommand({ rawInput = '', env = {}, cfg = {}, log = null } = {}) {
|
|
340
|
+
const parsed = parseCommandInput(rawInput, ['--branch'], PREVIEW_USAGE);
|
|
341
|
+
if (parsed.error) return { kind: 'error', text: parsed.error };
|
|
342
|
+
const tokens = String(rawInput || '').trim().split(/\s+/).filter(Boolean);
|
|
343
|
+
const bi = tokens.indexOf('--branch');
|
|
344
|
+
const branch = bi >= 0 ? (tokens[bi + 1] || '') : '';
|
|
345
|
+
const target = parsed.path || branch ? parsed.path : '';
|
|
346
|
+
if (!target) return { kind: 'error', text: `请给 owner/repo 或 URL。\n${PREVIEW_USAGE}\n${PREVIEW_EXAMPLE}` };
|
|
347
|
+
const r = await callTool('git_clone_preview', { target, branch }, env, cfg, log);
|
|
348
|
+
if (!r || r.ok === false) return { kind: 'error', text: r?.error || '预览失败' };
|
|
349
|
+
return { kind: 'success', text: formatClonePreviewCommandText(r) };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/* ───────────────────────── 注册 ───────────────────────── */
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* 命令清单。每条都零副作用(不写文件、不改远端)。
|
|
356
|
+
* keep: 便于测试断言注册集合,避免漏挂/多挂。
|
|
357
|
+
*/
|
|
358
|
+
export const SLASH_COMMANDS = [
|
|
359
|
+
{ name: 'git-audit', description: '审计当前会话工作区(未分类须写路径;--full 全量)', input: { hint: '[路径] [--full]' }, run: runGitAuditCommand },
|
|
360
|
+
{ name: 'git-scan', description: '列出各仓库分支/未提交/未推送(只读)', input: { hint: '[路径]' }, run: runGitScanCommand },
|
|
361
|
+
{ name: 'git-io-scan', description: '扫描脚本里读写文件的调用与路径,标四级风险(只读)', input: { hint: '[路径] [--write]' }, run: runGitIoScanCommand },
|
|
362
|
+
{ name: 'link-check', description: '检查文档内链接有效性(只读)', input: { hint: '[文件或目录]' }, run: runLinkCheckCommand },
|
|
363
|
+
{ name: 'git-account', description: '校验 GitHub 账号与凭据(只读)', input: { hint: '' }, run: runGitAccountCommand },
|
|
364
|
+
{ name: 'git-clone-preview', description: '预览 clone 将下载/跳过哪些文件,不落盘(只读)', input: { hint: '<owner/repo> [--branch 名]' }, run: runGitClonePreviewCommand },
|
|
365
|
+
];
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* 注册斜杠命令。commands 服务缺失时静默跳过(无 UI 的宿主不挂命令适配器)。
|
|
369
|
+
* @returns {number} 注册成功条数
|
|
370
|
+
*/
|
|
371
|
+
export function registerSlashCommands(ctx, { env = {}, cfg = {}, log = null } = {}) {
|
|
372
|
+
if (typeof ctx?.inject !== 'function') return 0;
|
|
373
|
+
let count = 0;
|
|
374
|
+
ctx.inject(['commands'], (cctx) => {
|
|
375
|
+
const commands = cctx?.commands || cctx?.get?.('commands');
|
|
376
|
+
if (!commands?.register) {
|
|
377
|
+
try { log?.warn?.('dsh-git-push: commands 服务不可用,斜杠命令未注册'); } catch { /* 日志失败不影响接线 */ }
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
for (const cmd of SLASH_COMMANDS) {
|
|
381
|
+
try {
|
|
382
|
+
commands.register({
|
|
383
|
+
name: cmd.name,
|
|
384
|
+
description: cmd.description,
|
|
385
|
+
input: cmd.input,
|
|
386
|
+
handler: (invocation) => cmd.run({ rawInput: invocation?.rawInput, invocation, env, cfg, log }),
|
|
387
|
+
});
|
|
388
|
+
count += 1;
|
|
389
|
+
} catch (e) {
|
|
390
|
+
try { log?.warn?.(`dsh-git-push: /${cmd.name} 注册失败: ${e?.message || e}`); } catch { /* 日志通道不可用时降级跳过 */ }
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
return count;
|
|
395
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件入口层 · 槽位命中统计
|
|
3
|
+
*
|
|
4
|
+
* 记录最近一次审计各规则槽位的命中数与时间/仓库(模块级可变状态,故集中一处,
|
|
5
|
+
* 避免散落多个模块各存一份导致读到旧值)。callTool 写入,handleHttp 读取回显。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/* ───────────── 最近一次审计的按规则包命中数缓存 ─────────────
|
|
9
|
+
* 侧边栏规则包列表的「拦截 / 警告 / 通过」要显示实际审计命中数,但 HTTP
|
|
10
|
+
* /rule-slots 是独立分发(不共享工具调用的调用栈/局部状态),故用模块级缓存:
|
|
11
|
+
* code_audit 每次审计后写入(setLastSlotHitStats),接口读取(getLastSlotHitStats)。
|
|
12
|
+
*/
|
|
13
|
+
let lastSlotHitStats = null;
|
|
14
|
+
|
|
15
|
+
let lastSlotHitAt = null;
|
|
16
|
+
|
|
17
|
+
let lastSlotHitRepo = '';
|
|
18
|
+
|
|
19
|
+
/** 写入最近一次审计的按规则包命中数(code_audit 审计后调用)。 */
|
|
20
|
+
export function setLastSlotHitStats(stats, repo = '') {
|
|
21
|
+
if (!stats || typeof stats !== 'object') return;
|
|
22
|
+
lastSlotHitStats = stats;
|
|
23
|
+
lastSlotHitAt = new Date().toISOString();
|
|
24
|
+
lastSlotHitRepo = String(repo || '');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 读最近一次审计的按规则包命中数(无审计记录时返回 null → 前端回退规则条数口径)。 */
|
|
28
|
+
export function getLastSlotHitStats() {
|
|
29
|
+
return lastSlotHitStats;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 读最近一次审计命中数的时间与仓库(供状态接口展示「统计时间」)。 */
|
|
33
|
+
export function getLastSlotHitMeta() {
|
|
34
|
+
return { at: lastSlotHitAt, repo: lastSlotHitRepo };
|
|
35
|
+
}
|