Skip to content

Commit 027fe8e

Browse files
committed
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`.
1 parent 595e402 commit 027fe8e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/patch/__snapshots__/parse.test.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`the patch parser can handle files with CRLF line breaks 1`] = `
4+
Array [
5+
Object {
6+
"lines": Array [
7+
"this is a new file
8+
",
9+
],
10+
"mode": 420,
11+
"noNewlineAtEndOfFile": false,
12+
"path": "banana.ts",
13+
"type": "file creation",
14+
},
15+
]
16+
`;
17+
318
exports[`the patch parser works for a simple case 1`] = `
419
Array [
520
Object {

src/patch/parse.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ index 2de83dd..842652c 100644
9191
file
9292
`
9393

94+
const crlfLineBreaks = `diff --git a/banana.ts b/banana.ts
95+
new file mode 100644
96+
index 0000000..3e1267f
97+
--- /dev/null
98+
+++ b/banana.ts
99+
@@ -0,0 +1 @@
100+
+this is a new file
101+
`.replace(/\n/g, "\r\n")
102+
94103
describe("the patch parser", () => {
95104
it("works for a simple case", () => {
96105
expect(parsePatch(patch)).toMatchSnapshot()
@@ -105,4 +114,7 @@ describe("the patch parser", () => {
105114
it("is OK when blank lines are accidentally created", () => {
106115
expect(parsePatch(accidentalBlankLine)).toEqual(parsePatch(patch))
107116
})
117+
it(`can handle files with CRLF line breaks`, () => {
118+
expect(parsePatch(crlfLineBreaks)).toMatchSnapshot()
119+
})
108120
})

src/patch/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class PatchParser {
266266
}
267267

268268
private parseFileModification() {
269-
const startPath = this.currentLine.slice("--- ".length)
269+
const startPath = this.currentLine.trim().slice("--- ".length)
270270
this.nextLine()
271271
const endPath = this.currentLine.trim().slice("--- ".length)
272272
this.nextLine()

0 commit comments

Comments
 (0)