Skip to content

Commit 58b1d26

Browse files
committed
support old-style patches
1 parent c901f8c commit 58b1d26

File tree

3 files changed

+201
-4
lines changed

3 files changed

+201
-4
lines changed

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,141 @@ Array [
3333
]
3434
`;
3535

36+
exports[`the patch parser parses old-style patches 1`] = `
37+
Array [
38+
Object {
39+
"afterHash": null,
40+
"beforeHash": null,
41+
"hunks": Array [
42+
Object {
43+
"header": Object {
44+
"original": Object {
45+
"length": 10,
46+
"start": 41,
47+
},
48+
"patched": Object {
49+
"length": 11,
50+
"start": 41,
51+
},
52+
},
53+
"parts": Array [
54+
Object {
55+
"lines": Array [
56+
" */",
57+
"function isValidNameError(name, node) {",
58+
" !(typeof name === 'string') ? (0, _invariant2.default)(0, 'Expected string') : void 0;",
59+
],
60+
"noNewlineAtEndOfFile": false,
61+
"type": "context",
62+
},
63+
Object {
64+
"lines": Array [
65+
" if (name.length > 1 && name[0] === '_' && name[1] === '_') {",
66+
" return new _GraphQLError.GraphQLError('Name \\"' + name + '\\" must not begin with \\"__\\", which is reserved by ' + 'GraphQL introspection.', node);",
67+
" }",
68+
],
69+
"noNewlineAtEndOfFile": false,
70+
"type": "deletion",
71+
},
72+
Object {
73+
"lines": Array [
74+
" // if (name.length > 1 && name[0] === '_' && name[1] === '_') {",
75+
" // return new _GraphQLError.GraphQLError('Name \\"' + name + '\\" must not begin with \\"__\\", which is reserved by ' + 'GraphQL introspection.', node);",
76+
" // }",
77+
],
78+
"noNewlineAtEndOfFile": false,
79+
"type": "insertion",
80+
},
81+
Object {
82+
"lines": Array [
83+
" if (!NAME_RX.test(name)) {",
84+
" return new _GraphQLError.GraphQLError('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \\"' + name + '\\" does not.', node);",
85+
" }",
86+
],
87+
"noNewlineAtEndOfFile": false,
88+
"type": "context",
89+
},
90+
Object {
91+
"lines": Array [
92+
"",
93+
],
94+
"noNewlineAtEndOfFile": false,
95+
"type": "insertion",
96+
},
97+
Object {
98+
"lines": Array [
99+
"}",
100+
],
101+
"noNewlineAtEndOfFile": true,
102+
"type": "context",
103+
},
104+
],
105+
},
106+
],
107+
"path": "node_modules/graphql/utilities/assertValidName.js",
108+
"type": "patch",
109+
},
110+
Object {
111+
"afterHash": null,
112+
"beforeHash": null,
113+
"hunks": Array [
114+
Object {
115+
"header": Object {
116+
"original": Object {
117+
"length": 9,
118+
"start": 29,
119+
},
120+
"patched": Object {
121+
"length": 9,
122+
"start": 29,
123+
},
124+
},
125+
"parts": Array [
126+
Object {
127+
"lines": Array [
128+
" */",
129+
"export function isValidNameError(name, node) {",
130+
" !(typeof name === 'string') ? invariant(0, 'Expected string') : void 0;",
131+
],
132+
"noNewlineAtEndOfFile": false,
133+
"type": "context",
134+
},
135+
Object {
136+
"lines": Array [
137+
" if (name.length > 1 && name[0] === '_' && name[1] === '_') {",
138+
" return new GraphQLError('Name \\"' + name + '\\" must not begin with \\"__\\", which is reserved by ' + 'GraphQL introspection.', node);",
139+
" }",
140+
],
141+
"noNewlineAtEndOfFile": false,
142+
"type": "deletion",
143+
},
144+
Object {
145+
"lines": Array [
146+
" // if (name.length > 1 && name[0] === '_' && name[1] === '_') {",
147+
" // return new GraphQLError('Name \\"' + name + '\\" must not begin with \\"__\\", which is reserved by ' + 'GraphQL introspection.', node);",
148+
" // }",
149+
],
150+
"noNewlineAtEndOfFile": false,
151+
"type": "insertion",
152+
},
153+
Object {
154+
"lines": Array [
155+
" if (!NAME_RX.test(name)) {",
156+
" return new GraphQLError('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \\"' + name + '\\" does not.', node);",
157+
" }",
158+
],
159+
"noNewlineAtEndOfFile": false,
160+
"type": "context",
161+
},
162+
],
163+
},
164+
],
165+
"path": "node_modules/graphql/utilities/assertValidName.mjs",
166+
"type": "patch",
167+
},
168+
]
169+
`;
170+
36171
exports[`the patch parser works 1`] = `
37172
Array [
38173
Object {

src/patch/parse.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ index 0000000..3e1267f
100100
+this is a new file
101101
`.replace(/\n/g, "\r\n")
102102

103-
const modeChangeAndModifyAndRename = `
104-
diff --git a/numbers.txt b/banana.txt
103+
const modeChangeAndModifyAndRename = `diff --git a/numbers.txt b/banana.txt
105104
old mode 100644
106105
new mode 100755
107106
similarity index 96%
@@ -118,6 +117,42 @@ index fbf1785..92d2c5f
118117
119118
`
120119

120+
const oldStylePatch = `patch-package
121+
--- a/node_modules/graphql/utilities/assertValidName.js
122+
+++ b/node_modules/graphql/utilities/assertValidName.js
123+
@@ -41,10 +41,11 @@ function assertValidName(name) {
124+
*/
125+
function isValidNameError(name, node) {
126+
!(typeof name === 'string') ? (0, _invariant2.default)(0, 'Expected string') : void 0;
127+
- if (name.length > 1 && name[0] === '_' && name[1] === '_') {
128+
- return new _GraphQLError.GraphQLError('Name "' + name + '" must not begin with "__", which is reserved by ' + 'GraphQL introspection.', node);
129+
- }
130+
+ // if (name.length > 1 && name[0] === '_' && name[1] === '_') {
131+
+ // return new _GraphQLError.GraphQLError('Name "' + name + '" must not begin with "__", which is reserved by ' + 'GraphQL introspection.', node);
132+
+ // }
133+
if (!NAME_RX.test(name)) {
134+
return new _GraphQLError.GraphQLError('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "' + name + '" does not.', node);
135+
}
136+
+
137+
}
138+
\\ No newline at end of file
139+
--- a/node_modules/graphql/utilities/assertValidName.mjs
140+
+++ b/node_modules/graphql/utilities/assertValidName.mjs
141+
@@ -29,9 +29,9 @@ export function assertValidName(name) {
142+
*/
143+
export function isValidNameError(name, node) {
144+
!(typeof name === 'string') ? invariant(0, 'Expected string') : void 0;
145+
- if (name.length > 1 && name[0] === '_' && name[1] === '_') {
146+
- return new GraphQLError('Name "' + name + '" must not begin with "__", which is reserved by ' + 'GraphQL introspection.', node);
147+
- }
148+
+ // if (name.length > 1 && name[0] === '_' && name[1] === '_') {
149+
+ // return new GraphQLError('Name "' + name + '" must not begin with "__", which is reserved by ' + 'GraphQL introspection.', node);
150+
+ // }
151+
if (!NAME_RX.test(name)) {
152+
return new GraphQLError('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "' + name + '" does not.', node);
153+
}
154+
`
155+
121156
describe("the patch parser", () => {
122157
it("works for a simple case", () => {
123158
expect(parsePatchFile(patch)).toMatchSnapshot()
@@ -142,4 +177,8 @@ describe("the patch parser", () => {
142177
expect(parsePatchFile(accidentalBlankLine)).toMatchSnapshot()
143178
expect(parsePatchFile(modeChangeAndModifyAndRename)).toMatchSnapshot()
144179
})
180+
181+
it.only("parses old-style patches", () => {
182+
expect(parsePatchFile(oldStylePatch)).toMatchSnapshot()
183+
})
145184
})

src/patch/parse.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ const hunkLinetypes: {
144144
undefined: "context",
145145
}
146146

147-
function parsePatchLines(lines: string[]): FileDeets[] {
147+
function parsePatchLines(
148+
lines: string[],
149+
{ supportLegacyDiffs }: { supportLegacyDiffs: boolean },
150+
): FileDeets[] {
148151
const result: FileDeets[] = []
149152
let currentFilePatch: FileDeets = emptyFilePatch()
150153
let state: State = "parsing header"
@@ -213,6 +216,12 @@ function parsePatchLines(lines: string[]): FileDeets[] {
213216
currentFilePatch.toPath = line.slice("+++ b/".length)
214217
}
215218
} else {
219+
if (supportLegacyDiffs && line.startsWith("--- a/")) {
220+
state = "parsing header"
221+
commitFilePatch()
222+
i--
223+
continue
224+
}
216225
// parsing hunks
217226
const lineType = hunkLinetypes[line[0]] || null
218227
switch (lineType) {
@@ -399,7 +408,21 @@ export function parsePatchFile(file: string): ParsedPatchFile {
399408
if (lines[lines.length - 1] === "") {
400409
lines.pop()
401410
}
402-
return interpretParsedPatchFile(parsePatchLines(lines))
411+
try {
412+
return interpretParsedPatchFile(
413+
parsePatchLines(lines, { supportLegacyDiffs: false }),
414+
)
415+
} catch (e) {
416+
if (
417+
e instanceof Error &&
418+
e.message === "hunk header integrity check failed"
419+
) {
420+
return interpretParsedPatchFile(
421+
parsePatchLines(lines, { supportLegacyDiffs: true }),
422+
)
423+
}
424+
throw e
425+
}
403426
}
404427

405428
export function verifyHunkIntegrity(hunk: Hunk) {

0 commit comments

Comments
 (0)