Skip to content

Commit d7d55f7

Browse files
committed
fix: comptability between git diff and unidiff (otakustay#53)
1 parent 657f22c commit d7d55f7

3 files changed

Lines changed: 47 additions & 62 deletions

File tree

‎src/utils/__test__/__snapshots__/parse.test.js.snap‎

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ Array [
5656

5757
exports[`parseDiff ensure test case 2`] = `
5858
Array [
59-
Object {
60-
"hunks": Array [],
61-
"newEndingNewLine": true,
62-
"newMode": "100644",
63-
"newPath": "",
64-
"newRevision": "2222222",
65-
"oldEndingNewLine": true,
66-
"oldMode": "100644",
67-
"oldPath": "",
68-
"oldRevision": "1111111",
69-
"type": "modify",
70-
},
7159
Object {
7260
"hunks": Array [
7361
Object {
@@ -134,52 +122,45 @@ Array [
134122
"type": "normal",
135123
},
136124
Object {
137-
"content": "",
125+
"content": "export const detectLanguage = filename => {",
138126
"isNormal": true,
139127
"newLineNumber": 156,
140128
"oldLineNumber": 156,
141129
"type": "normal",
142130
},
143-
Object {
144-
"content": "export const detectLanguage = filename => {",
145-
"isNormal": true,
146-
"newLineNumber": 157,
147-
"oldLineNumber": 157,
148-
"type": "normal",
149-
},
150131
Object {
151132
"content": " // 仅仅是为了处理特殊情况,特殊情况应该已经处理完毕",
152133
"isInsert": true,
153-
"lineNumber": 158,
134+
"lineNumber": 157,
154135
"type": "insert",
155136
},
156137
Object {
157138
"content": " if (!filename) {",
158139
"isNormal": true,
159-
"newLineNumber": 159,
160-
"oldLineNumber": 158,
140+
"newLineNumber": 158,
141+
"oldLineNumber": 157,
161142
"type": "normal",
162143
},
163144
Object {
164145
"content": " return 'text';",
165146
"isNormal": true,
166-
"newLineNumber": 160,
167-
"oldLineNumber": 159,
147+
"newLineNumber": 159,
148+
"oldLineNumber": 158,
168149
"type": "normal",
169150
},
170151
Object {
171152
"content": " }",
172153
"isNormal": true,
173-
"newLineNumber": 161,
174-
"oldLineNumber": 160,
154+
"newLineNumber": 160,
155+
"oldLineNumber": 159,
175156
"type": "normal",
176157
},
177158
],
178-
"content": "@@ -155,6 +155,7 @@",
159+
"content": "@@ -155,5 +155,6 @@",
179160
"isPlain": false,
180-
"newLines": 7,
161+
"newLines": 6,
181162
"newStart": 155,
182-
"oldLines": 6,
163+
"oldLines": 5,
183164
"oldStart": 155,
184165
},
185166
],
@@ -257,7 +238,7 @@ Array [
257238
]
258239
`;
259240

260-
exports[`parseDiff undiff 1`] = `
241+
exports[`parseDiff unidiff 1`] = `
261242
Array [
262243
Object {
263244
"hunks": Array [
@@ -271,62 +252,55 @@ Array [
271252
"type": "normal",
272253
},
273254
Object {
274-
"content": "",
255+
"content": "export const detectLanguage = filename => {",
275256
"isNormal": true,
276257
"newLineNumber": 156,
277258
"oldLineNumber": 156,
278259
"type": "normal",
279260
},
280-
Object {
281-
"content": "export const detectLanguage = filename => {",
282-
"isNormal": true,
283-
"newLineNumber": 157,
284-
"oldLineNumber": 157,
285-
"type": "normal",
286-
},
287261
Object {
288262
"content": " // 仅仅是为了处理特殊情况,特殊情况应该已经处理完毕",
289263
"isInsert": true,
290-
"lineNumber": 158,
264+
"lineNumber": 157,
291265
"type": "insert",
292266
},
293267
Object {
294268
"content": " if (!filename) {",
295269
"isNormal": true,
296-
"newLineNumber": 159,
297-
"oldLineNumber": 158,
270+
"newLineNumber": 158,
271+
"oldLineNumber": 157,
298272
"type": "normal",
299273
},
300274
Object {
301275
"content": " return 'text';",
302276
"isNormal": true,
303-
"newLineNumber": 160,
304-
"oldLineNumber": 159,
277+
"newLineNumber": 159,
278+
"oldLineNumber": 158,
305279
"type": "normal",
306280
},
307281
Object {
308282
"content": " }",
309283
"isNormal": true,
310-
"newLineNumber": 161,
311-
"oldLineNumber": 160,
284+
"newLineNumber": 160,
285+
"oldLineNumber": 159,
312286
"type": "normal",
313287
},
314288
],
315-
"content": "@@ -155,6 +155,7 @@",
289+
"content": "@@ -155,5 +155,6 @@",
316290
"isPlain": false,
317-
"newLines": 7,
291+
"newLines": 6,
318292
"newStart": 155,
319-
"oldLines": 6,
293+
"oldLines": 5,
320294
"oldStart": 155,
321295
},
322296
],
323297
"newEndingNewLine": true,
324298
"newMode": "100644",
325-
"newPath": "55,7",
299+
"newPath": "x.js",
326300
"newRevision": "2222222",
327301
"oldEndingNewLine": true,
328302
"oldMode": "100644",
329-
"oldPath": "5,6",
303+
"oldPath": "x.js",
330304
"oldRevision": "1111111",
331305
"type": "modify",
332306
},

‎src/utils/__test__/parse.test.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ describe('parseDiff', () => {
1212
index 1eadcc9..022bfd4 100644
1313
--- a/src/common/utils/languages.js
1414
+++ b/src/common/utils/languages.js
15-
@@ -155,6 +155,7 @@
15+
@@ -155,5 +155,6 @@
1616
const genericExtension = new Set(['.tpl', '.tmp']);
17-
1817
export const detectLanguage = filename => {
1918
+ // 仅仅是为了处理特殊情况,特殊情况应该已经处理完毕
2019
if (!filename) {
@@ -23,10 +22,11 @@ index 1eadcc9..022bfd4 100644
2322
expect(parseDiff(diff, {nearbySequences: 'zip'})).toMatchSnapshot();
2423
});
2524

26-
test('undiff', () => {
27-
const diff = `@@ -155,6 +155,7 @@
25+
test('unidiff', () => {
26+
const diff = `--- x.js 2002-02-21 23:30:39.942229878 -0800
27+
+++ x.js 2002-02-21 23:30:50.442260588 -0800
28+
@@ -155,5 +155,6 @@
2829
const genericExtension = new Set(['.tpl', '.tmp']);
29-
3030
export const detectLanguage = filename => {
3131
+ // 仅仅是为了处理特殊情况,特殊情况应该已经处理完毕
3232
if (!filename) {
@@ -40,7 +40,6 @@ index 1eadcc9..022bfd4 100644
4040
similarity index 100%
4141
rename from src/error/components/ErrorBase.jsx
4242
rename to src/components/ErrorPages/ErrorBase.jsx`;
43-
4443
expect(parseDiff(diff, {nearbySequences: 'zip'})).toMatchSnapshot();
4544
});
4645

‎src/utils/parse.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,39 @@ const mapFile = (file, options) => {
4444
};
4545

4646
const normalizeDiffText = text => {
47+
// Git diff header:
48+
//
49+
// diff --git a/test/fixture/test/ci.go b/test/fixture/test/ci.go
50+
// index 6829b8a2..4c565f1b 100644
51+
// --- a/test/fixture/test/ci.go
52+
// +++ b/test/fixture/test/ci.go
4753
if (text.indexOf('diff --git') === 0) {
4854
return text;
4955
}
5056

57+
// Unidiff header:
58+
//
59+
// --- /test/fixture/test/ci.go 2002-02-21 23:30:39.942229878 -0800
60+
// +++ /test/fixture/test/ci.go 2002-02-21 23:30:50.442260588 -0800
5161
const indexOfFirstLineBreak = text.indexOf('\n');
5262
const indexOfSecondLineBreak = text.indexOf('\n', indexOfFirstLineBreak + 1);
5363
const firstLine = text.slice(0, indexOfFirstLineBreak);
5464
const secondLine = text.slice(indexOfFirstLineBreak + 1, indexOfSecondLineBreak);
55-
const oldPath = firstLine.slice(4);
56-
const newPath = secondLine.slice(4);
65+
const oldPath = firstLine.split(' ').slice(1, -3).join(' ');
66+
const newPath = secondLine.split(' ').slice(1, -3).join(' ');
5767
const segments = [
58-
`diff --git ${oldPath} ${newPath}`,
68+
`diff --git a/${oldPath} b/${newPath}`,
5969
'index 1111111..2222222 100644',
60-
text,
70+
`--- a/${oldPath}`,
71+
`+++ b/${newPath}`,
72+
text.slice(indexOfSecondLineBreak + 1),
6173
];
6274

6375
return segments.join('\n');
6476
};
6577

6678
export const parseDiff = (text, options = {}) => {
67-
const diffText = normalizeDiffText(text);
79+
const diffText = normalizeDiffText(text.trim());
6880
const files = parser.parse(diffText);
6981

7082
return files.map(file => mapFile(file, options));

0 commit comments

Comments
 (0)