diff --git a/packages/local-vcs/src/index.test.ts b/packages/local-vcs/src/index.test.ts index 716dcfb020..80a2c764b1 100644 --- a/packages/local-vcs/src/index.test.ts +++ b/packages/local-vcs/src/index.test.ts @@ -584,6 +584,47 @@ describe("local vcs", () => { ]); }); + it("reads a path with spaces from a file section that has only a header", () => { + const patch = [ + "diff --git a/docs/Screen Shot 1.png b/docs/Screen Shot 1.png", + "new file mode 100644", + "index 0000000..9c2e1f3", + "Binary files /dev/null and b/docs/Screen Shot 1.png differ", + "diff --git a/run me.sh b/run me.sh", + "old mode 100644", + "new mode 100755", + "diff --git a/old notes.md b/old notes.md", + "deleted file mode 100644", + "index e69de29..0000000", + "", + ].join("\n"); + + expect(splitGitPatchFiles(patch).map((entry) => entry.file)).toEqual([ + { + path: "docs/Screen Shot 1.png", + previousPath: undefined, + status: "added", + additions: 0, + deletions: 0, + binary: true, + }, + { + path: "run me.sh", + previousPath: undefined, + status: "modified", + additions: 0, + deletions: 0, + }, + { + path: "old notes.md", + previousPath: undefined, + status: "deleted", + additions: 0, + deletions: 0, + }, + ]); + }); + it("refuses a diff whose counts arrive before its records", () => { const records = [ "1\t0\tsrc/app.ts", diff --git a/packages/local-vcs/src/vcs.ts b/packages/local-vcs/src/vcs.ts index f7e29c543b..940a34b83a 100644 --- a/packages/local-vcs/src/vcs.ts +++ b/packages/local-vcs/src/vcs.ts @@ -2082,7 +2082,21 @@ function parseDiffGitHeaderPaths( line: string, ): { oldPath: string; newPath: string } | null { if (!line.startsWith("diff --git ")) return null; - const tokens = line.slice("diff --git ".length).match(/"([^"\\]|\\.)*"|\S+/g); + const names = line.slice("diff --git ".length); + + // Git does not quote a space, so one path named twice splits at its middle. + const middle = (names.length - 1) / 2; + + if (names[middle] === " ") { + const oldPath = stripDiffPathPrefix(unquoteGitPath(names.slice(0, middle))); + + if ( + oldPath === stripDiffPathPrefix(unquoteGitPath(names.slice(middle + 1))) + ) + return { oldPath, newPath: oldPath }; + } + + const tokens = names.match(/"([^"\\]|\\.)*"|\S+/g); if (!tokens || tokens.length < 2) return null;