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

87 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { truncateSha } from '~/lib/utils/text_utility';
2021-06-08 01:23:25 +05:30
import { uuids } from '~/lib/utils/uuids';
2021-03-11 19:13:27 +05:30
2021-01-29 00:20:46 +05:30
import {
DIFF_FILE_SYMLINK_MODE,
DIFF_FILE_DELETED_MODE,
DIFF_FILE_MANUAL_COLLAPSE,
DIFF_FILE_AUTOMATIC_COLLAPSE,
2021-02-22 17:27:13 +05:30
} from '../constants';
2021-03-08 18:12:59 +05:30
import { getDerivedMergeRequestInformation } from './merge_request';
2020-10-24 23:57:45 +05:30
function fileSymlinkInformation(file, fileList) {
2021-03-08 18:12:59 +05:30
const duplicates = fileList.filter((iteratedFile) => iteratedFile.file_hash === file.file_hash);
const includesSymlink = duplicates.some((iteratedFile) => {
2020-10-24 23:57:45 +05:30
return [iteratedFile.a_mode, iteratedFile.b_mode].includes(DIFF_FILE_SYMLINK_MODE);
});
const brokenSymlinkScenario = duplicates.length > 1 && includesSymlink;
return (
brokenSymlinkScenario && {
replaced: file.b_mode === DIFF_FILE_DELETED_MODE,
wasSymbolic: file.a_mode === DIFF_FILE_SYMLINK_MODE,
isSymbolic: file.b_mode === DIFF_FILE_SYMLINK_MODE,
wasReal: ![DIFF_FILE_SYMLINK_MODE, DIFF_FILE_DELETED_MODE].includes(file.a_mode),
isReal: ![DIFF_FILE_SYMLINK_MODE, DIFF_FILE_DELETED_MODE].includes(file.b_mode),
}
);
}
2021-01-03 14:25:43 +05:30
function collapsed(file) {
const viewer = file.viewer || {};
return {
automaticallyCollapsed: viewer.automaticallyCollapsed || viewer.collapsed || false,
2021-01-29 00:20:46 +05:30
manuallyCollapsed: null,
2021-01-03 14:25:43 +05:30
};
}
2021-02-22 17:27:13 +05:30
function identifier(file) {
2021-03-08 18:12:59 +05:30
const { userOrGroup, project, id } = getDerivedMergeRequestInformation({
endpoint: file.load_collapsed_diff_url,
});
2021-02-22 17:27:13 +05:30
return uuids({
2021-03-08 18:12:59 +05:30
seeds: [userOrGroup, project, id, file.file_identifier_hash, file.blob?.id],
2021-02-22 17:27:13 +05:30
})[0];
}
export function prepareRawDiffFile({ file, allFiles, meta = false }) {
const additionalProperties = {
2020-10-24 23:57:45 +05:30
brokenSymlink: fileSymlinkInformation(file, allFiles),
2021-01-03 14:25:43 +05:30
viewer: {
...file.viewer,
...collapsed(file),
},
2021-02-22 17:27:13 +05:30
};
2021-03-08 18:12:59 +05:30
// It's possible, but not confirmed, that `blob.id` isn't available sometimes
2021-02-22 17:27:13 +05:30
// See: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/49506#note_464692057
// We don't want duplicate IDs if that's the case, so we just don't assign an ID
2021-03-08 18:12:59 +05:30
if (!meta && file.blob?.id && file.load_collapsed_diff_url) {
2021-02-22 17:27:13 +05:30
additionalProperties.id = identifier(file);
}
2020-10-24 23:57:45 +05:30
2021-02-22 17:27:13 +05:30
return Object.assign(file, additionalProperties);
2020-10-24 23:57:45 +05:30
}
2021-01-29 00:20:46 +05:30
export function collapsedType(file) {
const isManual = typeof file.viewer?.manuallyCollapsed === 'boolean';
return isManual ? DIFF_FILE_MANUAL_COLLAPSE : DIFF_FILE_AUTOMATIC_COLLAPSE;
}
export function isCollapsed(file) {
const type = collapsedType(file);
const collapsedStates = {
[DIFF_FILE_AUTOMATIC_COLLAPSE]: file.viewer?.automaticallyCollapsed || false,
[DIFF_FILE_MANUAL_COLLAPSE]: file.viewer?.manuallyCollapsed,
};
return collapsedStates[type];
}
2021-03-11 19:13:27 +05:30
export function getShortShaFromFile(file) {
return file.content_sha ? truncateSha(String(file.content_sha)) : null;
}