debian-mirror-gitlab/app/assets/javascripts/diffs/store/utils.js

792 lines
22 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { property, isEqual } from 'lodash';
2019-03-02 22:35:43 +05:30
import { truncatePathMiddleToLength } from '~/lib/utils/text_utility';
2019-07-07 11:18:12 +05:30
import { diffModes, diffViewerModes } from '~/ide/constants';
2018-11-08 19:23:39 +05:30
import {
LINE_POSITION_LEFT,
LINE_POSITION_RIGHT,
TEXT_DIFF_POSITION_TYPE,
2018-12-05 23:21:45 +05:30
LEGACY_DIFF_NOTE_TYPE,
2018-11-08 19:23:39 +05:30
DIFF_NOTE_TYPE,
NEW_LINE_TYPE,
OLD_LINE_TYPE,
MATCH_LINE_TYPE,
2018-11-20 20:47:30 +05:30
LINES_TO_BE_RENDERED_DIRECTLY,
2019-03-02 22:35:43 +05:30
TREE_TYPE,
2020-04-08 14:13:33 +05:30
INLINE_DIFF_VIEW_TYPE,
PARALLEL_DIFF_VIEW_TYPE,
2020-07-28 23:09:34 +05:30
SHOW_WHITESPACE,
NO_SHOW_WHITESPACE,
2018-11-08 19:23:39 +05:30
} from '../constants';
2020-10-24 23:57:45 +05:30
import { prepareRawDiffFile } from '../diff_file';
2018-11-08 19:23:39 +05:30
2020-11-24 15:15:51 +05:30
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', 'new-nonewline', 'old-nonewline'].includes(line.type);
/**
* 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
*
2021-01-29 00:20:46 +05:30
* @param {Boolean} inline - is inline context or not
*
2020-11-24 15:15:51 +05:30
* @returns {Object[]} parallel lines
*/
2021-01-29 00:20:46 +05:30
export const parallelizeDiffLines = (diffLines, inline) => {
2020-11-24 15:15:51 +05:30
let freeRightIndex = null;
const lines = [];
for (let i = 0, diffLinesLength = diffLines.length, index = 0; i < diffLinesLength; i += 1) {
const line = diffLines[i];
if (isRemoved(line)) {
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)) {
2021-01-29 00:20:46 +05:30
if (freeRightIndex !== null && !inline) {
2020-11-24 15:15:51 +05:30
// 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)) {
// line in the right panel is the same as in the left one
lines.push({
[LINE_POSITION_LEFT]: line,
[LINE_POSITION_RIGHT]: line,
});
freeRightIndex = null;
index += 1;
}
}
return lines;
};
2019-07-07 11:18:12 +05:30
export function findDiffFile(files, match, matchKey = 'file_hash') {
return files.find(file => file[matchKey] === match);
2018-11-08 19:23:39 +05:30
}
export const getReversePosition = linePosition => {
if (linePosition === LINE_POSITION_RIGHT) {
return LINE_POSITION_LEFT;
}
return LINE_POSITION_RIGHT;
};
2018-12-05 23:21:45 +05:30
export function getFormData(params) {
2018-11-08 19:23:39 +05:30
const {
2019-02-15 15:39:39 +05:30
commit,
2018-11-08 19:23:39 +05:30
note,
noteableType,
noteableData,
diffFile,
noteTargetLine,
diffViewType,
linePosition,
2018-12-13 13:39:08 +05:30
positionType,
2020-06-23 00:09:42 +05:30
lineRange,
2018-11-08 19:23:39 +05:30
} = params;
const position = JSON.stringify({
2019-02-15 15:39:39 +05:30
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-12-13 13:39:08 +05:30
position_type: positionType || TEXT_DIFF_POSITION_TYPE,
2019-02-15 15:39:39 +05:30
old_line: noteTargetLine ? noteTargetLine.old_line : null,
new_line: noteTargetLine ? noteTargetLine.new_line : null,
2018-12-13 13:39:08 +05:30
x: params.x,
y: params.y,
width: params.width,
height: params.height,
2020-06-23 00:09:42 +05:30
line_range: lineRange,
2018-11-08 19:23:39 +05:30
});
const postData = {
view: diffViewType,
line_type: linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
2019-02-15 15:39:39 +05:30
merge_request_diff_head_sha: diffFile.diff_refs.head_sha,
2018-11-08 19:23:39 +05:30
in_reply_to_discussion_id: '',
note_project_id: '',
target_type: noteableData.targetType,
target_id: noteableData.id,
2018-12-05 23:21:45 +05:30
return_discussion: true,
2018-11-08 19:23:39 +05:30
note: {
note,
position,
noteable_type: noteableType,
noteable_id: noteableData.id,
2019-02-15 15:39:39 +05:30
commit_id: commit && commit.id,
2018-12-05 23:21:45 +05:30
type:
2019-02-15 15:39:39 +05:30
diffFile.diff_refs.start_sha && diffFile.diff_refs.head_sha
2018-12-05 23:21:45 +05:30
? DIFF_NOTE_TYPE
: LEGACY_DIFF_NOTE_TYPE,
2019-02-15 15:39:39 +05:30
line_code: noteTargetLine ? noteTargetLine.line_code : null,
2018-11-08 19:23:39 +05:30
},
};
2018-12-05 23:21:45 +05:30
return postData;
}
export function getNoteFormData(params) {
const data = getFormData(params);
2018-11-08 19:23:39 +05:30
return {
2018-12-05 23:21:45 +05:30
endpoint: params.noteableData.create_note_path,
data,
2018-11-08 19:23:39 +05:30
};
}
export const findIndexInInlineLines = (lines, lineNumbers) => {
const { oldLineNumber, newLineNumber } = lineNumbers;
2020-04-08 14:13:33 +05:30
return lines.findIndex(
2019-02-15 15:39:39 +05:30
line => line.old_line === oldLineNumber && line.new_line === newLineNumber,
2018-11-08 19:23:39 +05:30
);
};
export const findIndexInParallelLines = (lines, lineNumbers) => {
const { oldLineNumber, newLineNumber } = lineNumbers;
2020-04-08 14:13:33 +05:30
return lines.findIndex(
2018-11-08 19:23:39 +05:30
line =>
line.left &&
line.right &&
2019-02-15 15:39:39 +05:30
line.left.old_line === oldLineNumber &&
line.right.new_line === newLineNumber,
2018-11-08 19:23:39 +05:30
);
};
2020-04-08 14:13:33 +05:30
const indexGettersByViewType = {
[INLINE_DIFF_VIEW_TYPE]: findIndexInInlineLines,
[PARALLEL_DIFF_VIEW_TYPE]: findIndexInParallelLines,
};
export const getPreviousLineIndex = (diffViewType, file, lineNumbers) => {
const findIndex = indexGettersByViewType[diffViewType];
const lines = {
[INLINE_DIFF_VIEW_TYPE]: file.highlighted_diff_lines,
[PARALLEL_DIFF_VIEW_TYPE]: file.parallel_diff_lines,
};
return findIndex && findIndex(lines[diffViewType], lineNumbers);
};
2018-11-08 19:23:39 +05:30
export function removeMatchLine(diffFile, lineNumbers, bottom) {
2019-02-15 15:39:39 +05:30
const indexForInline = findIndexInInlineLines(diffFile.highlighted_diff_lines, lineNumbers);
const indexForParallel = findIndexInParallelLines(diffFile.parallel_diff_lines, lineNumbers);
2018-11-08 19:23:39 +05:30
const factor = bottom ? 1 : -1;
2020-04-08 14:13:33 +05:30
if (indexForInline > -1) {
diffFile.highlighted_diff_lines.splice(indexForInline + factor, 1);
}
if (indexForParallel > -1) {
diffFile.parallel_diff_lines.splice(indexForParallel + factor, 1);
}
2018-11-08 19:23:39 +05:30
}
2019-10-12 21:52:04 +05:30
export function addLineReferences(lines, lineNumbers, bottom, isExpandDown, nextLineNumbers) {
2018-11-08 19:23:39 +05:30
const { oldLineNumber, newLineNumber } = lineNumbers;
const lineCount = lines.length;
let matchLineIndex = -1;
const linesWithNumbers = lines.map((l, index) => {
2019-02-15 15:39:39 +05:30
if (l.type === MATCH_LINE_TYPE) {
2018-11-08 19:23:39 +05:30
matchLineIndex = index;
} else {
2019-02-15 15:39:39 +05:30
Object.assign(l, {
old_line: bottom ? oldLineNumber + index + 1 : oldLineNumber + index - lineCount,
new_line: bottom ? newLineNumber + index + 1 : newLineNumber + index - lineCount,
2018-11-08 19:23:39 +05:30
});
}
2019-02-15 15:39:39 +05:30
return l;
2018-11-08 19:23:39 +05:30
});
if (matchLineIndex > -1) {
const line = linesWithNumbers[matchLineIndex];
2019-10-12 21:52:04 +05:30
let targetLine;
if (isExpandDown) {
targetLine = nextLineNumbers;
} else if (bottom) {
targetLine = linesWithNumbers[matchLineIndex - 1];
} else {
targetLine = linesWithNumbers[matchLineIndex + 1];
}
2018-11-08 19:23:39 +05:30
Object.assign(line, {
2019-02-15 15:39:39 +05:30
meta_data: {
old_pos: targetLine.old_line,
new_pos: targetLine.new_line,
2018-11-08 19:23:39 +05:30
},
});
}
return linesWithNumbers;
}
2020-04-08 14:13:33 +05:30
function addParallelContextLines(options) {
const { parallelLines, contextLines, lineNumbers, isExpandDown } = options;
2018-11-08 19:23:39 +05:30
const normalizedParallelLines = contextLines.map(line => ({
left: line,
right: line,
2019-07-07 11:18:12 +05:30
line_code: line.line_code,
2018-11-08 19:23:39 +05:30
}));
2019-10-12 21:52:04 +05:30
const factor = isExpandDown ? 1 : 0;
2018-11-08 19:23:39 +05:30
2019-10-12 21:52:04 +05:30
if (!isExpandDown && options.bottom) {
2018-11-08 19:23:39 +05:30
parallelLines.push(...normalizedParallelLines);
} else {
const parallelIndex = findIndexInParallelLines(parallelLines, lineNumbers);
2019-10-12 21:52:04 +05:30
parallelLines.splice(parallelIndex + factor, 0, ...normalizedParallelLines);
2018-11-08 19:23:39 +05:30
}
}
2020-04-08 14:13:33 +05:30
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) {
const { diffViewType } = options;
const contextLineHandlers = {
[INLINE_DIFF_VIEW_TYPE]: addInlineContextLines,
[PARALLEL_DIFF_VIEW_TYPE]: addParallelContextLines,
};
const contextLineHandler = contextLineHandlers[diffViewType];
if (contextLineHandler) {
contextLineHandler(options);
}
}
2018-11-08 19:23:39 +05:30
/**
* Trims the first char of the `richText` property when it's either a space or a diff symbol.
* @param {Object} line
* @returns {Object}
2020-03-13 15:44:24 +05:30
* @deprecated
2018-11-08 19:23:39 +05:30
*/
export function trimFirstCharOfLineContent(line = {}) {
2018-11-20 20:47:30 +05:30
// eslint-disable-next-line no-param-reassign
delete line.text;
2020-05-24 23:13:21 +05:30
const parsedLine = { ...line };
2018-11-08 19:23:39 +05:30
2019-02-15 15:39:39 +05:30
if (line.rich_text) {
const firstChar = parsedLine.rich_text.charAt(0);
2018-11-08 19:23:39 +05:30
if (firstChar === ' ' || firstChar === '+' || firstChar === '-') {
2019-02-15 15:39:39 +05:30
parsedLine.rich_text = line.rich_text.substring(1);
2018-11-08 19:23:39 +05:30
}
}
return parsedLine;
}
2018-11-18 11:00:15 +05:30
2019-02-15 15:39:39 +05:30
function getLineCode({ left, right }, index) {
if (left && left.line_code) {
return left.line_code;
} else if (right && right.line_code) {
return right.line_code;
}
return index;
}
2020-03-13 15:44:24 +05:30
function diffFileUniqueId(file) {
return `${file.content_sha}-${file.file_hash}`;
}
2018-11-20 20:47:30 +05:30
2020-03-13 15:44:24 +05:30
function mergeTwoFiles(target, source) {
const originalInline = target.highlighted_diff_lines;
const originalParallel = target.parallel_diff_lines;
const missingInline = !originalInline.length;
const missingParallel = !originalParallel.length;
return {
...target,
highlighted_diff_lines: missingInline ? source.highlighted_diff_lines : originalInline,
parallel_diff_lines: missingParallel ? source.parallel_diff_lines : originalParallel,
renderIt: source.renderIt,
collapsed: source.collapsed,
};
}
2018-11-20 20:47:30 +05:30
2020-03-13 15:44:24 +05:30
function ensureBasicDiffFileLines(file) {
const missingInline = !file.highlighted_diff_lines;
2020-11-24 15:15:51 +05:30
const missingParallel = !file.parallel_diff_lines || window.gon?.features?.unifiedDiffLines;
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
Object.assign(file, {
highlighted_diff_lines: missingInline ? [] : file.highlighted_diff_lines,
parallel_diff_lines: missingParallel ? [] : file.parallel_diff_lines,
});
return file;
}
function cleanRichText(text) {
return text ? text.replace(/^[+ -]/, '') : undefined;
}
2020-10-24 23:57:45 +05:30
function prepareLine(line, file) {
2020-03-13 15:44:24 +05:30
if (!line.alreadyPrepared) {
Object.assign(line, {
2020-10-24 23:57:45 +05:30
commentsDisabled: file.brokenSymlink,
2020-03-13 15:44:24 +05:30
rich_text: cleanRichText(line.rich_text),
discussionsExpanded: true,
2018-12-13 13:39:08 +05:30
discussions: [],
2020-03-13 15:44:24 +05:30
hasForm: false,
text: undefined,
alreadyPrepared: true,
2018-11-20 20:47:30 +05:30
});
}
}
2020-05-24 23:13:21 +05:30
export function prepareLineForRenamedFile({ line, diffViewType, 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,
};
2020-10-24 23:57:45 +05:30
prepareLine(cleanLine, diffFile); // WARNING: In-Place Mutations!
2020-05-24 23:13:21 +05:30
if (diffViewType === PARALLEL_DIFF_VIEW_TYPE) {
return {
left: { ...cleanLine },
right: { ...cleanLine },
line_code: cleanLine.line_code,
};
}
return cleanLine;
}
2020-03-13 15:44:24 +05:30
function prepareDiffFileLines(file) {
const inlineLines = file.highlighted_diff_lines;
const parallelLines = file.parallel_diff_lines;
let parallelLinesCount = 0;
2020-10-24 23:57:45 +05:30
inlineLines.forEach(line => prepareLine(line, file)); // WARNING: In-Place Mutations!
2020-03-13 15:44:24 +05:30
parallelLines.forEach((line, index) => {
Object.assign(line, { line_code: getLineCode(line, index) });
if (line.left) {
parallelLinesCount += 1;
2020-10-24 23:57:45 +05:30
prepareLine(line.left, file); // WARNING: In-Place Mutations!
2020-03-13 15:44:24 +05:30
}
if (line.right) {
parallelLinesCount += 1;
2020-10-24 23:57:45 +05:30
prepareLine(line.right, file); // WARNING: In-Place Mutations!
2020-03-13 15:44:24 +05:30
}
});
Object.assign(file, {
inlineLinesCount: inlineLines.length,
parallelLinesCount,
});
return file;
}
function getVisibleDiffLines(file) {
return Math.max(file.inlineLinesCount, file.parallelLinesCount);
}
function finalizeDiffFile(file) {
const lines = getVisibleDiffLines(file);
Object.assign(file, {
renderIt: lines < LINES_TO_BE_RENDERED_DIRECTLY,
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 = []) {
const cleanedFiles = (diff.diff_files || [])
2020-10-24 23:57:45 +05:30
.map((file, index, allFiles) => prepareRawDiffFile({ file, allFiles }))
2020-03-13 15:44:24 +05:30
.map(ensureBasicDiffFileLines)
.map(prepareDiffFileLines)
.map(finalizeDiffFile);
return deduplicateFilesList([...priorFiles, ...cleanedFiles]);
}
2020-11-24 15:15:51 +05:30
export function getDiffPositionByLineCode(diffFiles) {
2020-03-13 15:44:24 +05:30
let lines = [];
const hasInlineDiffs = diffFiles.some(file => file.highlighted_diff_lines.length > 0);
2020-11-24 15:15:51 +05:30
if (hasInlineDiffs) {
2020-03-13 15:44:24 +05:30
// In either of these cases, we can use `highlighted_diff_lines` because
// that will include all of the parallel diff lines, too
lines = diffFiles.reduce((acc, diffFile) => {
2019-02-15 15:39:39 +05:30
diffFile.highlighted_diff_lines.forEach(line => {
2020-03-13 15:44:24 +05:30
acc.push({ file: diffFile, line });
});
return acc;
}, []);
} else {
// If we're in single diff view mode and the inline lines haven't been
// loaded yet, we need to parse the parallel lines
lines = diffFiles.reduce((acc, diffFile) => {
diffFile.parallel_diff_lines.forEach(pair => {
// It's possible for a parallel line to have an opposite line that doesn't exist
// For example: *deleted* lines will have `null` right lines, while
// *added* lines will have `null` left lines.
// So we have to check each line before we push it onto the array so we're not
// pushing null line diffs
if (pair.left) {
acc.push({ file: diffFile, line: pair.left });
}
if (pair.right) {
acc.push({ file: diffFile, line: pair.right });
2018-11-18 11:00:15 +05:30
}
});
2020-03-13 15:44:24 +05:30
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,
2020-04-22 19:07:51 +05:30
line_range: null,
2020-03-13 15:44:24 +05:30
line_code: line.line_code,
position_type: 'text',
};
2018-11-18 11:00:15 +05:30
}
return acc;
}, {});
}
2018-11-20 20:47:30 +05:30
// This method will check whether the discussion is still applicable
// to the diff line in question regarding different versions of the MR
2018-12-05 23:21:45 +05:30
export function isDiscussionApplicableToLine({ discussion, diffPosition, latestDiff }) {
2020-10-24 23:57:45 +05:30
if (!diffPosition) {
return false;
}
2020-05-24 23:13:21 +05:30
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-12-05 23:21:45 +05:30
if (discussion.original_position && discussion.position) {
2020-04-22 19:07:51 +05:30
const discussionPositions = [
discussion.original_position,
discussion.position,
...(discussion.positions || []),
];
2018-12-05 23:21:45 +05:30
2020-05-24 23:13:21 +05:30
const removeLineRange = position => {
const { line_range: pNotUsed, ...positionNoLineRange } = position;
return positionNoLineRange;
};
return discussionPositions
.map(removeLineRange)
.some(position => isEqual(position, diffPositionCopy));
2018-12-05 23:21:45 +05:30
}
2018-11-20 20:47:30 +05:30
2019-02-15 15:39:39 +05:30
// eslint-disable-next-line
return latestDiff && discussion.active && line_code === discussion.line_code;
2018-11-20 20:47:30 +05:30
}
2018-12-05 23:21:45 +05:30
2019-03-02 22:35:43 +05:30
export const getLowestSingleFolder = folder => {
const getFolder = (blob, start = []) =>
blob.tree.reduce(
(acc, file) => {
const shouldGetFolder = file.tree.length === 1 && file.tree[0].type === TREE_TYPE;
const currentFileTypeTree = file.type === TREE_TYPE;
const path = shouldGetFolder || currentFileTypeTree ? acc.path.concat(file.name) : acc.path;
const tree = shouldGetFolder || currentFileTypeTree ? acc.tree.concat(file) : acc.tree;
if (shouldGetFolder) {
const firstFolder = getFolder(file);
path.push(...firstFolder.path);
tree.push(...firstFolder.tree);
}
return {
...acc,
path,
tree,
};
},
{ path: start, tree: [] },
);
const { path, tree } = getFolder(folder, [folder.name]);
return {
path: truncatePathMiddleToLength(path.join('/'), 40),
treeAcc: tree.length ? tree[tree.length - 1].tree : null,
};
};
export const flattenTree = tree => {
const flatten = blobTree =>
blobTree.reduce((acc, file) => {
const blob = file;
let treeToFlatten = blob.tree;
if (file.type === TREE_TYPE && file.tree.length === 1) {
const { treeAcc, path } = getLowestSingleFolder(file);
if (treeAcc) {
blob.name = path;
treeToFlatten = flatten(treeAcc);
}
}
blob.tree = flatten(treeToFlatten);
return acc.concat(blob);
}, []);
return flatten(tree);
};
export const generateTreeList = files => {
const { treeEntries, tree } = files.reduce(
2018-12-05 23:21:45 +05:30
(acc, file) => {
2019-02-15 15:39:39 +05:30
const split = file.new_path.split('/');
2018-12-05 23:21:45 +05:30
split.forEach((name, i) => {
const parent = acc.treeEntries[split.slice(0, i).join('/')];
const path = `${parent ? `${parent.path}/` : ''}${name}`;
if (!acc.treeEntries[path]) {
2019-02-15 15:39:39 +05:30
const type = path === file.new_path ? 'blob' : 'tree';
2018-12-05 23:21:45 +05:30
acc.treeEntries[path] = {
key: path,
path,
name,
type,
tree: [],
};
const entry = acc.treeEntries[path];
if (type === 'blob') {
Object.assign(entry, {
changed: true,
2019-02-15 15:39:39 +05:30
tempFile: file.new_file,
deleted: file.deleted_file,
fileHash: file.file_hash,
addedLines: file.added_lines,
removedLines: file.removed_lines,
2019-03-02 22:35:43 +05:30
parentPath: parent ? `${parent.path}/` : '/',
2021-01-29 00:20:46 +05:30
submodule: file.submodule,
2018-12-05 23:21:45 +05:30
});
} else {
Object.assign(entry, {
opened: true,
});
}
(parent ? parent.tree : acc.tree).push(entry);
}
});
return acc;
},
{ treeEntries: {}, tree: [] },
);
2018-12-13 13:39:08 +05:30
2019-03-02 22:35:43 +05:30
return { treeEntries, tree: flattenTree(tree) };
};
2018-12-13 13:39:08 +05:30
export const getDiffMode = diffFile => {
2019-02-15 15:39:39 +05:30
const diffModeKey = Object.keys(diffModes).find(key => diffFile[`${key}_file`]);
return (
diffModes[diffModeKey] ||
2019-07-07 11:18:12 +05:30
(diffFile.viewer &&
diffFile.viewer.name === diffViewerModes.mode_changed &&
diffViewerModes.mode_changed) ||
2019-02-15 15:39:39 +05:30
diffModes.replaced
);
2018-12-13 13:39:08 +05:30
};
2019-07-07 11:18:12 +05:30
export const convertExpandLines = ({
diffLines,
data,
typeKey,
oldLineKey,
newLineKey,
mapLine,
}) => {
const dataLength = data.length;
2019-07-31 22:56:46 +05:30
const lines = [];
for (let i = 0, diffLinesLength = diffLines.length; i < diffLinesLength; i += 1) {
const line = diffLines[i];
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
if (property(typeKey)(line) === 'match') {
2019-07-07 11:18:12 +05:30
const beforeLine = diffLines[i - 1];
const afterLine = diffLines[i + 1];
2020-04-08 14:13:33 +05:30
const newLineProperty = property(newLineKey);
2019-07-31 22:56:46 +05:30
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-04-08 14:13:33 +05:30
oldLine: (property(oldLineKey)(beforeLine) || 0) + index + 1,
2019-07-31 22:56:46 +05:30
newLine: (newLineProperty(beforeLine) || 0) + index + 1,
2019-07-07 11:18:12 +05:30
}),
2019-07-31 22:56:46 +05:30
),
2019-07-07 11:18:12 +05:30
);
} else {
2019-07-31 22:56:46 +05:30
lines.push(line);
2019-07-07 11:18:12 +05:30
}
2019-07-31 22:56:46 +05:30
}
2019-07-07 11:18:12 +05:30
2019-07-31 22:56:46 +05:30
return lines;
2019-07-07 11:18:12 +05:30
};
2019-07-31 22:56:46 +05:30
export const idleCallback = cb => requestIdleCallback(cb);
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
function getLinesFromFileByLineCode(file, lineCode) {
const parallelLines = file.parallel_diff_lines;
const inlineLines = file.highlighted_diff_lines;
const matchesCode = line => line.line_code === lineCode;
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
return [
...parallelLines.reduce((acc, line) => {
if (line.left) {
acc.push(line.left);
}
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
if (line.right) {
acc.push(line.right);
}
return acc;
}, []),
...inlineLines,
].filter(matchesCode);
}
export const updateLineInFile = (selectedFile, lineCode, updateFn) => {
getLinesFromFileByLineCode(selectedFile, lineCode).forEach(updateFn);
2019-09-30 21:07:59 +05:30
};
export const allDiscussionWrappersExpanded = diff => {
2020-03-13 15:44:24 +05:30
let discussionsExpanded = true;
const changeExpandedResult = line => {
if (line && line.discussions.length) {
discussionsExpanded = discussionsExpanded && line.discussionsExpanded;
}
};
diff.parallel_diff_lines.forEach(line => {
changeExpandedResult(line.left);
changeExpandedResult(line.right);
});
diff.highlighted_diff_lines.forEach(line => {
changeExpandedResult(line);
});
return discussionsExpanded;
2019-09-30 21:07:59 +05:30
};
2020-07-28 23:09:34 +05:30
export const getDefaultWhitespace = (queryString, cookie) => {
// Querystring should override stored cookie value
if (queryString) return queryString === SHOW_WHITESPACE;
if (cookie === NO_SHOW_WHITESPACE) return false;
return true;
};