Skip to content

fix(diff): 头部 +n/−m 改为 git 口径,一处改写不再两侧各计一次 - #665

Merged
Menghuan1918 merged 1 commit into
omdsh-dev:mainfrom
yanzhaohui1999:fix/diff-stats-mod-pair
Oct 4, 2026
Merged

Menghuan1918 merged 1 commit into
omdsh-dev:mainfrom
yanzhaohui1999:fix/diff-stats-mod-pair

Conversation

@yanzhaohui1999

Copy link
Copy Markdown
Contributor

问题

diffStats(src/client/diff/rows.ts:504)数的是渲染行,而 pairMods(rows.ts:48-64)把一处改写拆成两行 mod(旧侧一行、新侧一行),函数对每一行都 added += 1; deleted += 1 —— 一处改写被记成 +2 −2。

两个参照都不支持这个数字:

参照 一处改写
git diff --numstat 1 / 1
屏幕上画出来的 一行 -old + 一行 +new = 1 删 1 增
本函数 2 / 2

连它自己 JSDoc 声称的「行数」口径也不自洽:一对改写只有 2 行,却使 added 和 deleted 各 +2(合计 4)。测试里那条 counts header stats with mods on both sides(tests/diff-rows.spec.ts:96)是把现状写成了期望,不是外部契约。

用户可见的影响是头部数字与 git 对不上账。实测 taktop_redo 8b4b933,git show --shortstat 是 3299 insertions(+), 1 deletion(-),插件头部显示 +3300 −2;该 commit 的 CURRENT_STATUS.md 芯片显示 +9 −2,numstat 是 8 1。差值恰好等于改写行数(1 处 → 每列各多 1)。

头部数字来自 DiffPane.tsx:403-413 对 diffStats 的求和,不是 git 的 shortstat,所以这 1 是插件自己算出来的。

修复

按「行在哪一侧」计数,而不是每行乘 2:

const onNewSide = row.kind === 'add' || (row.kind === 'mod' && row.newLine !== undefined)
const onOldSide = row.kind === 'del' || (row.kind === 'mod' && row.oldLine !== undefined)
if (onNewSide) added += 1
if (onOldSide) deleted += 1

pairMods 不动:一处改写仍然渲染两行(旧侧、新侧,带 intra-line 高亮),只是头部不再把一对当两对。修后 8b4b933 头部 = +3299 −1,CURRENT_STATUS.md 芯片 = +8 −1;同一函数的会话 op 摘要(DiffPane.tsx:346)随之落到同一口径(单行改写 +2 −2 → +1 −1)。

测试

  • tests/diff-rows.spec.ts 的 mods on both sides 期望 {5, 4} → {3, 2}(同一 fixture:两处改写 + 1 行纯新增),用例名与注释改为描述 git 口径;
  • 新增:单行改写 → {1, 1}(回归主断言,注释里写明 numstat 对照值);会话 op 车道(diffLines)→ {1, 1} 与 {1, 0};
  • 纯新增(untracked 回退)保持 {2, 0} 不变。

验证

  • pnpm vitest run tests/diff-rows.spec.ts;pnpm typecheck / pnpm lint / pnpm build 绿
  • 全量 pnpm test:1315 passed,33 failed 全部是 tests/agent-pty.spec.ts + tests/smoke.spec.ts 的 posix_spawnp failed(PTY 在本机沙箱起不来,origin/main 裸基线重跑同样 33 个失败)
  • 真机:8b4b933 头部与 git show --shortstat 一致

文档同步

无。

diffStats counted ROWS, treating every `mod` row as both an insertion and
a deletion — but pairMods turns one rewritten line into two `mod` rows
(one per side), so a single-line rewrite reported +2 -2. Neither reference
agrees with that: `git diff --numstat` says 1/1, and the renderer draws
exactly one `-old` plus one `+new` line for it.

Count sides instead: an `add` row, or the new side of a `mod` row, is one
insertion; a `del` row, or the old side, is one deletion. On the commit
that surfaced this (taktop_redo 8b4b933) the changes-tab header read
+3300 -2 while `git show --shortstat` printed "3299 insertions(+),
1 deletion(-)"; it now reads +3299 -1, and CURRENT_STATUS.md's chip reads
+8 -1 against numstat's `8 1`. The session-op lane (the same function over
diffLines rows) moves to the same accounting.

tests/diff-rows.spec.ts replaces the "mods on both sides" expectation
(5/4 -> 3/2 for the same fixture) and adds a single-line-rewrite case plus
a session-op case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/git Git panel: status, branches, commit, worktree, multi-repo bug Something isn't working P3 低:体验/建议

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants