From 9f1724e3df043b8840b2acdfa1b27006baafd682 Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Sat, 10 Oct 2026 20:34:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(diff):=20=E6=94=B9=E5=8A=A8=E5=9D=97?= =?UTF-8?q?=E5=86=85=E7=9A=84=20`---`/`+++`=20=E4=B8=8D=E5=86=8D=E5=BD=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B4=EF=BC=8C=E5=88=A0=E6=8E=89=20`--=20?= =?UTF-8?q?=E2=80=A6`=20=E8=A1=8C=E4=B8=8D=E5=86=8D=E6=88=AA=E6=96=AD=20di?= =?UTF-8?q?ff?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除行的 diff 原文是 `-` 加源码原文,所以删掉一行以「`-- `」(两个减号加 一个空格)开头的内容——SQL / Lua / Haskell 注释、文档或脚本里的命令行参数 行——会产出 `--- foo`,与文件头分隔行 `--- a/路径` 逐字节同形。解析器把 「在 hunk 内」那道闸门排在 `---`/`+++` 两个分支之后,等于没保护它们:命中 后当前 hunk 当场被截断(该行之后没有 `@@` 可重新同步时,剩下的改动整段不 显示),并把被删文本盖到 oldPath 上。新增行原文以 `++ ` 开头(C 的 `++ i;`)同理会把 newPath 改成 `i;` 并丢掉那一行。 git 从不在 hunk 内写 `---`/`+++`,所以判据是「在不在 hunk 内」:两个分支 各加 !inHunk 守卫(等价于把它们挪到那道闸门之后)。 实测(同一份 fixture,修复前 → 修复后,直接跑真实 parseUnifiedDiff): 删掉 `-- foo`:oldPath `"foo"` → `"a/sample.txt"`;hunk 1 行 → 3 行 新增 `++ i;`:newPath `"i;"` → `"b/loop.c"`;新增行丢失 → 保留 统计与 `git diff --numstat` 一致(0/1 与 1/0) 用例:`tests/diff-rows.spec.ts` 新增一组 3 例(删除行、新增行,以及 hunk 之后的真文件头仍要认得——守护这道守卫别把正常头也挡掉)。 --- src/client/diff/rows.ts | 11 ++++-- tests/diff-rows.spec.ts | 75 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/client/diff/rows.ts b/src/client/diff/rows.ts index d52111ac..4a85e792 100644 --- a/src/client/diff/rows.ts +++ b/src/client/diff/rows.ts @@ -431,6 +431,13 @@ function pathsFromBinaryLine(raw: string): { oldPath: string; newPath: string } * binary file, a pure rename and a mode-only change, so without those * fallbacks the section renders as an anonymous header (the badge with no * file name the reader cannot place). + * + * Both of those header branches are gated on `!inHunk`, because a hunk line can + * wear the same prefix: a deleted line is `-` plus its text, so deleting `-- x` + * emits `--- x` — a hunk line that starts exactly like the separator, and one + * that used to be read as a NEW file's header (truncating the hunk here, then + * stamping the deleted text onto the path). The hunk is the tiebreaker: git + * never writes `---`/`+++` inside one. */ export function parseUnifiedDiff(text: string): ParsedDiff { const files: DiffFile[] = [] @@ -477,12 +484,12 @@ export function parseUnifiedDiff(text: string): ParsedDiff { } continue } - if (raw.startsWith('--- ')) { + if (!inHunk && raw.startsWith('--- ')) { flushHunk() current.oldPath = headerPath(raw.slice(4)) continue } - if (raw.startsWith('+++ ')) { + if (!inHunk && raw.startsWith('+++ ')) { current.newPath = headerPath(raw.slice(4)) continue } diff --git a/tests/diff-rows.spec.ts b/tests/diff-rows.spec.ts index fb11b7f4..908679a1 100644 --- a/tests/diff-rows.spec.ts +++ b/tests/diff-rows.spec.ts @@ -347,6 +347,81 @@ describe('parseUnifiedDiff paths', () => { }) }) +describe('hunk lines that wear a file-header prefix', () => { + // A deleted line is `-` plus its text, so deleting `-- foo` emits `--- foo`: + // a hunk line that starts exactly like the `--- a/path` separator. Reading it + // as a header cut the hunk there (everything after it vanished when no later + // `@@` resynced) and stamped the deleted text onto the file's old path. The + // mirror case is an added `++ i;` (C's increment), which renamed the file. + // git never writes `---`/`+++` INSIDE a hunk — that is the tiebreaker. + it('keeps a deleted `-- foo` line inside its hunk, path and stats intact', () => { + // The deleted source line is `-- foo`, so the diff row is `-` + that text — + // i.e. `--- foo`, byte-for-byte the shape of a `--- a/path` header. + const file = parseUnifiedDiff([ + 'diff --git a/sample.txt b/sample.txt', + '--- a/sample.txt', + '+++ b/sample.txt', + '@@ -1,3 +1,2 @@', + ' keep 1', + '--- foo', + ' keep 3', + ].join('\n')).files[0]! + expect({ oldPath: file.oldPath, newPath: file.newPath, hunks: file.hunks.length }).toEqual({ + oldPath: 'a/sample.txt', + newPath: 'b/sample.txt', + hunks: 1, + }) + // All three rows survive, in order, with the deleted line's own text (a + // row's text is what follows its marker, so a context row keeps no leading + // space and the deleted line keeps both of its dashes). + expect(file.hunks[0]!.lines.map(line => `${line.kind}:${line.text}`)) + .toEqual(['ctx:keep 1', 'del:-- foo', 'ctx:keep 3']) + // `git diff --numstat` for this change is `0 1`: the row the old parser + // swallowed is the only change in the file. + expect(diffStats(unifiedSegments(file))).toEqual({ added: 0, deleted: 1 }) + }) + + it('keeps an added `++ i;` line inside its hunk and does not rename the file', () => { + // The added source line is `++ i;`, so the diff row is `+` + that text. + const file = parseUnifiedDiff([ + 'diff --git a/loop.c b/loop.c', + '--- a/loop.c', + '+++ b/loop.c', + '@@ -1,2 +1,3 @@', + ' for (;;) {', + '+++ i;', + ' }', + ].join('\n')).files[0]! + expect({ newPath: file.newPath, hunks: file.hunks.length }) + .toEqual({ newPath: 'b/loop.c', hunks: 1 }) + expect(file.hunks[0]!.lines.map(line => `${line.kind}:${line.text}`)) + .toEqual(['ctx:for (;;) {', 'add:++ i;', 'ctx:}']) + }) + + it('still reads a real header that follows a hunk (the guard is the hunk, not the prefix)', () => { + // The second file's `---`/`+++` arrive while the first file's hunk is still + // open (nothing flushed it yet) — exactly the state the gate must let pass. + const parsed = parseUnifiedDiff([ + 'diff --git a/a.txt b/a.txt', + '--- a/a.txt', + '+++ b/a.txt', + '@@ -1,1 +1,1 @@', + '-old', + '+new', + 'diff --git a/b/-- deeper.txt b/b/-- deeper.txt', + '--- a/b/-- deeper.txt', + '+++ b/b/-- deeper.txt', + '@@ -1,1 +1,1 @@', + '-gone', + '+kept', + ].join('\n')) + expect(parsed.files.map(file => [file.oldPath, file.newPath, file.hunks.length])).toEqual([ + ['a/a.txt', 'b/a.txt', 1], + ['a/b/-- deeper.txt', 'b/b/-- deeper.txt', 1], + ]) + }) +}) + describe('decodeGitPath', () => { it('decodes git octal escapes as UTF-8 bytes', () => { expect(decodeGitPath('"b/docs/\\346\\226\\207.md"')).toBe('b/docs/文.md')