2020-02-24 09:08:51 +00:00
|
|
|
import { property, isEqual } from 'lodash';
|
2019-02-15 17:56:50 +00:00
|
|
|
import { diffModes, diffViewerModes } from '~/ide/constants';
|
2018-06-21 12:22:40 +00:00
|
|
|
import {
|
|
|
|
LINE_POSITION_LEFT,
|
|
|
|
LINE_POSITION_RIGHT,
|
|
|
|
TEXT_DIFF_POSITION_TYPE,
|
2018-09-10 18:41:57 +00:00
|
|
|
LEGACY_DIFF_NOTE_TYPE,
|
2018-06-21 12:22:40 +00:00
|
|
|
DIFF_NOTE_TYPE,
|
|
|
|
NEW_LINE_TYPE,
|
|
|
|
OLD_LINE_TYPE,
|
|
|
|
MATCH_LINE_TYPE,
|
2020-11-19 18:09:13 +00:00
|
|
|
INLINE_DIFF_LINES_KEY,
|
2020-12-17 09:10:19 +00:00
|
|
|
CONFLICT_OUR,
|
|
|
|
CONFLICT_THEIR,
|
|
|
|
CONFLICT_MARKER,
|
|
|
|
CONFLICT_MARKER_OUR,
|
|
|
|
CONFLICT_MARKER_THEIR,
|
2022-04-20 21:08:12 +00:00
|
|
|
EXPANDED_LINE_TYPE,
|
2018-06-21 12:22:40 +00:00
|
|
|
} from '../constants';
|
2020-12-04 09:09:36 +00:00
|
|
|
import { prepareRawDiffFile } from '../utils/diff_file';
|
2018-06-21 12:22:40 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
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;
|
2022-04-20 21:08:12 +00:00
|
|
|
export const isMeta = (line) =>
|
|
|
|
['match', EXPANDED_LINE_TYPE, 'new-nonewline', 'old-nonewline'].includes(line.type);
|
2020-12-23 21:10:24 +00:00
|
|
|
export const isConflictMarker = (line) =>
|
2020-12-17 09:10:19 +00:00
|
|
|
[CONFLICT_MARKER_OUR, CONFLICT_MARKER_THEIR].includes(line.type);
|
2020-12-23 21:10:24 +00:00
|
|
|
export const isConflictSeperator = (line) => line.type === CONFLICT_MARKER;
|
|
|
|
export const isConflictOur = (line) => line.type === CONFLICT_OUR;
|
|
|
|
export const isConflictTheir = (line) => line.type === CONFLICT_THEIR;
|
2020-08-26 12:10:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2020-11-11 21:08:55 +00:00
|
|
|
* @param {Boolean} inline - is inline context or not
|
|
|
|
*
|
2020-08-26 12:10:53 +00:00
|
|
|
* @returns {Object[]} parallel lines
|
|
|
|
*/
|
2020-11-11 21:08:55 +00:00
|
|
|
|
|
|
|
export const parallelizeDiffLines = (diffLines, inline) => {
|
2020-08-26 12:10:53 +00:00
|
|
|
let freeRightIndex = null;
|
2020-12-17 09:10:19 +00:00
|
|
|
let conflictStartIndex = -1;
|
2020-08-26 12:10:53 +00:00
|
|
|
const lines = [];
|
|
|
|
|
2021-01-14 18:10:59 +00:00
|
|
|
// `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;
|
|
|
|
|
2020-08-26 12:10:53 +00:00
|
|
|
for (let i = 0, diffLinesLength = diffLines.length, index = 0; i < diffLinesLength; i += 1) {
|
|
|
|
const line = diffLines[i];
|
2021-01-14 18:10:59 +00:00
|
|
|
line.chunk = chunk;
|
|
|
|
|
2022-04-26 12:09:50 +00:00
|
|
|
if (isMeta(line) && line.type !== EXPANDED_LINE_TYPE) chunk += 1;
|
2020-08-26 12:10:53 +00:00
|
|
|
|
2020-12-17 09:10:19 +00:00
|
|
|
if (isRemoved(line) || isConflictOur(line) || inline) {
|
2020-08-26 12:10:53 +00:00
|
|
|
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;
|
2020-12-17 09:10:19 +00:00
|
|
|
} else if (isAdded(line) || isConflictTheir(line)) {
|
2020-11-26 15:09:30 +00:00
|
|
|
if (freeRightIndex !== null) {
|
2020-08-26 12:10:53 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2020-12-17 09:10:19 +00:00
|
|
|
} 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,
|
|
|
|
});
|
2020-08-26 12:10:53 +00:00
|
|
|
|
2020-12-17 09:10:19 +00:00
|
|
|
if (!inline && isConflictMarker(line)) {
|
|
|
|
conflictStartIndex = index;
|
|
|
|
}
|
|
|
|
freeRightIndex = null;
|
|
|
|
index += 1;
|
|
|
|
} else {
|
|
|
|
lines[conflictStartIndex][LINE_POSITION_RIGHT] = line;
|
|
|
|
conflictStartIndex = -1;
|
|
|
|
}
|
2020-08-26 12:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
};
|
|
|
|
|
2019-02-28 14:14:15 +00:00
|
|
|
export function findDiffFile(files, match, matchKey = 'file_hash') {
|
2020-12-23 21:10:24 +00:00
|
|
|
return files.find((file) => file[matchKey] === match);
|
2018-06-21 12:22:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 17:16:40 +00:00
|
|
|
export function getFormData(params) {
|
2018-06-21 12:22:40 +00:00
|
|
|
const {
|
2018-11-26 19:36:06 +00:00
|
|
|
commit,
|
2018-06-21 12:22:40 +00:00
|
|
|
note,
|
|
|
|
noteableType,
|
|
|
|
noteableData,
|
|
|
|
diffFile,
|
|
|
|
noteTargetLine,
|
|
|
|
diffViewType,
|
|
|
|
linePosition,
|
2018-10-18 07:50:38 +00:00
|
|
|
positionType,
|
2020-06-02 18:08:32 +00:00
|
|
|
lineRange,
|
2018-06-21 12:22:40 +00:00
|
|
|
} = params;
|
|
|
|
|
|
|
|
const position = JSON.stringify({
|
2018-11-09 19:48:41 +00:00
|
|
|
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,
|
2018-10-18 07:50:38 +00:00
|
|
|
position_type: positionType || TEXT_DIFF_POSITION_TYPE,
|
2018-11-09 19:48:41 +00:00
|
|
|
old_line: noteTargetLine ? noteTargetLine.old_line : null,
|
|
|
|
new_line: noteTargetLine ? noteTargetLine.new_line : null,
|
2018-10-18 07:50:38 +00:00
|
|
|
x: params.x,
|
|
|
|
y: params.y,
|
2018-11-06 09:40:03 +00:00
|
|
|
width: params.width,
|
|
|
|
height: params.height,
|
2020-06-02 18:08:32 +00:00
|
|
|
line_range: lineRange,
|
2018-06-21 12:22:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const postData = {
|
|
|
|
view: diffViewType,
|
|
|
|
line_type: linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
|
2018-11-09 19:48:41 +00:00
|
|
|
merge_request_diff_head_sha: diffFile.diff_refs.head_sha,
|
2018-06-21 12:22:40 +00:00
|
|
|
in_reply_to_discussion_id: '',
|
|
|
|
note_project_id: '',
|
|
|
|
target_type: noteableData.targetType,
|
|
|
|
target_id: noteableData.id,
|
2018-09-18 16:12:37 +00:00
|
|
|
return_discussion: true,
|
2018-06-21 12:22:40 +00:00
|
|
|
note: {
|
|
|
|
note,
|
|
|
|
position,
|
|
|
|
noteable_type: noteableType,
|
|
|
|
noteable_id: noteableData.id,
|
2018-11-26 19:36:06 +00:00
|
|
|
commit_id: commit && commit.id,
|
2018-09-14 09:17:31 +00:00
|
|
|
type:
|
2018-11-09 19:48:41 +00:00
|
|
|
diffFile.diff_refs.start_sha && diffFile.diff_refs.head_sha
|
2018-09-14 09:17:31 +00:00
|
|
|
? DIFF_NOTE_TYPE
|
|
|
|
: LEGACY_DIFF_NOTE_TYPE,
|
2018-11-09 19:48:41 +00:00
|
|
|
line_code: noteTargetLine ? noteTargetLine.line_code : null,
|
2018-06-21 12:22:40 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-06 17:16:40 +00:00
|
|
|
return postData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNoteFormData(params) {
|
|
|
|
const data = getFormData(params);
|
|
|
|
|
2018-06-21 12:22:40 +00:00
|
|
|
return {
|
2018-10-06 17:16:40 +00:00
|
|
|
endpoint: params.noteableData.create_note_path,
|
|
|
|
data,
|
2018-06-21 12:22:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const findIndexInInlineLines = (lines, lineNumbers) => {
|
|
|
|
const { oldLineNumber, newLineNumber } = lineNumbers;
|
|
|
|
|
2020-02-20 18:08:51 +00:00
|
|
|
return lines.findIndex(
|
2020-12-23 21:10:24 +00:00
|
|
|
(line) => line.old_line === oldLineNumber && line.new_line === newLineNumber,
|
2018-06-21 12:22:40 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-20 21:08:12 +00:00
|
|
|
export const getPreviousLineIndex = (file, lineNumbers) => {
|
2020-11-19 18:09:13 +00:00
|
|
|
return findIndexInInlineLines(file[INLINE_DIFF_LINES_KEY], lineNumbers);
|
2020-02-20 18:08:51 +00:00
|
|
|
};
|
|
|
|
|
2018-06-21 12:22:40 +00:00
|
|
|
export function removeMatchLine(diffFile, lineNumbers, bottom) {
|
2020-11-19 18:09:13 +00:00
|
|
|
const indexForInline = findIndexInInlineLines(diffFile[INLINE_DIFF_LINES_KEY], lineNumbers);
|
2018-06-21 12:22:40 +00:00
|
|
|
const factor = bottom ? 1 : -1;
|
|
|
|
|
2020-02-20 18:08:51 +00:00
|
|
|
if (indexForInline > -1) {
|
2020-11-19 18:09:13 +00:00
|
|
|
diffFile[INLINE_DIFF_LINES_KEY].splice(indexForInline + factor, 1);
|
2020-02-20 18:08:51 +00:00
|
|
|
}
|
2018-06-21 12:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 02:36:32 +00:00
|
|
|
export function addLineReferences(lines, lineNumbers, bottom, isExpandDown, nextLineNumbers) {
|
2018-06-21 12:22:40 +00:00
|
|
|
const { oldLineNumber, newLineNumber } = lineNumbers;
|
|
|
|
const lineCount = lines.length;
|
|
|
|
let matchLineIndex = -1;
|
|
|
|
|
|
|
|
const linesWithNumbers = lines.map((l, index) => {
|
2018-11-09 19:48:41 +00:00
|
|
|
if (l.type === MATCH_LINE_TYPE) {
|
2018-06-21 12:22:40 +00:00
|
|
|
matchLineIndex = index;
|
|
|
|
} else {
|
2018-11-09 19:48:41 +00:00
|
|
|
Object.assign(l, {
|
|
|
|
old_line: bottom ? oldLineNumber + index + 1 : oldLineNumber + index - lineCount,
|
|
|
|
new_line: bottom ? newLineNumber + index + 1 : newLineNumber + index - lineCount,
|
2018-06-21 12:22:40 +00:00
|
|
|
});
|
|
|
|
}
|
2018-11-09 19:48:41 +00:00
|
|
|
return l;
|
2018-06-21 12:22:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (matchLineIndex > -1) {
|
|
|
|
const line = linesWithNumbers[matchLineIndex];
|
2019-08-10 02:36:32 +00:00
|
|
|
let targetLine;
|
|
|
|
|
|
|
|
if (isExpandDown) {
|
|
|
|
targetLine = nextLineNumbers;
|
|
|
|
} else if (bottom) {
|
|
|
|
targetLine = linesWithNumbers[matchLineIndex - 1];
|
|
|
|
} else {
|
|
|
|
targetLine = linesWithNumbers[matchLineIndex + 1];
|
|
|
|
}
|
2018-06-21 12:22:40 +00:00
|
|
|
|
|
|
|
Object.assign(line, {
|
2018-11-09 19:48:41 +00:00
|
|
|
meta_data: {
|
|
|
|
old_pos: targetLine.old_line,
|
|
|
|
new_pos: targetLine.new_line,
|
2018-06-21 12:22:40 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return linesWithNumbers;
|
|
|
|
}
|
|
|
|
|
2020-02-20 18:08:51 +00:00
|
|
|
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) {
|
2020-11-19 18:09:13 +00:00
|
|
|
addInlineContextLines(options);
|
2020-02-20 18:08:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 12:05:57 +00:00
|
|
|
/**
|
|
|
|
* Trims the first char of the `richText` property when it's either a space or a diff symbol.
|
|
|
|
* @param {Object} line
|
|
|
|
* @returns {Object}
|
2021-02-18 18:10:41 +00:00
|
|
|
* @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
|
2018-07-05 12:05:57 +00:00
|
|
|
*/
|
|
|
|
export function trimFirstCharOfLineContent(line = {}) {
|
2018-09-02 21:26:25 +00:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2018-08-06 21:13:34 +00:00
|
|
|
delete line.text;
|
|
|
|
|
2020-05-07 21:09:26 +00:00
|
|
|
const parsedLine = { ...line };
|
2018-07-05 12:05:57 +00:00
|
|
|
|
2018-11-09 19:48:41 +00:00
|
|
|
if (line.rich_text) {
|
|
|
|
const firstChar = parsedLine.rich_text.charAt(0);
|
2018-07-05 12:05:57 +00:00
|
|
|
|
|
|
|
if (firstChar === ' ' || firstChar === '+' || firstChar === '-') {
|
2018-11-09 19:48:41 +00:00
|
|
|
parsedLine.rich_text = line.rich_text.substring(1);
|
2018-07-05 12:05:57 +00:00
|
|
|
}
|
2018-06-21 12:22:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 12:05:57 +00:00
|
|
|
return parsedLine;
|
2018-06-21 12:22:40 +00:00
|
|
|
}
|
2018-08-08 02:50:01 +00:00
|
|
|
|
2020-01-14 12:07:41 +00:00
|
|
|
function diffFileUniqueId(file) {
|
|
|
|
return `${file.content_sha}-${file.file_hash}`;
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:08:46 +00:00
|
|
|
function mergeTwoFiles(target, source) {
|
2020-11-19 18:09:13 +00:00
|
|
|
const originalInline = target[INLINE_DIFF_LINES_KEY];
|
2020-02-13 00:08:46 +00:00
|
|
|
const missingInline = !originalInline.length;
|
2020-01-14 12:07:41 +00:00
|
|
|
|
2020-02-13 00:08:46 +00:00
|
|
|
return {
|
|
|
|
...target,
|
2020-11-19 18:09:13 +00:00
|
|
|
[INLINE_DIFF_LINES_KEY]: missingInline ? source[INLINE_DIFF_LINES_KEY] : originalInline,
|
|
|
|
parallel_diff_lines: null,
|
2020-02-13 00:08:46 +00:00
|
|
|
renderIt: source.renderIt,
|
|
|
|
collapsed: source.collapsed,
|
|
|
|
};
|
2020-01-14 12:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ensureBasicDiffFileLines(file) {
|
2020-11-19 18:09:13 +00:00
|
|
|
const missingInline = !file[INLINE_DIFF_LINES_KEY];
|
2020-01-14 12:07:41 +00:00
|
|
|
|
|
|
|
Object.assign(file, {
|
2020-11-19 18:09:13 +00:00
|
|
|
[INLINE_DIFF_LINES_KEY]: missingInline ? [] : file[INLINE_DIFF_LINES_KEY],
|
|
|
|
parallel_diff_lines: null,
|
2020-01-14 12:07:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanRichText(text) {
|
|
|
|
return text ? text.replace(/^[+ -]/, '') : undefined;
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:09:36 +00:00
|
|
|
function prepareLine(line, file) {
|
2022-10-20 15:10:58 +00:00
|
|
|
const problems = {
|
|
|
|
brokenSymlink: file.brokenSymlink,
|
|
|
|
brokenLineCode: !line.line_code,
|
|
|
|
fileOnlyMoved: file.renamed_file && file.added_lines === 0 && file.removed_lines === 0,
|
|
|
|
};
|
|
|
|
|
2020-02-13 00:08:46 +00:00
|
|
|
if (!line.alreadyPrepared) {
|
|
|
|
Object.assign(line, {
|
2022-10-20 15:10:58 +00:00
|
|
|
commentsDisabled: Boolean(
|
|
|
|
problems.brokenSymlink || problems.fileOnlyMoved || problems.brokenLineCode,
|
|
|
|
),
|
2020-02-13 00:08:46 +00:00
|
|
|
rich_text: cleanRichText(line.rich_text),
|
|
|
|
discussionsExpanded: true,
|
|
|
|
discussions: [],
|
|
|
|
hasForm: false,
|
|
|
|
text: undefined,
|
|
|
|
alreadyPrepared: true,
|
2022-10-20 15:10:58 +00:00
|
|
|
problems,
|
2020-02-13 00:08:46 +00:00
|
|
|
});
|
|
|
|
}
|
2020-01-14 12:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:09:13 +00:00
|
|
|
export function prepareLineForRenamedFile({ line, diffFile, index = 0 }) {
|
2020-05-14 06:08:18 +00:00
|
|
|
/*
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-07-28 18:09:36 +00:00
|
|
|
prepareLine(cleanLine, diffFile); // WARNING: In-Place Mutations!
|
2020-05-14 06:08:18 +00:00
|
|
|
|
|
|
|
return cleanLine;
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:07:41 +00:00
|
|
|
function prepareDiffFileLines(file) {
|
2020-11-19 18:09:13 +00:00
|
|
|
const inlineLines = file[INLINE_DIFF_LINES_KEY];
|
2020-01-14 12:07:41 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
inlineLines.forEach((line) => prepareLine(line, file)); // WARNING: In-Place Mutations!
|
2020-01-14 12:07:41 +00:00
|
|
|
|
|
|
|
return file;
|
2018-09-05 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 15:15:04 +00:00
|
|
|
function finalizeDiffFile(file) {
|
2020-01-14 12:07:41 +00:00
|
|
|
Object.assign(file, {
|
2022-02-15 15:15:04 +00:00
|
|
|
renderIt: true,
|
2020-01-14 12:07:41 +00:00
|
|
|
isShowingFullFile: false,
|
|
|
|
isLoadingFullFile: false,
|
|
|
|
discussions: [],
|
|
|
|
renderingLines: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:08:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-12-15 09:10:00 +00:00
|
|
|
export function prepareDiffData({ diff, priorFiles = [], meta = false }) {
|
2020-02-13 00:08:46 +00:00
|
|
|
const cleanedFiles = (diff.diff_files || [])
|
2022-04-20 12:09:35 +00:00
|
|
|
.map((file, index, allFiles) => prepareRawDiffFile({ file, allFiles, meta, index }))
|
2020-01-14 12:07:41 +00:00
|
|
|
.map(ensureBasicDiffFileLines)
|
|
|
|
.map(prepareDiffFileLines)
|
2022-02-15 15:15:04 +00:00
|
|
|
.map((file) => finalizeDiffFile(file));
|
2020-02-13 00:08:46 +00:00
|
|
|
|
|
|
|
return deduplicateFilesList([...priorFiles, ...cleanedFiles]);
|
2020-01-14 12:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 12:10:37 +00:00
|
|
|
export function getDiffPositionByLineCode(diffFiles) {
|
2022-02-15 15:15:04 +00:00
|
|
|
const lines = diffFiles.reduce((acc, diffFile) => {
|
2020-12-23 21:10:24 +00:00
|
|
|
diffFile[INLINE_DIFF_LINES_KEY].forEach((line) => {
|
2020-11-19 18:09:13 +00:00
|
|
|
acc.push({ file: diffFile, line });
|
|
|
|
});
|
2020-01-14 12:07:41 +00:00
|
|
|
|
2020-11-19 18:09:13 +00:00
|
|
|
return acc;
|
|
|
|
}, []);
|
2020-01-14 12:07:41 +00:00
|
|
|
|
|
|
|
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,
|
2020-04-16 09:09:37 +00:00
|
|
|
line_range: null,
|
2020-01-14 12:07:41 +00:00
|
|
|
line_code: line.line_code,
|
|
|
|
position_type: 'text',
|
|
|
|
};
|
2018-08-08 02:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
2018-09-08 06:37:41 +00:00
|
|
|
|
|
|
|
// This method will check whether the discussion is still applicable
|
|
|
|
// to the diff line in question regarding different versions of the MR
|
2018-09-18 15:27:53 +00:00
|
|
|
export function isDiscussionApplicableToLine({ discussion, diffPosition, latestDiff }) {
|
2020-08-18 15:10:33 +00:00
|
|
|
if (!diffPosition) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-28 18:09:35 +00:00
|
|
|
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;
|
2018-09-08 06:37:41 +00:00
|
|
|
|
2018-09-10 18:41:57 +00:00
|
|
|
if (discussion.original_position && discussion.position) {
|
2020-04-21 15:21:10 +00:00
|
|
|
const discussionPositions = [
|
|
|
|
discussion.original_position,
|
|
|
|
discussion.position,
|
|
|
|
...(discussion.positions || []),
|
|
|
|
];
|
2018-09-10 18:41:57 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
const removeLineRange = (position) => {
|
2020-04-28 18:09:35 +00:00
|
|
|
const { line_range: pNotUsed, ...positionNoLineRange } = position;
|
|
|
|
return positionNoLineRange;
|
|
|
|
};
|
|
|
|
|
|
|
|
return discussionPositions
|
|
|
|
.map(removeLineRange)
|
2020-12-23 21:10:24 +00:00
|
|
|
.some((position) => isEqual(position, diffPositionCopy));
|
2018-09-10 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:48:41 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
return latestDiff && discussion.active && line_code === discussion.line_code;
|
2018-09-08 06:37:41 +00:00
|
|
|
}
|
2018-10-03 09:05:43 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const getDiffMode = (diffFile) => {
|
|
|
|
const diffModeKey = Object.keys(diffModes).find((key) => diffFile[`${key}_file`]);
|
2018-12-06 14:39:29 +00:00
|
|
|
return (
|
|
|
|
diffModes[diffModeKey] ||
|
2019-02-15 17:56:50 +00:00
|
|
|
(diffFile.viewer &&
|
|
|
|
diffFile.viewer.name === diffViewerModes.mode_changed &&
|
|
|
|
diffViewerModes.mode_changed) ||
|
2018-12-06 14:39:29 +00:00
|
|
|
diffModes.replaced
|
|
|
|
);
|
2018-10-18 07:50:38 +00:00
|
|
|
};
|
2019-02-28 14:14:15 +00:00
|
|
|
|
|
|
|
export const convertExpandLines = ({
|
|
|
|
diffLines,
|
|
|
|
data,
|
|
|
|
typeKey,
|
|
|
|
oldLineKey,
|
|
|
|
newLineKey,
|
|
|
|
mapLine,
|
|
|
|
}) => {
|
|
|
|
const dataLength = data.length;
|
2019-05-03 07:33:15 +00:00
|
|
|
const lines = [];
|
|
|
|
|
|
|
|
for (let i = 0, diffLinesLength = diffLines.length; i < diffLinesLength; i += 1) {
|
|
|
|
const line = diffLines[i];
|
2019-02-28 14:14:15 +00:00
|
|
|
|
2020-02-24 09:08:51 +00:00
|
|
|
if (property(typeKey)(line) === 'match') {
|
2019-02-28 14:14:15 +00:00
|
|
|
const beforeLine = diffLines[i - 1];
|
|
|
|
const afterLine = diffLines[i + 1];
|
2020-02-24 09:08:51 +00:00
|
|
|
const newLineProperty = property(newLineKey);
|
2019-05-03 07:33:15 +00:00
|
|
|
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: [] }),
|
2020-02-24 09:08:51 +00:00
|
|
|
oldLine: (property(oldLineKey)(beforeLine) || 0) + index + 1,
|
2019-05-03 07:33:15 +00:00
|
|
|
newLine: (newLineProperty(beforeLine) || 0) + index + 1,
|
2019-02-28 14:14:15 +00:00
|
|
|
}),
|
2019-05-03 07:33:15 +00:00
|
|
|
),
|
2019-02-28 14:14:15 +00:00
|
|
|
);
|
|
|
|
} else {
|
2019-05-03 07:33:15 +00:00
|
|
|
lines.push(line);
|
2019-02-28 14:14:15 +00:00
|
|
|
}
|
2019-05-03 07:33:15 +00:00
|
|
|
}
|
2019-02-28 14:14:15 +00:00
|
|
|
|
2019-05-03 07:33:15 +00:00
|
|
|
return lines;
|
2019-02-28 14:14:15 +00:00
|
|
|
};
|
2019-05-03 07:33:15 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const idleCallback = (cb) => requestIdleCallback(cb);
|
2019-07-03 08:26:57 +00:00
|
|
|
|
2020-01-14 12:07:41 +00:00
|
|
|
function getLinesFromFileByLineCode(file, lineCode) {
|
2020-11-19 18:09:13 +00:00
|
|
|
const inlineLines = file[INLINE_DIFF_LINES_KEY];
|
2020-12-23 21:10:24 +00:00
|
|
|
const matchesCode = (line) => line.line_code === lineCode;
|
2019-07-03 08:26:57 +00:00
|
|
|
|
2020-11-19 18:09:13 +00:00
|
|
|
return inlineLines.filter(matchesCode);
|
2020-01-14 12:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const updateLineInFile = (selectedFile, lineCode, updateFn) => {
|
|
|
|
getLinesFromFileByLineCode(selectedFile, lineCode).forEach(updateFn);
|
2019-07-03 08:26:57 +00:00
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const allDiscussionWrappersExpanded = (diff) => {
|
2020-01-14 12:07:41 +00:00
|
|
|
let discussionsExpanded = true;
|
2020-12-23 21:10:24 +00:00
|
|
|
const changeExpandedResult = (line) => {
|
2020-01-14 12:07:41 +00:00
|
|
|
if (line && line.discussions.length) {
|
|
|
|
discussionsExpanded = discussionsExpanded && line.discussionsExpanded;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
diff[INLINE_DIFF_LINES_KEY].forEach((line) => {
|
2020-01-14 12:07:41 +00:00
|
|
|
changeExpandedResult(line);
|
|
|
|
});
|
|
|
|
|
|
|
|
return discussionsExpanded;
|
2019-07-03 08:26:57 +00:00
|
|
|
};
|