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

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import { diffViewerModes as viewerModes } from '~/ide/constants';
import { changeInPercent, numberToHumanSize } from '~/lib/utils/number_utils';
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];
}
2021-09-04 01:27:46 +05:30
export const isNotDiffable = (file) => file?.viewer?.name === viewerModes.not_diffable;
2021-02-22 17:27:13 +05:30
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;
}
2021-09-04 01:27:46 +05:30
export function stats(file) {
let valid = false;
let classes = '';
let sign = '';
let text = '';
let percent = 0;
let diff = 0;
if (file) {
percent = changeInPercent(file.old_size, file.new_size);
diff = file.new_size - file.old_size;
sign = diff >= 0 ? '+' : '';
text = `${sign}${numberToHumanSize(diff)} (${sign}${percent}%)`;
valid = true;
if (diff > 0) {
classes = 'gl-text-green-600';
} else if (diff < 0) {
classes = 'gl-text-red-500';
}
}
return {
changed: diff,
text,
percent,
classes,
sign,
valid,
};
}