fix(diff): 改动块内的 ---/+++ 不再当文件头,删掉 -- … 行不再截断 diff
删除行的 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 之后的真文件头仍要认得——守护这道守卫别把正常头也挡掉)。
This commit is contained in:
1 parent
1184d063c9
commit
9f1724e3df
2 files changed
+84
-2
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user