Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/local-vcs/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion packages/local-vcs/src/vcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down