summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-13 00:08:46 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-13 00:08:46 +0000
commit47d1f417f03aca055b2ba49c32bb6fb01c459831 (patch)
tree200f05f28369cbf3a34abcb4a3c388558268b86f /app/assets/javascripts/diffs
parent006e89697dd5165f355afc20fc6bb0cdfa7b381a (diff)
downloadgitlab-ce-47d1f417f03aca055b2ba49c32bb6fb01c459831.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/diffs')
-rw-r--r--app/assets/javascripts/diffs/store/actions.js27
-rw-r--r--app/assets/javascripts/diffs/store/mutations.js4
-rw-r--r--app/assets/javascripts/diffs/store/utils.js79
3 files changed, 65 insertions, 45 deletions
diff --git a/app/assets/javascripts/diffs/store/actions.js b/app/assets/javascripts/diffs/store/actions.js
index b920e041135..bd85105ccb4 100644
--- a/app/assets/javascripts/diffs/store/actions.js
+++ b/app/assets/javascripts/diffs/store/actions.js
@@ -111,15 +111,22 @@ export const fetchDiffFilesBatch = ({ commit, state }) => {
commit(types.SET_BATCH_LOADING, true);
commit(types.SET_RETRIEVING_BATCHES, true);
- const getBatch = page =>
+ const getBatch = (page = 1) =>
axios
.get(state.endpointBatch, {
- params: { ...urlParams, page },
+ params: {
+ ...urlParams,
+ page,
+ },
})
.then(({ data: { pagination, diff_files } }) => {
commit(types.SET_DIFF_DATA_BATCH, { diff_files });
commit(types.SET_BATCH_LOADING, false);
- if (!pagination.next_page) commit(types.SET_RETRIEVING_BATCHES, false);
+
+ if (!pagination.next_page) {
+ commit(types.SET_RETRIEVING_BATCHES, false);
+ }
+
return pagination.next_page;
})
.then(nextPage => nextPage && getBatch(nextPage))
@@ -132,6 +139,11 @@ export const fetchDiffFilesBatch = ({ commit, state }) => {
export const fetchDiffFilesMeta = ({ commit, state }) => {
const worker = new TreeWorker();
+ const urlParams = {};
+
+ if (state.useSingleDiffStyle) {
+ urlParams.view = state.diffViewType;
+ }
commit(types.SET_LOADING, true);
@@ -142,16 +154,17 @@ export const fetchDiffFilesMeta = ({ commit, state }) => {
});
return axios
- .get(state.endpointMetadata)
+ .get(mergeUrlParams(urlParams, state.endpointMetadata))
.then(({ data }) => {
const strippedData = { ...data };
+
delete strippedData.diff_files;
commit(types.SET_LOADING, false);
commit(types.SET_MERGE_REQUEST_DIFFS, data.merge_request_diffs || []);
commit(types.SET_DIFF_DATA, strippedData);
- prepareDiffData(data);
- worker.postMessage(data.diff_files);
+ worker.postMessage(prepareDiffData(data, state.diffFiles));
+
return data;
})
.catch(() => worker.terminate());
@@ -226,7 +239,7 @@ export const startRenderDiffsQueue = ({ state, commit }) => {
const nextFile = state.diffFiles.find(
file =>
!file.renderIt &&
- (file.viewer && (!file.viewer.collapsed || !file.viewer.name === diffViewerModes.text)),
+ (file.viewer && (!file.viewer.collapsed || file.viewer.name !== diffViewerModes.text)),
);
if (nextFile) {
diff --git a/app/assets/javascripts/diffs/store/mutations.js b/app/assets/javascripts/diffs/store/mutations.js
index 1505be1a0b2..c26411af5d7 100644
--- a/app/assets/javascripts/diffs/store/mutations.js
+++ b/app/assets/javascripts/diffs/store/mutations.js
@@ -148,8 +148,8 @@ export default {
},
[types.ADD_COLLAPSED_DIFFS](state, { file, data }) {
- prepareDiffData(data);
- const [newFileData] = data.diff_files.filter(f => f.file_hash === file.file_hash);
+ const files = prepareDiffData(data);
+ const [newFileData] = files.filter(f => f.file_hash === file.file_hash);
const selectedFile = state.diffFiles.find(f => f.file_hash === file.file_hash);
Object.assign(selectedFile, { ...newFileData });
},
diff --git a/app/assets/javascripts/diffs/store/utils.js b/app/assets/javascripts/diffs/store/utils.js
index b379f1fabef..80972d2aeb8 100644
--- a/app/assets/javascripts/diffs/store/utils.js
+++ b/app/assets/javascripts/diffs/store/utils.js
@@ -217,30 +217,19 @@ function diffFileUniqueId(file) {
return `${file.content_sha}-${file.file_hash}`;
}
-function combineDiffFilesWithPriorFiles(files, prior = []) {
- files.forEach(file => {
- const id = diffFileUniqueId(file);
- const oldMatch = prior.find(oldFile => diffFileUniqueId(oldFile) === id);
-
- if (oldMatch) {
- const missingInline = !file.highlighted_diff_lines;
- const missingParallel = !file.parallel_diff_lines;
-
- if (missingInline) {
- Object.assign(file, {
- highlighted_diff_lines: oldMatch.highlighted_diff_lines,
- });
- }
+function mergeTwoFiles(target, source) {
+ const originalInline = target.highlighted_diff_lines;
+ const originalParallel = target.parallel_diff_lines;
+ const missingInline = !originalInline.length;
+ const missingParallel = !originalParallel.length;
- if (missingParallel) {
- Object.assign(file, {
- parallel_diff_lines: oldMatch.parallel_diff_lines,
- });
- }
- }
- });
-
- return files;
+ 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,
+ };
}
function ensureBasicDiffFileLines(file) {
@@ -260,13 +249,16 @@ function cleanRichText(text) {
}
function prepareLine(line) {
- return Object.assign(line, {
- rich_text: cleanRichText(line.rich_text),
- discussionsExpanded: true,
- discussions: [],
- hasForm: false,
- text: undefined,
- });
+ if (!line.alreadyPrepared) {
+ Object.assign(line, {
+ rich_text: cleanRichText(line.rich_text),
+ discussionsExpanded: true,
+ discussions: [],
+ hasForm: false,
+ text: undefined,
+ alreadyPrepared: true,
+ });
+ }
}
function prepareDiffFileLines(file) {
@@ -288,11 +280,11 @@ function prepareDiffFileLines(file) {
parallelLinesCount += 1;
prepareLine(line.right);
}
+ });
- Object.assign(file, {
- inlineLinesCount: inlineLines.length,
- parallelLinesCount,
- });
+ Object.assign(file, {
+ inlineLinesCount: inlineLines.length,
+ parallelLinesCount,
});
return file;
@@ -318,11 +310,26 @@ function finalizeDiffFile(file) {
return file;
}
-export function prepareDiffData(diffData, priorFiles) {
- return combineDiffFilesWithPriorFiles(diffData.diff_files, priorFiles)
+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 || [])
.map(ensureBasicDiffFileLines)
.map(prepareDiffFileLines)
.map(finalizeDiffFile);
+
+ return deduplicateFilesList([...priorFiles, ...cleanedFiles]);
}
export function getDiffPositionByLineCode(diffFiles, useSingleDiffStyle) {