| 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 {RecordOf} from 'immutable'; |
| b69ab31 | | | 9 | import type {Author, Hash, RepoPath} from 'shared/types/common'; |
| b69ab31 | | | 10 | import type {FileFlag} from 'shared/types/stack'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Map as ImMap, Set as ImSet, List, Record, is} from 'immutable'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | /** Check if a path at the given commit is a rename. */ |
| b69ab31 | | | 15 | export function isRename(commit: CommitState, path: RepoPath): boolean { |
| b69ab31 | | | 16 | const files = commit.files; |
| b69ab31 | | | 17 | const copyFromPath = files.get(path)?.copyFrom; |
| b69ab31 | | | 18 | if (copyFromPath == null) { |
| b69ab31 | | | 19 | return false; |
| b69ab31 | | | 20 | } |
| b69ab31 | | | 21 | return isAbsent(files.get(copyFromPath)); |
| b69ab31 | | | 22 | } |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | /** Test if a file is absent. */ |
| b69ab31 | | | 25 | export function isAbsent(file: FileState | FileMetadata | undefined): boolean { |
| b69ab31 | | | 26 | if (file == null) { |
| b69ab31 | | | 27 | return true; |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | return file.flags === ABSENT_FLAG; |
| b69ab31 | | | 30 | } |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | /** Test if a file has utf-8 content. */ |
| b69ab31 | | | 33 | export function isUtf8(file: FileState): boolean { |
| b69ab31 | | | 34 | return typeof file.data === 'string' || file.data instanceof FileIdx; |
| b69ab31 | | | 35 | } |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | /** Test if 2 files have the same content, ignoring "copyFrom". */ |
| b69ab31 | | | 38 | export function isContentSame(file1: FileState, file2: FileState): boolean { |
| b69ab31 | | | 39 | return is(file1.data, file2.data) && (file1.flags ?? '') === (file2.flags ?? ''); |
| b69ab31 | | | 40 | } |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | /** Extract metadata */ |
| b69ab31 | | | 43 | export function toMetadata(file: FileState): FileMetadata { |
| b69ab31 | | | 44 | return FileMetadata({copyFrom: file.copyFrom, flags: file.flags}); |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | type DateTupleProps = { |
| b69ab31 | | | 48 | /** UTC Unix timestamp in seconds. */ |
| b69ab31 | | | 49 | unix: number; |
| b69ab31 | | | 50 | /** Timezone offset in minutes. */ |
| b69ab31 | | | 51 | tz: number; |
| b69ab31 | | | 52 | }; |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | export const DateTuple = Record<DateTupleProps>({unix: 0, tz: 0}); |
| b69ab31 | | | 55 | export type DateTuple = RecordOf<DateTupleProps>; |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | /** Mutable commit state. */ |
| b69ab31 | | | 58 | export type CommitStateProps = { |
| b69ab31 | | | 59 | rev: CommitRev; |
| b69ab31 | | | 60 | /** Original hashes. Used for "predecessor" information. */ |
| b69ab31 | | | 61 | originalNodes: ImSet<Hash>; |
| b69ab31 | | | 62 | /** |
| b69ab31 | | | 63 | * Unique identifier within the stack. Useful for React animation. |
| b69ab31 | | | 64 | * |
| b69ab31 | | | 65 | * Note this should not be a random string, since we expect the CommitState[] |
| b69ab31 | | | 66 | * state to be purely derived from the initial ExportStack. It makes it easier |
| b69ab31 | | | 67 | * to check what commits are actually modified by just comparing CommitStates. |
| b69ab31 | | | 68 | * The "skip unchanged commits" logic is used by `calculateImportStack()`. |
| b69ab31 | | | 69 | * |
| b69ab31 | | | 70 | * We use commit hashes initially. When there is a split or add a new commit, |
| b69ab31 | | | 71 | * we assign new keys in a predicable (non-random) way. This property is |
| b69ab31 | | | 72 | * never empty, unlike `originalNodes`. |
| b69ab31 | | | 73 | */ |
| b69ab31 | | | 74 | key: string; |
| b69ab31 | | | 75 | author: Author; |
| b69ab31 | | | 76 | date: DateTuple; |
| b69ab31 | | | 77 | /** Commit message. */ |
| b69ab31 | | | 78 | text: string; |
| b69ab31 | | | 79 | /** |
| b69ab31 | | | 80 | * - hash: commit hash is immutable; this commit and ancestors |
| b69ab31 | | | 81 | * cannot be edited in any way. |
| b69ab31 | | | 82 | * - content: file contents are immutable; commit hash can change |
| b69ab31 | | | 83 | * if ancestors are changed. |
| b69ab31 | | | 84 | * - diff: file changes (diff) are immutable; file contents or |
| b69ab31 | | | 85 | * commit hash can change if ancestors are changed. |
| b69ab31 | | | 86 | * - none: nothing is immutable; this commit can be edited. |
| b69ab31 | | | 87 | */ |
| b69ab31 | | | 88 | immutableKind: 'hash' | 'content' | 'diff' | 'none'; |
| b69ab31 | | | 89 | /** Parent commits. */ |
| b69ab31 | | | 90 | parents: List<CommitRev>; |
| b69ab31 | | | 91 | /** Changed files. */ |
| b69ab31 | | | 92 | files: ImMap<RepoPath, FileState>; |
| b69ab31 | | | 93 | }; |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | export const CommitState = Record<CommitStateProps>({ |
| b69ab31 | | | 96 | rev: 0 as CommitRev, |
| b69ab31 | | | 97 | originalNodes: ImSet(), |
| b69ab31 | | | 98 | key: '', |
| b69ab31 | | | 99 | author: '', |
| b69ab31 | | | 100 | date: DateTuple(), |
| b69ab31 | | | 101 | text: '', |
| b69ab31 | | | 102 | immutableKind: 'none', |
| b69ab31 | | | 103 | parents: List(), |
| b69ab31 | | | 104 | files: ImMap(), |
| b69ab31 | | | 105 | }); |
| b69ab31 | | | 106 | export type CommitState = RecordOf<CommitStateProps>; |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | /** |
| b69ab31 | | | 109 | * Similar to `ExportFile` but `data` can be lazy by redirecting to a rev in a file stack. |
| b69ab31 | | | 110 | * Besides, supports "absent" state. |
| b69ab31 | | | 111 | */ |
| b69ab31 | | | 112 | type FileStateProps = { |
| b69ab31 | | | 113 | data: string | Base85 | FileIdx | DataRef; |
| b69ab31 | | | 114 | } & FileMetadataProps; |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | /** |
| b69ab31 | | | 117 | * File metadata properties without file content. |
| b69ab31 | | | 118 | */ |
| b69ab31 | | | 119 | type FileMetadataProps = { |
| b69ab31 | | | 120 | /** If present, this file is copied (or renamed) from another file. */ |
| b69ab31 | | | 121 | copyFrom?: RepoPath; |
| b69ab31 | | | 122 | /** |
| b69ab31 | | | 123 | * If present, whether this file is special (symlink, submodule, deleted, |
| b69ab31 | | | 124 | * executable). |
| b69ab31 | | | 125 | */ |
| b69ab31 | | | 126 | flags?: FileFlag; |
| b69ab31 | | | 127 | }; |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | type Base85Props = {dataBase85: string}; |
| b69ab31 | | | 130 | export const Base85 = Record<Base85Props>({dataBase85: ''}); |
| b69ab31 | | | 131 | export type Base85 = RecordOf<Base85Props>; |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | type DataRefProps = {node: Hash; path: RepoPath}; |
| b69ab31 | | | 134 | export const DataRef = Record<DataRefProps>({node: '', path: ''}); |
| b69ab31 | | | 135 | export type DataRef = RecordOf<DataRefProps>; |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | export const FileState = Record<FileStateProps>({data: '', copyFrom: undefined, flags: ''}); |
| b69ab31 | | | 138 | export type FileState = RecordOf<FileStateProps>; |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | export const FileMetadata = Record<FileMetadataProps>({copyFrom: undefined, flags: ''}); |
| b69ab31 | | | 141 | export type FileMetadata = RecordOf<FileMetadataProps>; |
| b69ab31 | | | 142 | |
| b69ab31 | | | 143 | export type FileStackIndex = number; |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | type FileIdxProps = { |
| b69ab31 | | | 146 | fileIdx: FileStackIndex; |
| b69ab31 | | | 147 | fileRev: FileRev; |
| b69ab31 | | | 148 | }; |
| b69ab31 | | | 149 | |
| b69ab31 | | | 150 | type CommitIdxProps = { |
| b69ab31 | | | 151 | rev: CommitRev; |
| b69ab31 | | | 152 | path: RepoPath; |
| b69ab31 | | | 153 | }; |
| b69ab31 | | | 154 | |
| b69ab31 | | | 155 | export const FileIdx = Record<FileIdxProps>({fileIdx: 0, fileRev: 0 as FileRev}); |
| b69ab31 | | | 156 | export type FileIdx = RecordOf<FileIdxProps>; |
| b69ab31 | | | 157 | |
| b69ab31 | | | 158 | export const CommitIdx = Record<CommitIdxProps>({rev: -1 as CommitRev, path: ''}); |
| b69ab31 | | | 159 | export type CommitIdx = RecordOf<CommitIdxProps>; |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | export const ABSENT_FLAG = 'a'; |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | /** |
| b69ab31 | | | 164 | * Represents an absent (or deleted) file. |
| b69ab31 | | | 165 | * |
| b69ab31 | | | 166 | * Helps simplify `null` handling logic. Since `data` is a regular |
| b69ab31 | | | 167 | * string, an absent file can be compared (data-wise) with its |
| b69ab31 | | | 168 | * adjacent versions and edited. This makes it easier to, for example, |
| b69ab31 | | | 169 | * split a newly added file. |
| b69ab31 | | | 170 | */ |
| b69ab31 | | | 171 | export const ABSENT_FILE = FileState({ |
| b69ab31 | | | 172 | data: '', |
| b69ab31 | | | 173 | flags: ABSENT_FLAG, |
| b69ab31 | | | 174 | }); |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | /** A revision number used in the `FileStackState`. Identifies a version of a multi-version file. */ |
| b69ab31 | | | 177 | export type FileRev = number & {__brand: 'FileStackRev'}; |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | /** A revision number used in the `CommitStackState`. Identifies a commit in the stack. */ |
| b69ab31 | | | 180 | export type CommitRev = number & {__branded: 'CommitRev'}; |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | // Re-export |
| b69ab31 | | | 183 | export type {FileFlag}; |