Merge pull request #910 from UNscientific-9/fix/diff-hunk-header-guard

fix(diff): 改动块内的 `---`/`+++` 不再当文件头,删掉 `-- …` 行不再截断 diff
This commit is contained in:
Atlas Zeng authored and GitHub committed 2026-10-12 01:21:16 +08:00
commit 23598d1f51
2 files changed
+84 -2

No files matched your search

+9 -2
View File
@@ -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
}
+75
View File
@@ -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')