Commit 7af9c56a authored by Thomas Randolph's avatar Thomas Randolph

Upgrade diff preparation

This change modifies the actions and mutations
to handle only a single diff style (either inline or
parallel) and properly merge that with existing
files already in state (if any).
parent 07dadce8
...@@ -6,6 +6,7 @@ import { __ } from '~/locale'; ...@@ -6,6 +6,7 @@ import { __ } from '~/locale';
import createFlash from '~/flash'; import createFlash from '~/flash';
import PanelResizer from '~/vue_shared/components/panel_resizer.vue'; import PanelResizer from '~/vue_shared/components/panel_resizer.vue';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { isSingleViewStyle } from '~/helpers/diffs_helper';
import eventHub from '../../notes/event_hub'; import eventHub from '../../notes/event_hub';
import CompareVersions from './compare_versions.vue'; import CompareVersions from './compare_versions.vue';
import DiffFile from './diff_file.vue'; import DiffFile from './diff_file.vue';
...@@ -145,6 +146,9 @@ export default { ...@@ -145,6 +146,9 @@ export default {
}, },
watch: { watch: {
diffViewType() { diffViewType() {
if (this.needsReload() || this.needsFirstLoad()) {
this.refetchDiffData();
}
this.adjustView(); this.adjustView();
}, },
shouldShow() { shouldShow() {
...@@ -224,6 +228,16 @@ export default { ...@@ -224,6 +228,16 @@ export default {
{ timeout: 1000 }, { timeout: 1000 },
); );
}, },
needsReload() {
return (
this.glFeatures.singleMrDiffView &&
this.diffFiles.length &&
isSingleViewStyle(this.diffFiles[0])
);
},
needsFirstLoad() {
return this.glFeatures.singleMrDiffView && !this.diffFiles.length;
},
fetchData(toggleTree = true) { fetchData(toggleTree = true) {
if (this.glFeatures.diffsBatchLoad) { if (this.glFeatures.diffsBatchLoad) {
this.fetchDiffFilesMeta() this.fetchDiffFilesMeta()
...@@ -237,6 +251,13 @@ export default { ...@@ -237,6 +251,13 @@ export default {
}); });
this.fetchDiffFilesBatch() this.fetchDiffFilesBatch()
.then(() => {
// Guarantee the discussions are assigned after the batch finishes.
// Just watching the length of the discussions or the diff files
// isn't enough, because with split diff loading, neither will
// change when loading the other half of the diff files.
this.setDiscussions();
})
.then(() => this.startDiffRendering()) .then(() => this.startDiffRendering())
.catch(() => { .catch(() => {
createFlash(__('Something went wrong on our end. Please try again!')); createFlash(__('Something went wrong on our end. Please try again!'));
...@@ -250,6 +271,7 @@ export default { ...@@ -250,6 +271,7 @@ export default {
requestIdleCallback( requestIdleCallback(
() => { () => {
this.setDiscussions();
this.startRenderDiffsQueue(); this.startRenderDiffsQueue();
}, },
{ timeout: 1000 }, { timeout: 1000 },
......
...@@ -4,6 +4,7 @@ import _ from 'underscore'; ...@@ -4,6 +4,7 @@ import _ from 'underscore';
import { GlLoadingIcon } from '@gitlab/ui'; import { GlLoadingIcon } from '@gitlab/ui';
import { __, sprintf } from '~/locale'; import { __, sprintf } from '~/locale';
import createFlash from '~/flash'; import createFlash from '~/flash';
import { hasDiff } from '~/helpers/diffs_helper';
import eventHub from '../../notes/event_hub'; import eventHub from '../../notes/event_hub';
import DiffFileHeader from './diff_file_header.vue'; import DiffFileHeader from './diff_file_header.vue';
import DiffContent from './diff_content.vue'; import DiffContent from './diff_content.vue';
...@@ -55,12 +56,7 @@ export default { ...@@ -55,12 +56,7 @@ export default {
return this.isLoadingCollapsedDiff || (!this.file.renderIt && !this.isCollapsed); return this.isLoadingCollapsedDiff || (!this.file.renderIt && !this.isCollapsed);
}, },
hasDiff() { hasDiff() {
return ( return hasDiff(this.file);
(this.file.highlighted_diff_lines &&
this.file.parallel_diff_lines &&
this.file.parallel_diff_lines.length > 0) ||
!this.file.blob.readable_text
);
}, },
isFileTooLarge() { isFileTooLarge() {
return this.file.viewer.error === diffViewerErrors.too_large; return this.file.viewer.error === diffViewerErrors.too_large;
......
...@@ -65,6 +65,10 @@ export const fetchDiffFiles = ({ state, commit }) => { ...@@ -65,6 +65,10 @@ export const fetchDiffFiles = ({ state, commit }) => {
w: state.showWhitespace ? '0' : '1', w: state.showWhitespace ? '0' : '1',
}; };
if (state.useSingleDiffStyle) {
urlParams.view = state.diffViewType;
}
commit(types.SET_LOADING, true); commit(types.SET_LOADING, true);
worker.addEventListener('message', ({ data }) => { worker.addEventListener('message', ({ data }) => {
...@@ -90,13 +94,22 @@ export const fetchDiffFiles = ({ state, commit }) => { ...@@ -90,13 +94,22 @@ export const fetchDiffFiles = ({ state, commit }) => {
}; };
export const fetchDiffFilesBatch = ({ commit, state }) => { export const fetchDiffFilesBatch = ({ commit, state }) => {
const urlParams = {
per_page: DIFFS_PER_PAGE,
w: state.showWhitespace ? '0' : '1',
};
if (state.useSingleDiffStyle) {
urlParams.view = state.diffViewType;
}
commit(types.SET_BATCH_LOADING, true); commit(types.SET_BATCH_LOADING, true);
commit(types.SET_RETRIEVING_BATCHES, true); commit(types.SET_RETRIEVING_BATCHES, true);
const getBatch = page => const getBatch = page =>
axios axios
.get(state.endpointBatch, { .get(state.endpointBatch, {
params: { page, per_page: DIFFS_PER_PAGE, w: state.showWhitespace ? '0' : '1' }, params: { ...urlParams, page },
}) })
.then(({ data: { pagination, diff_files } }) => { .then(({ data: { pagination, diff_files } }) => {
commit(types.SET_DIFF_DATA_BATCH, { diff_files }); commit(types.SET_DIFF_DATA_BATCH, { diff_files });
...@@ -150,7 +163,10 @@ export const assignDiscussionsToDiff = ( ...@@ -150,7 +163,10 @@ export const assignDiscussionsToDiff = (
{ commit, state, rootState }, { commit, state, rootState },
discussions = rootState.notes.discussions, discussions = rootState.notes.discussions,
) => { ) => {
const diffPositionByLineCode = getDiffPositionByLineCode(state.diffFiles); const diffPositionByLineCode = getDiffPositionByLineCode(
state.diffFiles,
state.useSingleDiffStyle,
);
const hash = getLocationHash(); const hash = getLocationHash();
discussions discussions
...@@ -339,24 +355,23 @@ export const toggleFileDiscussions = ({ getters, dispatch }, diff) => { ...@@ -339,24 +355,23 @@ export const toggleFileDiscussions = ({ getters, dispatch }, diff) => {
export const toggleFileDiscussionWrappers = ({ commit }, diff) => { export const toggleFileDiscussionWrappers = ({ commit }, diff) => {
const discussionWrappersExpanded = allDiscussionWrappersExpanded(diff); const discussionWrappersExpanded = allDiscussionWrappersExpanded(diff);
let linesWithDiscussions; const lineCodesWithDiscussions = new Set();
if (diff.highlighted_diff_lines) { const { parallel_diff_lines: parallelLines, highlighted_diff_lines: inlineLines } = diff;
linesWithDiscussions = diff.highlighted_diff_lines.filter(line => line.discussions.length); const allLines = inlineLines.concat(
} parallelLines.map(line => line.left),
if (diff.parallel_diff_lines) { parallelLines.map(line => line.right),
linesWithDiscussions = diff.parallel_diff_lines.filter( );
line => const lineHasDiscussion = line => Boolean(line?.discussions.length);
(line.left && line.left.discussions.length) || const registerDiscussionLine = line => lineCodesWithDiscussions.add(line.line_code);
(line.right && line.right.discussions.length),
); allLines.filter(lineHasDiscussion).forEach(registerDiscussionLine);
}
if (lineCodesWithDiscussions.size) {
if (linesWithDiscussions.length) { Array.from(lineCodesWithDiscussions).forEach(lineCode => {
linesWithDiscussions.forEach(line => {
commit(types.TOGGLE_LINE_DISCUSSIONS, { commit(types.TOGGLE_LINE_DISCUSSIONS, {
fileHash: diff.file_hash, fileHash: diff.file_hash,
lineCode: line.line_code,
expanded: !discussionWrappersExpanded, expanded: !discussionWrappersExpanded,
lineCode,
}); });
}); });
} }
......
...@@ -45,26 +45,28 @@ export default { ...@@ -45,26 +45,28 @@ export default {
}, },
[types.SET_DIFF_DATA](state, data) { [types.SET_DIFF_DATA](state, data) {
let files = state.diffFiles;
if ( if (
!( !(gon?.features?.diffsBatchLoad && window.location.search.indexOf('diff_id') === -1) &&
gon && data.diff_files
gon.features &&
gon.features.diffsBatchLoad &&
window.location.search.indexOf('diff_id') === -1
)
) { ) {
prepareDiffData(data); files = prepareDiffData(data, files);
} }
Object.assign(state, { Object.assign(state, {
...convertObjectPropsToCamelCase(data), ...convertObjectPropsToCamelCase(data),
diffFiles: files,
}); });
}, },
[types.SET_DIFF_DATA_BATCH](state, data) { [types.SET_DIFF_DATA_BATCH](state, data) {
prepareDiffData(data); const files = prepareDiffData(data, state.diffFiles);
state.diffFiles.push(...data.diff_files); Object.assign(state, {
...convertObjectPropsToCamelCase(data),
diffFiles: files,
});
}, },
[types.RENDER_FILE](state, file) { [types.RENDER_FILE](state, file) {
...@@ -88,11 +90,11 @@ export default { ...@@ -88,11 +90,11 @@ export default {
if (!diffFile) return; if (!diffFile) return;
if (diffFile.highlighted_diff_lines) { if (diffFile.highlighted_diff_lines.length) {
diffFile.highlighted_diff_lines.find(l => l.line_code === lineCode).hasForm = hasForm; diffFile.highlighted_diff_lines.find(l => l.line_code === lineCode).hasForm = hasForm;
} }
if (diffFile.parallel_diff_lines) { if (diffFile.parallel_diff_lines.length) {
const line = diffFile.parallel_diff_lines.find(l => { const line = diffFile.parallel_diff_lines.find(l => {
const { left, right } = l; const { left, right } = l;
...@@ -153,13 +155,13 @@ export default { ...@@ -153,13 +155,13 @@ export default {
}, },
[types.EXPAND_ALL_FILES](state) { [types.EXPAND_ALL_FILES](state) {
state.diffFiles = state.diffFiles.map(file => ({ state.diffFiles.forEach(file => {
...file, Object.assign(file, {
viewer: { viewer: Object.assign(file.viewer, {
...file.viewer, collapsed: false,
collapsed: false, }),
}, });
})); });
}, },
[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode, hash }) { [types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode, hash }) {
...@@ -197,29 +199,29 @@ export default { ...@@ -197,29 +199,29 @@ export default {
}; };
}; };
state.diffFiles = state.diffFiles.map(diffFile => { state.diffFiles.forEach(file => {
if (diffFile.file_hash === fileHash) { if (file.file_hash === fileHash) {
const file = { ...diffFile }; if (file.highlighted_diff_lines.length) {
file.highlighted_diff_lines.forEach(line => {
if (file.highlighted_diff_lines) { Object.assign(
file.highlighted_diff_lines = file.highlighted_diff_lines.map(line => line,
setDiscussionsExpanded(lineCheck(line) ? mapDiscussions(line) : line), setDiscussionsExpanded(lineCheck(line) ? mapDiscussions(line) : line),
); );
});
} }
if (file.parallel_diff_lines) { if (file.parallel_diff_lines.length) {
file.parallel_diff_lines = file.parallel_diff_lines.map(line => { file.parallel_diff_lines.forEach(line => {
const left = line.left && lineCheck(line.left); const left = line.left && lineCheck(line.left);
const right = line.right && lineCheck(line.right); const right = line.right && lineCheck(line.right);
if (left || right) { if (left || right) {
return { Object.assign(line, {
...line,
left: line.left ? setDiscussionsExpanded(mapDiscussions(line.left)) : null, left: line.left ? setDiscussionsExpanded(mapDiscussions(line.left)) : null,
right: line.right right: line.right
? setDiscussionsExpanded(mapDiscussions(line.right, () => !left)) ? setDiscussionsExpanded(mapDiscussions(line.right, () => !left))
: null, : null,
}; });
} }
return line; return line;
...@@ -227,15 +229,15 @@ export default { ...@@ -227,15 +229,15 @@ export default {
} }
if (!file.parallel_diff_lines || !file.highlighted_diff_lines) { if (!file.parallel_diff_lines || !file.highlighted_diff_lines) {
file.discussions = (file.discussions || []) const newDiscussions = (file.discussions || [])
.filter(d => d.id !== discussion.id) .filter(d => d.id !== discussion.id)
.concat(discussion); .concat(discussion);
}
return file; Object.assign(file, {
discussions: newDiscussions,
});
}
} }
return diffFile;
}); });
}, },
...@@ -259,9 +261,9 @@ export default { ...@@ -259,9 +261,9 @@ export default {
[types.TOGGLE_LINE_DISCUSSIONS](state, { fileHash, lineCode, expanded }) { [types.TOGGLE_LINE_DISCUSSIONS](state, { fileHash, lineCode, expanded }) {
const selectedFile = state.diffFiles.find(f => f.file_hash === fileHash); const selectedFile = state.diffFiles.find(f => f.file_hash === fileHash);
updateLineInFile(selectedFile, lineCode, line => updateLineInFile(selectedFile, lineCode, line => {
Object.assign(line, { discussionsExpanded: expanded }), Object.assign(line, { discussionsExpanded: expanded });
); });
}, },
[types.TOGGLE_FOLDER_OPEN](state, path) { [types.TOGGLE_FOLDER_OPEN](state, path) {
......
This diff is collapsed.
export function hasInlineLines(diffFile) {
return diffFile?.highlighted_diff_lines?.length > 0; /* eslint-disable-line camelcase */
}
export function hasParallelLines(diffFile) {
return diffFile?.parallel_diff_lines?.length > 0; /* eslint-disable-line camelcase */
}
export function isSingleViewStyle(diffFile) {
return !hasParallelLines(diffFile) || !hasInlineLines(diffFile);
}
export function hasDiff(diffFile) {
return (
hasInlineLines(diffFile) ||
hasParallelLines(diffFile) ||
!diffFile?.blob?.readable_text /* eslint-disable-line camelcase */
);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment