From 291a6ce4f041b7f8e2b6eb355674bd64f2584d82 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Sat, 7 Nov 2026 10:17:01 +0100 Subject: [PATCH] Name files whose diffs have no hunks Empty new or deleted files and mode-only changes produce no "---" and "+++" lines, which were the only source of file names, so their header showed no name. A deleted empty file also got the green "added" marker, since both names were empty. Take the name from the "diff --git" line when both halves name the same file, and use "new file mode" / "deleted file mode" to mark which side is missing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Y1vvcRJ5ENt6vxWCCT4WbJ --- src/__snapshots__/index.test.ts.snap | 132 +++++++++++++++++++++++++-- src/index.test.ts | 25 +++++ src/iterSideBySideDiffs.ts | 35 ++++++- 3 files changed, 185 insertions(+), 7 deletions(-) diff --git a/src/__snapshots__/index.test.ts.snap b/src/__snapshots__/index.test.ts.snap index 8049f4c..2956cbe 100644 --- a/src/__snapshots__/index.test.ts.snap +++ b/src/__snapshots__/index.test.ts.snap @@ -138,7 +138,7 @@ Date: Sun Apr 18 03:17:05 2021 -0700 Update main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ■■ + ■■ .github/workflows/main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ■■ scripts/screenshotTheme @@ -169,6 +169,26 @@ Date: Sat Apr 10 22:26:15 2021 -0700 " `; +exports[`inlineChangesHighlighted commit with files without hunks 1`] = ` +" +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ old empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ script.sh +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +" +`; + exports[`inlineChangesHighlighted commit with inline line changes 1`] = ` " commit 9e424d329800e945e3003c9e275f80bdef69e591 @@ -595,7 +615,7 @@ Date: Sun Apr 18 03:17:05 2021 -0700 Update main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ■■ + ■■ .github/workflows/main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ■■ scripts/screenshotTheme @@ -626,6 +646,26 @@ Date: Sat Apr 10 22:26:15 2021 -0700 " `; +exports[`splitWithWrapping commit with files without hunks 1`] = ` +" +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ old empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ script.sh +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +" +`; + exports[`splitWithWrapping commit with inline line changes 1`] = ` " commit 9e424d329800e945e3003c9e275f80bdef69e591 @@ -1101,7 +1141,7 @@ Date: Sun Apr 18 03:17:05 2021 -0700 Update main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ■■ + ■■ .github/workflows/main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ■■ scripts/screenshotTheme @@ -1132,6 +1172,26 @@ Date: Sat Apr 10 22:26:15 2021 -0700 " `; +exports[`splitWithoutWrapping commit with files without hunks 1`] = ` +" +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ old empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ script.sh +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +" +`; + exports[`splitWithoutWrapping commit with inline line changes 1`] = ` " commit 9e424d329800e945e3003c9e275f80bdef69e591 @@ -1556,7 +1616,7 @@ Date: Sun Apr 18 03:17:05 2021 -0700 Update main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ■■ + ■■ .github/workflows/main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ■■ scripts/screenshotTheme @@ -1587,6 +1647,26 @@ Date: Sat Apr 10 22:26:15 2021 -0700 " `; +exports[`syntaxHighlighted commit with files without hunks 1`] = ` +" +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ old empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ script.sh +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +" +`; + exports[`syntaxHighlighted commit with inline line changes 1`] = ` " commit 9e424d329800e945e3003c9e275f80bdef69e591 @@ -2012,7 +2092,7 @@ Date: Sun Apr 18 03:17:05 2021 -0700 Update main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ■■ + ■■ .github/workflows/main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ■■ scripts/screenshotTheme @@ -2043,6 +2123,26 @@ Date: Sat Apr 10 22:26:15 2021 -0700 " `; +exports[`unifiedWithInlineChangesHighlighted commit with files without hunks 1`] = ` +" +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ old empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ script.sh +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +" +`; + exports[`unifiedWithInlineChangesHighlighted commit with inline line changes 1`] = ` " commit 9e424d329800e945e3003c9e275f80bdef69e591 @@ -2482,7 +2582,7 @@ Date: Sun Apr 18 03:17:05 2021 -0700 Update main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── - ■■ + ■■ .github/workflows/main.yml ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ■■ scripts/screenshotTheme @@ -2513,6 +2613,26 @@ Date: Sat Apr 10 22:26:15 2021 -0700 " `; +exports[`unifiedWithWrapping commit with files without hunks 1`] = ` +" +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ old empty.txt +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── + ■■ script.sh +──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── +" +`; + exports[`unifiedWithWrapping commit with inline line changes 1`] = ` " commit 9e424d329800e945e3003c9e275f80bdef69e591 diff --git a/src/index.test.ts b/src/index.test.ts index 31025bf..dc33430 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -522,6 +522,31 @@ index 095ee29..439621e 100644 ).toMatchSnapshot(); }); + test('commit with files without hunks', async function () { + expect( + await transform( + config, + ` +commit 1111111111111111111111111111111111111111 +Author: Shrey Banga +Date: Sun Apr 11 15:25:34 2021 -0700 + + Add, delete and chmod files without content changes + +diff --git a/empty.txt b/empty.txt +new file mode 100644 +index 0000000..e69de29 +diff --git a/old empty.txt b/old empty.txt +deleted file mode 100644 +index e69de29..0000000 +diff --git a/script.sh b/script.sh +old mode 100644 +new mode 100755 +` + ) + ).toMatchSnapshot(); + }); + test('merge commit with 2 parents', async function () { // Source: the TypeScript repo expect( diff --git a/src/iterSideBySideDiffs.ts b/src/iterSideBySideDiffs.ts index ae38fed..d668b73 100644 --- a/src/iterSideBySideDiffs.ts +++ b/src/iterSideBySideDiffs.ts @@ -18,6 +18,33 @@ const ANSI_COLOR_CODE_REGEX = ansiRegex(); const BINARY_FILES_DIFF_REGEX = /^Binary files (?:a\/(.*)|\/dev\/null) and (?:b\/(.*)|\/dev\/null) differ$/; +/** + * Diffs without hunks (empty files, mode changes) have no "---" and "+++" + * lines, so the file name has to come from the "diff --git a/ b/" + * line. Spaces in names are not escaped there either, so this only trusts it + * when both halves name the same file. + */ +function getFileNameFromDiffGitLine(line: string): string { + const paths = line.slice('diff --git '.length); + const middle = (paths.length - 1) / 2; + if (!Number.isInteger(middle) || paths[middle] !== ' ') { + return ''; + } + const pathA = paths.slice(0, middle); + const pathB = paths.slice(middle + 1); + if (pathA === pathB) { + return pathA; + } + if ( + pathA.startsWith('a/') && + pathB.startsWith('b/') && + pathA.slice(2) === pathB.slice(2) + ) { + return pathA.slice(2); + } + return ''; +} + // Combined hunk headers begin with N+1 @ characters for N parents const COMBINED_HUNK_HEADER_START_REGEX = /^(@{2,}) /; @@ -157,7 +184,13 @@ async function* iterSideBySideDiffsFormatted( } case 'unified-diff': case 'combined-diff': { - if (line.startsWith('--- a/')) { + if (line.startsWith('diff --git ')) { + fileNameA = fileNameB = getFileNameFromDiffGitLine(line); + } else if (line.startsWith('new file mode ')) { + fileNameA = ''; + } else if (line.startsWith('deleted file mode ')) { + fileNameB = ''; + } else if (line.startsWith('--- a/')) { fileNameA = line.slice('--- a/'.length); } else if (line.startsWith('+++ b/')) { fileNameB = line.slice('+++ b/'.length);