diff --git a/app/Dockerfile b/app/Dockerfile index 9534e18..f93c5a0 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -2,6 +2,22 @@ FROM node:20-slim ENV TZ=Asia/Shanghai +# git:Git 面板(提交历史 / 未提交改动)要读仓库,本机与容器共用同一条代码路径。 +# 国内构建慢:apt 默认源在国外,apt-get update 能卡十几分钟。默认换成腾讯云镜像(与 executor 同源)。 +# 境外构建或想换源:docker compose build --build-arg DEBIAN_MIRROR=deb.debian.org +ARG DEBIAN_MIRROR=mirrors.cloud.tencent.com + +# 换 apt 源:bookworm 可能是 /etc/apt/sources.list,也可能是 deb822 的 /etc/apt/sources.list.d/*.sources, +# 直接 grep 扫一遍、只替换域名,两种格式都盖住(路径 /debian、/debian-security 保持不变)。 +RUN set -eux; \ + files="$(grep -rl 'deb.debian.org' /etc/apt/ 2>/dev/null || true)"; \ + if [ -n "$files" ]; then sed -i "s|deb.debian.org|${DEBIAN_MIRROR}|g" $files; fi + +# 装完清 apt 列表,不给镜像多留几十 MB。 +RUN apt-get update \ + && apt-get install -y --no-install-recommends git \ + && rm -rf /var/lib/apt/lists/* + WORKDIR /app # 国内构建慢:npm 默认源在国外。默认换成腾讯云 npm 镜像(与宿主机 ~/.npmrc 一致)。 diff --git a/app/server/git.js b/app/server/git.js new file mode 100644 index 0000000..1a6e1cb --- /dev/null +++ b/app/server/git.js @@ -0,0 +1,263 @@ +// 只读 git 数据层:仓库探测 / 提交列表 / 提交详情 / 未提交改动详情。 +// 一律 execFile + 参数数组(不经 shell),只跑 rev-parse / status / log / show / diff,不碰任何写操作。 +// -c core.quotepath=false:中文路径原样返回,不转义成 \346\226\207 那种八进制。 +const { execFile } = require('child_process'); +const fs = require('fs'); + +const TIMEOUT = 8000; +const MAX_BUFFER = 8 * 1024 * 1024; +const PATCH_FILE_MAX = 120 * 1024; // 单文件 diff 字符上限 +const PATCH_TOTAL_MAX = 400 * 1024; // 整页 diff 字符上限 +const UNTRACKED_MAX_BYTES = 512 * 1024; // 未跟踪文件超过此大小不生成 diff +const MAX_FILES = 300; // 一次最多返回的文件数 + +const BASE = ['-c', 'core.quotepath=false']; + +function run(dir, args) { + return new Promise((resolve, reject) => { + // cwd 不存在时 execFile 也报 ENOENT,会和「没装 git」混为一谈,先自己挡掉 + let isDir = false; + try { isDir = fs.statSync(dir).isDirectory(); } catch (_) { isDir = false; } + if (!isDir) return reject(Object.assign(new Error('会话目录不存在'), { reason: 'not_repo' })); + execFile('git', BASE.concat(args), { cwd: dir, timeout: TIMEOUT, maxBuffer: MAX_BUFFER }, (err, stdout, stderr) => { + if (!err) return resolve(stdout); + err.stdout = String(stdout || ''); + err.stderr = String(stderr || '').trim(); + reject(err); + }); + }); +} + +function reasonOf(err) { + if (err && err.code === 'ENOENT') return 'no_git'; + if (err && err.code === 'ETIMEDOUT') return 'timeout'; + return 'not_repo'; +} + +// 仓库信息:root 用 --show-toplevel(会话目录可能是仓库子目录);空仓库没有 HEAD,不算错。 +async function ensureRepo(dir) { + let root; + try { + root = (await run(dir, ['rev-parse', '--show-toplevel'])).trim(); + } catch (err) { + throw Object.assign(new Error(err.code === 'ENOENT' ? '未安装 git' : '当前目录不是 git 仓库'), { reason: reasonOf(err) }); + } + if (!root) throw Object.assign(new Error('当前目录不是 git 仓库'), { reason: 'not_repo' }); + let branch = ''; + let head = ''; + try { branch = (await run(root, ['symbolic-ref', '--short', '-q', 'HEAD'])).trim(); } catch (_) { /* detached */ } + try { head = (await run(root, ['rev-parse', '--short', 'HEAD'])).trim(); } catch (_) { /* 空仓库 */ } + return { root, branch: branch || (head ? 'detached' : ''), head }; +} + +// 把一个整段 diff 按 `diff --git` 切开,键是文件路径。 +// 路径优先取 `+++ b/...`(改后),删除文件取 `--- a/...`;都没有就等于没有内容变化(仅 mode 变更)。 +function patchPath(body) { + const plus = body.match(/^\+\+\+ b\/(.+)$/m); + if (plus) return plus[1].replace(/\t$/, ''); + const minus = body.match(/^--- a\/(.+)$/m); + if (minus) return minus[1].replace(/\t$/, ''); + const head = body.match(/^diff --git a\/(.+) b\/(.+)$/m); + return head ? head[2] : null; +} + +function splitPatch(text) { + const out = new Map(); + if (!text) return out; + const parts = String(text).split(/^diff --git /m); + for (let i = 1; i < parts.length; i++) { + const body = 'diff --git ' + parts[i]; + const p = patchPath(body); + if (p) out.set(p, body); + } + return out; +} + +function countLines(patch) { + let adds = 0; + let dels = 0; + for (const line of String(patch).split('\n')) { + if (line.charAt(0) === '+' && line.charAt(1) !== '+') adds++; + else if (line.charAt(0) === '-' && line.charAt(1) !== '-') dels++; + } + return { adds, dels }; +} + +function statusOf(patch, fallback) { + if (/^new file mode /m.test(patch)) return 'A'; + if (/^deleted file mode /m.test(patch)) return 'D'; + if (/^rename from /m.test(patch) || /^copy from /m.test(patch)) return 'R'; + if (/^Binary files .* differ$/m.test(patch) || /^GIT binary patch$/m.test(patch)) return fallback || 'M'; + return fallback || 'M'; +} + +function isBinary(patch) { + return /^Binary files .* differ$/m.test(patch) || /^GIT binary patch$/m.test(patch); +} + +// 裁剪 + 汇总。逐文件先裁,再按总量裁(超出的文件保留路径与统计,patch 置空并标 truncated)。 +function buildFiles(map, order, extra) { + const files = []; + const byPath = new Map(); + let budget = PATCH_TOTAL_MAX; + for (const p of order) { + if (files.length >= MAX_FILES) break; + const raw = map.get(p) || ''; + const binary = isBinary(raw); + let patch = raw; + let truncated = false; + if (patch.length > PATCH_FILE_MAX) { patch = patch.slice(0, PATCH_FILE_MAX); truncated = true; } + if (patch.length > budget) { patch = ''; truncated = true; } + budget -= patch.length; + const counts = binary ? { adds: 0, dels: 0 } : countLines(patch); + const item = { + path: p, + status: extra && extra[p] ? extra[p].status : statusOf(raw, extra && extra[p] && extra[p].status), + adds: counts.adds, + dels: counts.dels, + binary: binary, + truncated: truncated, + patch: binary ? '' : patch, + }; + if (extra && extra[p] && extra[p].old_path) item.old_path = extra[p].old_path; + files.push(item); + byPath.set(p, item); + } + return files; +} + +function summarize(files) { + let adds = 0; + let dels = 0; + for (const f of files) { adds += f.adds; dels += f.dels; } + return { stats: { files: files.length, adds, dels } }; +} + +// git status --porcelain -z:拿未跟踪文件和状态字母(已跟踪文件的 patch 统一从 `diff HEAD` 拆) +// -z 必须加:不加引号规则下含空格的路径会被写成 "a b.txt",和 diff 里的裸路径对不上,会多出幽灵条目。 +// -z 里改名的旧路径是紧随其后的一条 NUL 分隔记录。 +function worktreeEntries(out) { + const entries = []; + const parts = String(out || '').split('\0'); + for (let i = 0; i < parts.length; i++) { + const line = parts[i]; + if (!line || line.length < 4) continue; + const xy = line.slice(0, 2); + const p = line.slice(3); + let oldPath = ''; + if (/[RC]/.test(xy)) { + oldPath = parts[i + 1] || ''; + i++; + } + let status = 'M'; + if (xy === '??') status = 'U'; + else if (xy.indexOf('A') >= 0) status = 'A'; + else if (xy.indexOf('D') >= 0) status = 'D'; + else if (xy.indexOf('R') >= 0) status = 'R'; + entries.push({ path: p, old_path: oldPath || undefined, status }); + } + return entries; +} + +// 未跟踪文件没有 diff:用 --no-index 对 /dev/null 造一个(退出码 1 = 有差异,属正常) +async function untrackedPatch(root, rel) { + const abs = root + '/' + rel; + try { + const st = fs.statSync(abs); + if (!st.isFile() || st.size > UNTRACKED_MAX_BYTES) return { patch: '', binary: st.size > UNTRACKED_MAX_BYTES }; + } catch (_) { return { patch: '', binary: false }; } + try { + return { patch: await run(root, ['diff', '--no-index', '--', '/dev/null', rel]), binary: false }; + } catch (err) { + if (err.code === 1) return { patch: err.stdout, binary: false }; + return { patch: '', binary: false }; + } +} + +async function worktreeDetail(root) { + let entries = []; + try { entries = worktreeEntries(await run(root, ['status', '--porcelain', '-z'])); } + catch (_) { entries = []; } + let all = ''; + try { all = await run(root, ['diff', 'HEAD']); } catch (_) { all = ''; } // 空仓库没有 HEAD + const map = splitPatch(all); + const extra = {}; + for (const e of entries) extra[e.path] = e; + const order = []; + for (const e of entries) order.push(e.path); + for (const p of map.keys()) if (!extra[p]) { extra[p] = { status: 'M' }; order.push(p); } + for (const e of entries) { + if (e.status !== 'U' || map.has(e.path)) continue; + const r = await untrackedPatch(root, e.path); + if (r.patch) map.set(e.path, r.patch); + else if (r.binary) extra[e.path].binary = true; + } + const files = buildFiles(map, order, extra); + return { kind: 'worktree', commit: null, files: files, ...summarize(files) }; +} + +// git log:before 是上一页最后一条的 commit id,用 `^` 翻页(git 天然不含自身)。 +async function listCommits(dir, before, limit) { + const repo = await ensureRepo(dir); + const n = Math.max(1, Math.min(100, limit || 30)); + const args = ['log', '--max-count=' + (n + 1), '--pretty=format:%H%x1f%h%x1f%an%x1f%aI%x1f%s']; + if (before) args.push(before + '^', '--'); + let out = ''; + try { out = await run(repo.root, args); } catch (err) { + if (before) return { ok: true, repo, worktree: null, commits: [], next: null }; // 已经翻到底 + if (reasonOf(err) === 'no_git') throw err; + return { ok: true, repo, worktree: null, commits: [], next: null }; // 空仓库 + } + const rows = out.split('\n').filter(Boolean).map((line) => { + const f = line.split('\x1f'); + return { id: f[0], short: f[1], author: f[2], time: f[3], subject: f[4] || '' }; + }); + const hasMore = rows.length > n; + const commits = hasMore ? rows.slice(0, n) : rows; + const next = hasMore && commits.length ? commits[commits.length - 1].id : null; + let worktree = null; + try { + const st = await run(repo.root, ['status', '--porcelain']); + worktree = { files: st.split('\n').filter(Boolean).length }; + } catch (_) { worktree = null; } + return { ok: true, repo, worktree, commits, next }; +} + +async function commitDetail(dir, rev) { + const repo = await ensureRepo(dir); + if (rev === 'worktree') { + const d = await worktreeDetail(repo.root); + return { ok: true, repo, ...d }; + } + let meta; + try { + meta = await run(repo.root, ['show', '-s', '--first-parent', '--format=%H%x1f%h%x1f%an%x1f%ae%x1f%aI%x1f%P%x1f%s%x1f%b', rev]); + } catch (err) { + throw Object.assign(new Error('提交不存在或读取失败'), { reason: 'bad_rev' }); + } + const f = meta.split('\x1f'); + const commit = { + id: f[0], short: f[1], author: f[2], email: f[3], time: f[4], + parents: String(f[5] || '').split(' ').filter(Boolean), + subject: f[6] || '', body: (f[7] || '').replace(/\s+$/, ''), + }; + // --first-parent:合并提交默认不出 patch,跟第一个父提交比才是"这次合并带进来什么" + let all = ''; + try { all = await run(repo.root, ['show', '--first-parent', '--format=', '--patch', rev]); } catch (_) { all = ''; } + const map = splitPatch(all); + const order = [...map.keys()]; + const files = buildFiles(map, order, null); + return { ok: true, repo, kind: 'commit', commit, files, ...summarize(files) }; +} + +// 统一入口:ok:false 只用于「不是仓库 / 没装 git / 会话不存在」,前端按 reason 出空状态 +async function query(dir, kind, opts) { + try { + if (kind === 'commits') return await listCommits(dir, opts && opts.before, opts && opts.limit); + return await commitDetail(dir, (opts && opts.rev) || ''); + } catch (err) { + return { ok: false, reason: (err && err.reason) || 'not_repo', error: String((err && err.message) || err) }; + } +} + +module.exports = { query }; diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index f11f3e9..38f376b 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -1640,6 +1640,73 @@ body.pv-resizing #pv-pane iframe { pointer-events: none; } .pv-none button { margin-top: 8px; border: 0; background: var(--active); color: #fff; border-radius: 8px; padding: 9px 22px; font-size: 13px; cursor: pointer; } .pv-none button:hover { opacity: .88; } +/* ---------- Git 面板(复用预览栏:body.pv-mode-vcs。列表 / 提交详情 / 未提交改动)---------- */ +/* 文件树头的 Git 钮开启时高亮用通用的 .icon-btn.active(如显示隐藏文件那种),不另造类 */ +.vcs-list { padding-bottom: 24px; } +/* 列表顶部的仓库行:会话目录可能只是仓库的某个子目录,所以显式写出 root */ +.vcs-repo { padding: 9px 16px; border-bottom: 1px solid var(--border); background: var(--drawer-bg); + font: 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--gray); + overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +.vcs-empty { padding: 44px 24px; text-align: center; font-size: 13px; color: var(--gray-light); } +.vcs-item { padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; } +.vcs-item:hover { background: var(--surface); } +.vcs-item .t { font-size: 13.5px; font-weight: 600; color: var(--ink); word-break: break-word; line-height: 1.45; } +.vcs-item .m { margin-top: 3px; font-size: 12px; color: var(--gray); } +.vcs-item .m code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } +/* 「未提交的改动」入口:列表顶部单独一行,与提交行区分 */ +.vcs-item.wt { background: var(--active-light); } +.vcs-item.wt:hover { background: #e3ecfe; } +.vcs-more { display: block; margin: 14px auto 0; padding: 7px 18px; font-size: 12.5px; color: var(--ink); + background: var(--page); border: 1px solid var(--border); border-radius: 8px; cursor: pointer; } +.vcs-more:hover { background: var(--surface); } +.vcs-back { display: flex; align-items: center; gap: 6px; padding: 8px 14px; font-size: 12.5px; color: var(--active); + background: var(--page); border-bottom: 1px solid var(--border); cursor: pointer; position: sticky; top: 0; z-index: 2; } +.vcs-back:hover { background: var(--surface); } +.vcs-commit { padding: 12px 16px 14px; border-bottom: 1px solid var(--border); background: var(--drawer-bg); } +.vcs-subject { font-size: 14px; font-weight: 700; line-height: 1.5; word-break: break-word; } +.vcs-meta { margin-top: 6px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 12px; color: var(--gray); } +.vcs-meta code { padding: 1px 6px; border-radius: 5px; background: var(--surface); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } +.vcs-meta .vcs-hash { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--active); } +.vcs-err-t { max-width: 420px; } +.vcs-msg { margin: 8px 0 0; font: 12.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; color: #444; } +.vcs-sum { display: flex; align-items: center; gap: 10px; padding: 8px 16px; font-size: 12px; color: var(--gray); border-bottom: 1px solid var(--border); } +.vcs-sum b.a { color: #1a7f37; } +.vcs-sum b.d { color: #cf222e; } +.vcs-file { border-bottom: 1px solid var(--border); } +.vcs-fh { display: flex; align-items: center; gap: 8px; padding: 8px 12px; font-size: 12.5px; cursor: pointer; user-select: none; } +.vcs-fh:hover { background: var(--surface); } +.vcs-caret { flex: none; width: 10px; color: var(--gray); transition: transform .12s ease; } +.vcs-file.open .vcs-caret { transform: rotate(90deg); } +.vcs-path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; + font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--ink); } +.vcs-badge { flex: none; padding: 0 5px; border-radius: 4px; font-size: 11px; line-height: 17px; background: var(--surface); color: var(--gray); } +.vcs-badge.m { color: #9a6700; background: #fff8c5; } +.vcs-badge.a { color: #1a7f37; background: #dafbe1; } +.vcs-badge.d { color: #cf222e; background: #ffebe9; } +.vcs-badge.r { color: #0550ae; background: #ddf4ff; } +.vcs-badge.u { color: #6639ba; background: #f3e8ff; } +.vcs-stat { flex: none; font-size: 11.5px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } +.vcs-stat .a { color: #1a7f37; } +.vcs-stat .d { color: #cf222e; } +.vcs-fb { display: none; border-top: 1px solid var(--border); overflow-x: auto; background: #fff; } +.vcs-file.open .vcs-fb { display: block; } +/* 行:旧号 / 新号 / 代码三列。代码不换行,横向滚动(diff 语义上就该逐行对齐) */ +.vcs-line { display: grid; grid-template-columns: 44px 44px minmax(0, 1fr); + font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } +.vcs-line .n { padding: 0 6px; text-align: right; color: #b0b6bd; user-select: none; + background: #fafbfc; border-right: 1px solid rgba(0,0,0,.04); } +.vcs-line .c { padding: 0 8px; white-space: pre; color: var(--ink); } +.vcs-line.a { background: #e6ffec; } +.vcs-line.a .n { background: #ccffd8; } +.vcs-line.d { background: #ffebe9; } +.vcs-line.d .n { background: #ffd7d5; } +.vcs-line.h { background: #ddf4ff; } +.vcs-line.h .c { color: #0550ae; } +.vcs-line.h .n { background: #ddf4ff; } +.vcs-line.meta .c { color: var(--gray); font-style: italic; } +.vcs-nodiff { padding: 12px 16px; font-size: 12px; color: var(--gray); } +.vcs-note { padding: 8px 16px; font-size: 12px; color: var(--gray); background: var(--drawer-bg); } + /* 通用 UI 卡片:SSE ui_card → 聊天流可点卡片(动作注册表驱动,见 preview.js) */ .ui-card { display: flex; align-items: center; gap: 10px; max-width: 420px; margin: 8px 0; padding: 10px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--page); cursor: pointer; } diff --git a/app/server/public/index.html b/app/server/public/index.html index a45e8fb..d4b3ed6 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -553,6 +553,7 @@ +