summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs/utils/diff_file.js
blob: 7e6fde320d26897312b6568ab930618273a85c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { truncateSha } from '~/lib/utils/text_utility';

import {
  DIFF_FILE_SYMLINK_MODE,
  DIFF_FILE_DELETED_MODE,
  DIFF_FILE_MANUAL_COLLAPSE,
  DIFF_FILE_AUTOMATIC_COLLAPSE,
} from '../constants';
import { getDerivedMergeRequestInformation } from './merge_request';
import { uuids } from './uuids';

function fileSymlinkInformation(file, fileList) {
  const duplicates = fileList.filter((iteratedFile) => iteratedFile.file_hash === file.file_hash);
  const includesSymlink = duplicates.some((iteratedFile) => {
    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),
    }
  );
}

function collapsed(file) {
  const viewer = file.viewer || {};

  return {
    automaticallyCollapsed: viewer.automaticallyCollapsed || viewer.collapsed || false,
    manuallyCollapsed: null,
  };
}

function identifier(file) {
  const { userOrGroup, project, id } = getDerivedMergeRequestInformation({
    endpoint: file.load_collapsed_diff_url,
  });

  return uuids({
    seeds: [userOrGroup, project, id, file.file_identifier_hash, file.blob?.id],
  })[0];
}

export function prepareRawDiffFile({ file, allFiles, meta = false }) {
  const additionalProperties = {
    brokenSymlink: fileSymlinkInformation(file, allFiles),
    viewer: {
      ...file.viewer,
      ...collapsed(file),
    },
  };

  // It's possible, but not confirmed, that `blob.id` isn't available sometimes
  // 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
  if (!meta && file.blob?.id && file.load_collapsed_diff_url) {
    additionalProperties.id = identifier(file);
  }

  return Object.assign(file, additionalProperties);
}

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];
}

export function getShortShaFromFile(file) {
  return file.content_sha ? truncateSha(String(file.content_sha)) : null;
}