1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
import { property, isEqual } from 'lodash';
import { diffModes, diffViewerModes } from '~/ide/constants';
import {
LINE_POSITION_LEFT,
LINE_POSITION_RIGHT,
TEXT_DIFF_POSITION_TYPE,
LEGACY_DIFF_NOTE_TYPE,
DIFF_NOTE_TYPE,
NEW_LINE_TYPE,
OLD_LINE_TYPE,
MATCH_LINE_TYPE,
INLINE_DIFF_LINES_KEY,
CONFLICT_OUR,
CONFLICT_THEIR,
CONFLICT_MARKER,
CONFLICT_MARKER_OUR,
CONFLICT_MARKER_THEIR,
EXPANDED_LINE_TYPE,
} from '../constants';
import { prepareRawDiffFile } from '../utils/diff_file';
import { extractFileHash } from '../utils/merge_request';
export const isAdded = (line) => ['new', 'new-nonewline'].includes(line.type);
export const isRemoved = (line) => ['old', 'old-nonewline'].includes(line.type);
export const isUnchanged = (line) => !line.type;
export const isMeta = (line) =>
['match', EXPANDED_LINE_TYPE, 'new-nonewline', 'old-nonewline'].includes(line.type);
export const isConflictMarker = (line) =>
[CONFLICT_MARKER_OUR, CONFLICT_MARKER_THEIR].includes(line.type);
export const isConflictSeperator = (line) => line.type === CONFLICT_MARKER;
export const isConflictOur = (line) => line.type === CONFLICT_OUR;
export const isConflictTheir = (line) => line.type === CONFLICT_THEIR;
/**
* Pass in the inline diff lines array which gets converted
* to the parallel diff lines.
* This allows for us to convert inline diff lines to parallel
* on the frontend without needing to send any requests
* to the API.
*
* This method has been taken from the already existing backend
* implementation at lib/gitlab/diff/parallel_diff.rb
*
* @param {Object[]} diffLines - inline diff lines
*
* @param {Boolean} inline - is inline context or not
*
* @returns {Object[]} parallel lines
*/
export const parallelizeDiffLines = (diffLines, inline) => {
let freeRightIndex = null;
let conflictStartIndex = -1;
const lines = [];
// `chunk` is used for dragging to select diff lines
// we are restricting commenting to only lines that appear between
// "expansion rows". Here equal chunks are lines grouped together
// inbetween expansion rows.
let chunk = 0;
for (let i = 0, diffLinesLength = diffLines.length, index = 0; i < diffLinesLength; i += 1) {
const line = diffLines[i];
line.chunk = chunk;
if (isMeta(line) && line.type !== EXPANDED_LINE_TYPE) chunk += 1;
if (isRemoved(line) || isConflictOur(line) || inline) {
lines.push({
[LINE_POSITION_LEFT]: line,
[LINE_POSITION_RIGHT]: null,
});
if (freeRightIndex === null) {
// Once we come upon a new line it can be put on the right of this old line
freeRightIndex = index;
}
index += 1;
} else if (isAdded(line) || isConflictTheir(line)) {
if (freeRightIndex !== null) {
// If an old line came before this without a line on the right, this
// line can be put to the right of it.
lines[freeRightIndex].right = line;
// If there are any other old lines on the left that don't yet have
// a new counterpart on the right, update the free_right_index
const nextFreeRightIndex = freeRightIndex + 1;
freeRightIndex = nextFreeRightIndex < index ? nextFreeRightIndex : null;
} else {
lines.push({
[LINE_POSITION_LEFT]: null,
[LINE_POSITION_RIGHT]: line,
});
freeRightIndex = null;
index += 1;
}
} else if (
isMeta(line) ||
isUnchanged(line) ||
isConflictMarker(line) ||
(isConflictSeperator(line) && inline)
) {
if (conflictStartIndex <= 0) {
// line in the right panel is the same as in the left one
lines.push({
[LINE_POSITION_LEFT]: line,
[LINE_POSITION_RIGHT]: !inline && line,
});
if (!inline && isConflictMarker(line)) {
conflictStartIndex = index;
}
freeRightIndex = null;
index += 1;
} else {
lines[conflictStartIndex][LINE_POSITION_RIGHT] = line;
conflictStartIndex = -1;
}
}
}
return lines;
};
export function findDiffFile(files, match, matchKey = 'file_hash') {
return files.find((file) => file[matchKey] === match);
}
export function getFormData(params) {
const {
commit,
note,
noteableType,
noteableData,
diffFile,
noteTargetLine,
diffViewType,
linePosition,
positionType,
lineRange,
showWhitespace,
} = params;
const position = JSON.stringify({
base_sha: diffFile.diff_refs.base_sha,
start_sha: diffFile.diff_refs.start_sha,
head_sha: diffFile.diff_refs.head_sha,
old_path: diffFile.old_path,
new_path: diffFile.new_path,
position_type: positionType || TEXT_DIFF_POSITION_TYPE,
old_line: noteTargetLine ? noteTargetLine.old_line : null,
new_line: noteTargetLine ? noteTargetLine.new_line : null,
x: params.x,
y: params.y,
width: params.width,
height: params.height,
line_range: lineRange,
ignore_whitespace_change: !showWhitespace,
});
const postData = {
view: diffViewType,
line_type: linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
merge_request_diff_head_sha: diffFile.diff_refs.head_sha,
in_reply_to_discussion_id: '',
note_project_id: '',
target_type: noteableData.targetType,
target_id: noteableData.id,
return_discussion: true,
note: {
note,
position,
noteable_type: noteableType,
noteable_id: noteableData.id,
commit_id: commit && commit.id,
type:
diffFile.diff_refs.start_sha && diffFile.diff_refs.head_sha
? DIFF_NOTE_TYPE
: LEGACY_DIFF_NOTE_TYPE,
line_code: noteTargetLine ? noteTargetLine.line_code : null,
},
};
return postData;
}
export function getNoteFormData(params) {
const data = getFormData(params);
return {
endpoint: params.noteableData.create_note_path,
data,
};
}
export const findIndexInInlineLines = (lines, lineNumbers) => {
const { oldLineNumber, newLineNumber } = lineNumbers;
return lines.findIndex(
(line) => line.old_line === oldLineNumber && line.new_line === newLineNumber,
);
};
export const getPreviousLineIndex = (file, lineNumbers) => {
return findIndexInInlineLines(file[INLINE_DIFF_LINES_KEY], lineNumbers);
};
export function removeMatchLine(diffFile, lineNumbers, bottom) {
const indexForInline = findIndexInInlineLines(diffFile[INLINE_DIFF_LINES_KEY], lineNumbers);
const factor = bottom ? 1 : -1;
if (indexForInline > -1) {
diffFile[INLINE_DIFF_LINES_KEY].splice(indexForInline + factor, 1);
}
}
export function addLineReferences(lines, lineNumbers, bottom, isExpandDown, nextLineNumbers) {
const { oldLineNumber, newLineNumber } = lineNumbers;
const lineCount = lines.length;
let matchLineIndex = -1;
const linesWithNumbers = lines.map((l, index) => {
if (l.type === MATCH_LINE_TYPE) {
matchLineIndex = index;
} else {
Object.assign(l, {
old_line: bottom ? oldLineNumber + index + 1 : oldLineNumber + index - lineCount,
new_line: bottom ? newLineNumber + index + 1 : newLineNumber + index - lineCount,
});
}
return l;
});
if (matchLineIndex > -1) {
const line = linesWithNumbers[matchLineIndex];
let targetLine;
if (isExpandDown) {
targetLine = nextLineNumbers;
} else if (bottom) {
targetLine = linesWithNumbers[matchLineIndex - 1];
} else {
targetLine = linesWithNumbers[matchLineIndex + 1];
}
Object.assign(line, {
meta_data: {
old_pos: targetLine.old_line,
new_pos: targetLine.new_line,
},
});
}
return linesWithNumbers;
}
function addInlineContextLines(options) {
const { inlineLines, contextLines, lineNumbers, isExpandDown } = options;
const factor = isExpandDown ? 1 : 0;
if (!isExpandDown && options.bottom) {
inlineLines.push(...contextLines);
} else {
const inlineIndex = findIndexInInlineLines(inlineLines, lineNumbers);
inlineLines.splice(inlineIndex + factor, 0, ...contextLines);
}
}
export function addContextLines(options) {
addInlineContextLines(options);
}
/**
* Trims the first char of the `richText` property when it's either a space or a diff symbol.
* @param {Object} line
* @returns {Object}
* @deprecated Use `line.rich_text = line.rich_text ? line.rich_text.replace(/^[+ -]/, '') : undefined;` instead!. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/299329
*/
export function trimFirstCharOfLineContent(line = {}) {
// eslint-disable-next-line no-param-reassign
delete line.text;
const parsedLine = { ...line };
if (line.rich_text) {
const firstChar = parsedLine.rich_text.charAt(0);
if (firstChar === ' ' || firstChar === '+' || firstChar === '-') {
parsedLine.rich_text = line.rich_text.substring(1);
}
}
return parsedLine;
}
function diffFileUniqueId(file) {
return `${file.content_sha}-${file.file_hash}`;
}
function mergeTwoFiles(target, source) {
const originalInline = target[INLINE_DIFF_LINES_KEY];
const missingInline = !originalInline.length;
return {
...target,
[INLINE_DIFF_LINES_KEY]: missingInline ? source[INLINE_DIFF_LINES_KEY] : originalInline,
parallel_diff_lines: null,
collapsed: source.collapsed,
};
}
function ensureBasicDiffFileLines(file) {
const missingInline = !file[INLINE_DIFF_LINES_KEY];
Object.assign(file, {
[INLINE_DIFF_LINES_KEY]: missingInline ? [] : file[INLINE_DIFF_LINES_KEY],
parallel_diff_lines: null,
});
return file;
}
function cleanRichText(text) {
return text ? text.replace(/^[+ -]/, '') : undefined;
}
function prepareLine(line, file) {
const problems = {
brokenSymlink: file.brokenSymlink,
brokenLineCode: !line.line_code,
fileOnlyMoved: file.renamed_file && file.added_lines === 0 && file.removed_lines === 0,
};
if (!line.alreadyPrepared) {
Object.assign(line, {
commentsDisabled: Boolean(
problems.brokenSymlink || problems.fileOnlyMoved || problems.brokenLineCode,
),
rich_text: cleanRichText(line.rich_text),
discussionsExpanded: false,
discussions: [],
hasForm: false,
text: undefined,
alreadyPrepared: true,
problems,
});
}
}
export function prepareLineForRenamedFile({ line, diffFile, index = 0 }) {
/*
Renamed files are a little different than other diffs, which
is why this is distinct from `prepareDiffFileLines` below.
We don't get any of the diff file context when we get the diff
(so no "inline" vs. "parallel", no "line_code", etc.).
We can also assume that both the left and the right of each line
(for parallel diff view type) are identical, because the file
is renamed, not modified.
This should be cleaned up as part of the effort around flattening our data
==> https://gitlab.com/groups/gitlab-org/-/epics/2852#note_304803402
*/
const lineNumber = index + 1;
const cleanLine = {
...line,
line_code: `${diffFile.file_hash}_${lineNumber}_${lineNumber}`,
new_line: lineNumber,
old_line: lineNumber,
};
prepareLine(cleanLine, diffFile); // WARNING: In-Place Mutations!
return cleanLine;
}
function prepareDiffFileLines(file) {
const inlineLines = file[INLINE_DIFF_LINES_KEY];
inlineLines.forEach((line) => prepareLine(line, file)); // WARNING: In-Place Mutations!
return file;
}
function finalizeDiffFile(file) {
Object.assign(file, {
isShowingFullFile: false,
isLoadingFullFile: false,
discussions: [],
renderingLines: false,
});
return file;
}
function deduplicateFilesList(files) {
const dedupedFiles = files.reduce((newList, file) => {
const id = diffFileUniqueId(file);
return {
...newList,
[id]: newList[id] ? mergeTwoFiles(newList[id], file) : file,
};
}, {});
return Object.values(dedupedFiles);
}
export function prepareDiffData({ diff, priorFiles = [], meta = false }) {
const cleanedFiles = (diff.diff_files || [])
.map((file, index, allFiles) => prepareRawDiffFile({ file, allFiles, meta, index }))
.map(ensureBasicDiffFileLines)
.map(prepareDiffFileLines)
.map((file) => finalizeDiffFile(file));
return deduplicateFilesList([...priorFiles, ...cleanedFiles]);
}
export function getDiffPositionByLineCode(diffFiles) {
const lines = diffFiles.reduce((acc, diffFile) => {
diffFile[INLINE_DIFF_LINES_KEY].forEach((line) => {
acc.push({ file: diffFile, line });
});
return acc;
}, []);
return lines.reduce((acc, { file, line }) => {
if (line.line_code) {
acc[line.line_code] = {
base_sha: file.diff_refs.base_sha,
head_sha: file.diff_refs.head_sha,
start_sha: file.diff_refs.start_sha,
new_path: file.new_path,
old_path: file.old_path,
old_line: line.old_line,
new_line: line.new_line,
line_range: null,
line_code: line.line_code,
position_type: 'text',
};
}
return acc;
}, {});
}
// This method will check whether the discussion is still applicable
// to the diff line in question regarding different versions of the MR
export function isDiscussionApplicableToLine({ discussion, diffPosition, latestDiff }) {
if (!diffPosition) {
return false;
}
const { line_code, ...dp } = diffPosition;
// Removing `line_range` from diffPosition because the backend does not
// yet consistently return this property. This check can be removed,
// once this is addressed. see https://gitlab.com/gitlab-org/gitlab/-/issues/213010
const { line_range: dpNotUsed, ...diffPositionCopy } = dp;
if (discussion.original_position && discussion.position) {
const discussionPositions = [
discussion.original_position,
discussion.position,
...(discussion.positions || []),
];
const removeLineRange = (position) => {
const { line_range: pNotUsed, ...positionNoLineRange } = position;
return positionNoLineRange;
};
return discussionPositions
.map(removeLineRange)
.some((position) => isEqual(position, diffPositionCopy));
}
// eslint-disable-next-line
return latestDiff && discussion.active && line_code === discussion.line_code;
}
export const getDiffMode = (diffFile) => {
const diffModeKey = Object.keys(diffModes).find((key) => diffFile[`${key}_file`]);
return (
diffModes[diffModeKey] ||
(diffFile.viewer?.name === diffViewerModes.mode_changed && diffViewerModes.mode_changed) ||
(diffFile.viewer?.name === diffViewerModes.no_preview && diffViewerModes.no_preview) ||
diffModes.replaced
);
};
export const convertExpandLines = ({
diffLines,
data,
typeKey,
oldLineKey,
newLineKey,
mapLine,
}) => {
const dataLength = data.length;
const lines = [];
for (let i = 0, diffLinesLength = diffLines.length; i < diffLinesLength; i += 1) {
const line = diffLines[i];
if (property(typeKey)(line) === 'match') {
const beforeLine = diffLines[i - 1];
const afterLine = diffLines[i + 1];
const newLineProperty = property(newLineKey);
const beforeLineIndex = newLineProperty(beforeLine) || 0;
const afterLineIndex = newLineProperty(afterLine) - 1 || dataLength;
lines.push(
...data.slice(beforeLineIndex, afterLineIndex).map((l, index) =>
mapLine({
line: Object.assign(l, { hasForm: false, discussions: [] }),
oldLine: (property(oldLineKey)(beforeLine) || 0) + index + 1,
newLine: (newLineProperty(beforeLine) || 0) + index + 1,
}),
),
);
} else {
lines.push(line);
}
}
return lines;
};
export const idleCallback = (cb) => requestIdleCallback(cb);
function getLinesFromFileByLineCode(file, lineCode) {
const inlineLines = file[INLINE_DIFF_LINES_KEY];
const matchesCode = (line) => line.line_code === lineCode;
return inlineLines.filter(matchesCode);
}
export const updateLineInFile = (selectedFile, lineCode, updateFn) => {
getLinesFromFileByLineCode(selectedFile, lineCode).forEach(updateFn);
};
export const allDiscussionWrappersExpanded = (diff) => {
let discussionsExpanded = true;
const changeExpandedResult = (line) => {
if (line && line.discussions.length) {
discussionsExpanded = discussionsExpanded && line.discussionsExpanded;
}
};
diff[INLINE_DIFF_LINES_KEY].forEach((line) => {
changeExpandedResult(line);
});
return discussionsExpanded;
};
export function isUrlHashNoteLink(urlHash = '') {
const id = urlHash.replace(/^#/, '');
return id.startsWith('note');
}
export function isUrlHashFileHeader(urlHash = '') {
const id = urlHash.replace(/^#/, '');
return id.startsWith('diff-content');
}
export function parseUrlHashAsFileHash(urlHash = '', currentDiffFileId = '') {
const hashless = urlHash.replace(/^#/, '');
const isNoteLink = isUrlHashNoteLink(hashless);
const extractedSha1 = extractFileHash({ input: hashless });
let id = extractedSha1;
if (isNoteLink && currentDiffFileId) {
id = currentDiffFileId;
} else if (isUrlHashFileHeader(hashless)) {
id = hashless.replace('diff-content-', '');
} else if (!extractedSha1 || isNoteLink) {
id = null;
}
return id;
}
export function markTreeEntriesLoaded({ priorEntries, loadedFiles }) {
const newEntries = { ...priorEntries };
loadedFiles.forEach((newFile) => {
const entry = newEntries[newFile.new_path];
if (entry) {
entry.diffLoaded = true;
entry.diffLoading = false;
}
});
return newEntries;
}
|