From 027fe8e5697c568e33e2fe244fae05eff7c0fc66 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Sat, 20 Oct 2018 18:18:45 +0100 Subject: [PATCH] Handle CRLF line breaks in the patch parser This fixes an issue where file creation wasn't occurring on Windows due to `startPath !== "/dev/null"` because `startPath` had a trailing `\r`. --- src/patch/__snapshots__/parse.test.ts.snap | 15 +++++++++++++++ src/patch/parse.test.ts | 12 ++++++++++++ src/patch/parse.ts | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/patch/__snapshots__/parse.test.ts.snap b/src/patch/__snapshots__/parse.test.ts.snap index 34b381cf..f783672c 100644 --- a/src/patch/__snapshots__/parse.test.ts.snap +++ b/src/patch/__snapshots__/parse.test.ts.snap @@ -1,5 +1,20 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`the patch parser can handle files with CRLF line breaks 1`] = ` +Array [ + Object { + "lines": Array [ + "this is a new file +", + ], + "mode": 420, + "noNewlineAtEndOfFile": false, + "path": "banana.ts", + "type": "file creation", + }, +] +`; + exports[`the patch parser works for a simple case 1`] = ` Array [ Object { diff --git a/src/patch/parse.test.ts b/src/patch/parse.test.ts index 5152b71c..8871cee9 100644 --- a/src/patch/parse.test.ts +++ b/src/patch/parse.test.ts @@ -91,6 +91,15 @@ index 2de83dd..842652c 100644 file ` +const crlfLineBreaks = `diff --git a/banana.ts b/banana.ts +new file mode 100644 +index 0000000..3e1267f +--- /dev/null ++++ b/banana.ts +@@ -0,0 +1 @@ ++this is a new file +`.replace(/\n/g, "\r\n") + describe("the patch parser", () => { it("works for a simple case", () => { expect(parsePatch(patch)).toMatchSnapshot() @@ -105,4 +114,7 @@ describe("the patch parser", () => { it("is OK when blank lines are accidentally created", () => { expect(parsePatch(accidentalBlankLine)).toEqual(parsePatch(patch)) }) + it(`can handle files with CRLF line breaks`, () => { + expect(parsePatch(crlfLineBreaks)).toMatchSnapshot() + }) }) diff --git a/src/patch/parse.ts b/src/patch/parse.ts index cad265a8..a9ff4a10 100644 --- a/src/patch/parse.ts +++ b/src/patch/parse.ts @@ -266,7 +266,7 @@ class PatchParser { } private parseFileModification() { - const startPath = this.currentLine.slice("--- ".length) + const startPath = this.currentLine.trim().slice("--- ".length) this.nextLine() const endPath = this.currentLine.trim().slice("--- ".length) this.nextLine()