| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Set as ImSet} from 'immutable'; |
| b69ab31 | | | 9 | import type {ChangedFile, ChangedFileStatus, RepoRelativePath} from './types'; |
| b69ab31 | | | 10 | import type {UIChangedFile, VisualChangedFileStatus} from './UncommittedChanges'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {minimalDisambiguousPaths} from 'shared/minimalDisambiguousPaths'; |
| b69ab31 | | | 13 | import {notEmpty} from 'shared/utils'; |
| b69ab31 | | | 14 | import {t} from './i18n'; |
| b69ab31 | | | 15 | import {ChangedFileMode} from './types'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | export function processChangedFiles( |
| b69ab31 | | | 18 | files: Array<ChangedFile>, |
| b69ab31 | | | 19 | submodulePaths: ImSet<RepoRelativePath> | undefined, |
| b69ab31 | | | 20 | ): Array<UIChangedFile> { |
| b69ab31 | | | 21 | const disambiguousPaths = minimalDisambiguousPaths(files.map(file => file.path)); |
| b69ab31 | | | 22 | const copySources = new Set(files.map(file => file.copy).filter(notEmpty)); |
| b69ab31 | | | 23 | const removedFiles = new Set(files.filter(file => file.status === 'R').map(file => file.path)); |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | return ( |
| b69ab31 | | | 26 | files |
| b69ab31 | | | 27 | .map((file, i) => { |
| b69ab31 | | | 28 | const minimalName = disambiguousPaths[i]; |
| b69ab31 | | | 29 | const mode = |
| b69ab31 | | | 30 | submodulePaths && submodulePaths.has(file.path) |
| b69ab31 | | | 31 | ? ChangedFileMode.Submodule |
| b69ab31 | | | 32 | : ChangedFileMode.Regular; |
| b69ab31 | | | 33 | let fileLabel = minimalName; |
| b69ab31 | | | 34 | let tooltip = `${nameForStatus(file.status)}: ${file.path}`; |
| b69ab31 | | | 35 | let copiedFrom; |
| b69ab31 | | | 36 | let renamedFrom; |
| b69ab31 | | | 37 | let visualStatus: VisualChangedFileStatus = file.status; |
| b69ab31 | | | 38 | if (file.copy != null) { |
| b69ab31 | | | 39 | // Disambiguate between original file and the newly copy's name, |
| b69ab31 | | | 40 | // instead of disambiguating among all file names. |
| b69ab31 | | | 41 | const [originalName, copiedName] = minimalDisambiguousPaths([file.copy, file.path]); |
| b69ab31 | | | 42 | fileLabel = `${originalName} → ${copiedName}`; |
| b69ab31 | | | 43 | if (removedFiles.has(file.copy)) { |
| b69ab31 | | | 44 | renamedFrom = file.copy; |
| b69ab31 | | | 45 | tooltip = t('$newPath\n\nThis file was renamed from $originalPath', { |
| b69ab31 | | | 46 | replace: {$newPath: file.path, $originalPath: file.copy}, |
| b69ab31 | | | 47 | }); |
| b69ab31 | | | 48 | visualStatus = 'Renamed'; |
| b69ab31 | | | 49 | } else { |
| b69ab31 | | | 50 | copiedFrom = file.copy; |
| b69ab31 | | | 51 | tooltip = t('$newPath\n\nThis file was copied from $originalPath', { |
| b69ab31 | | | 52 | replace: {$newPath: file.path, $originalPath: file.copy}, |
| b69ab31 | | | 53 | }); |
| b69ab31 | | | 54 | visualStatus = 'Copied'; |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | return { |
| b69ab31 | | | 59 | path: file.path, |
| b69ab31 | | | 60 | label: fileLabel, |
| b69ab31 | | | 61 | status: file.status, |
| b69ab31 | | | 62 | mode, |
| b69ab31 | | | 63 | visualStatus, |
| b69ab31 | | | 64 | copiedFrom, |
| b69ab31 | | | 65 | renamedFrom, |
| b69ab31 | | | 66 | tooltip, |
| b69ab31 | | | 67 | }; |
| b69ab31 | | | 68 | }) |
| b69ab31 | | | 69 | // Hide files that were renamed. This comes after the map since we need to use the index to refer to minimalDisambiguousPaths |
| b69ab31 | | | 70 | .filter(file => !(file.status === 'R' && copySources.has(file.path))) |
| b69ab31 | | | 71 | .sort((a, b) => |
| b69ab31 | | | 72 | a.visualStatus === b.visualStatus |
| b69ab31 | | | 73 | ? a.path.localeCompare(b.path) |
| b69ab31 | | | 74 | : sortKeyForStatus[a.visualStatus] - sortKeyForStatus[b.visualStatus], |
| b69ab31 | | | 75 | ) |
| b69ab31 | | | 76 | ); |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | const sortKeyForStatus: Record<VisualChangedFileStatus, number> = { |
| b69ab31 | | | 80 | M: 0, |
| b69ab31 | | | 81 | Renamed: 1, |
| b69ab31 | | | 82 | A: 2, |
| b69ab31 | | | 83 | Copied: 3, |
| b69ab31 | | | 84 | R: 4, |
| b69ab31 | | | 85 | '!': 5, |
| b69ab31 | | | 86 | '?': 6, |
| b69ab31 | | | 87 | U: 7, |
| b69ab31 | | | 88 | Resolved: 8, |
| b69ab31 | | | 89 | }; |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | function nameForStatus(status: ChangedFileStatus): string { |
| b69ab31 | | | 92 | switch (status) { |
| b69ab31 | | | 93 | case '!': |
| b69ab31 | | | 94 | return t('Missing'); |
| b69ab31 | | | 95 | case '?': |
| b69ab31 | | | 96 | return t('Untracked'); |
| b69ab31 | | | 97 | case 'A': |
| b69ab31 | | | 98 | return t('Added'); |
| b69ab31 | | | 99 | case 'M': |
| b69ab31 | | | 100 | return t('Modified'); |
| b69ab31 | | | 101 | case 'R': |
| b69ab31 | | | 102 | return t('Removed'); |
| b69ab31 | | | 103 | case 'U': |
| b69ab31 | | | 104 | return t('Unresolved'); |
| b69ab31 | | | 105 | case 'Resolved': |
| b69ab31 | | | 106 | return t('Resolved'); |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | } |