| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {List, RecordOf} from 'immutable'; |
| 9 | import type {InternalTypes} from '../InternalTypes'; |
| 10 | import type {CommitPreview, WithPreviewType} from '../previews'; |
| 11 | import type {CommitRev} from '../stackEdit/commitStackState'; |
| 12 | import type { |
| 13 | CommitInfo, |
| 14 | CommitPhaseType, |
| 15 | Hash, |
| 16 | RepoRelativePath, |
| 17 | StableCommitMetadata, |
| 18 | SuccessorInfo, |
| 19 | } from '../types'; |
| 20 | |
| 21 | import {Record} from 'immutable'; |
| 22 | import {SelfUpdate} from 'shared/immutableExt'; |
| 23 | |
| 24 | type DagExt = { |
| 25 | /** Distance ancestors that are treated as direct parents. */ |
| 26 | ancestors?: List<Hash>; |
| 27 | |
| 28 | /** |
| 29 | * Insertion batch. Larger: later inserted. |
| 30 | * All 'sl log' commits share a same initial number. |
| 31 | * Later previews might have larger numbers. |
| 32 | * Used for sorting. |
| 33 | */ |
| 34 | seqNumber?: number; |
| 35 | |
| 36 | /** If true, this is a virtual "You are here" commit. */ |
| 37 | isYouAreHere?: boolean; |
| 38 | |
| 39 | /** If constructed from a "CommitStack", the "Rev" of the commit. */ |
| 40 | stackRev?: CommitRev; |
| 41 | }; |
| 42 | |
| 43 | // Note: There are some non-immutable containers (Array) in `CommitInfo` |
| 44 | // such as bookmarks. Since the "commitInfos" are "normalized" by |
| 45 | // `reuseEqualObjects`. Those non-immutable properties should still |
| 46 | // compare fine. |
| 47 | type CommitInfoExtProps = CommitInfo & WithPreviewType & DagExt; |
| 48 | |
| 49 | const CommitInfoExtRecord = Record<CommitInfoExtProps>({ |
| 50 | title: '', |
| 51 | hash: '', |
| 52 | parents: [], |
| 53 | grandparents: [], |
| 54 | phase: 'draft', |
| 55 | isDot: false, |
| 56 | author: '', |
| 57 | date: new Date(0), |
| 58 | description: '', |
| 59 | bookmarks: [], |
| 60 | remoteBookmarks: [], |
| 61 | successorInfo: undefined, |
| 62 | closestPredecessors: undefined, |
| 63 | optimisticRevset: undefined, |
| 64 | filePathsSample: [], |
| 65 | totalFileCount: 0, |
| 66 | diffId: undefined, |
| 67 | isFollower: undefined, |
| 68 | stableCommitMetadata: undefined, |
| 69 | maxCommonPathPrefix: '', |
| 70 | fullRepoBranch: undefined, |
| 71 | |
| 72 | // WithPreviewType |
| 73 | previewType: undefined, |
| 74 | |
| 75 | // DagExt |
| 76 | ancestors: undefined, |
| 77 | seqNumber: undefined, |
| 78 | isYouAreHere: undefined, |
| 79 | stackRev: undefined, |
| 80 | }); |
| 81 | type CommitInfoExtRecord = RecordOf<CommitInfoExtProps>; |
| 82 | |
| 83 | /** Immutable, extended `CommitInfo` */ |
| 84 | export class DagCommitInfo extends SelfUpdate<CommitInfoExtRecord> { |
| 85 | constructor(record: CommitInfoExtRecord) { |
| 86 | super(record); |
| 87 | } |
| 88 | |
| 89 | static fromCommitInfo(info: Partial<CommitInfoExtProps>): DagCommitInfo { |
| 90 | const record = CommitInfoExtRecord(info); |
| 91 | return new DagCommitInfo(record); |
| 92 | } |
| 93 | |
| 94 | // Immutable.js APIs |
| 95 | |
| 96 | set<K extends keyof CommitInfoExtProps>(key: K, value: CommitInfoExtProps[K]): DagCommitInfo { |
| 97 | return new DagCommitInfo(this.inner.set(key, value)); |
| 98 | } |
| 99 | |
| 100 | withMutations(mutator: (mutable: CommitInfoExtRecord) => CommitInfoExtRecord) { |
| 101 | const record = this.inner.withMutations(mutator); |
| 102 | return new DagCommitInfo(record); |
| 103 | } |
| 104 | |
| 105 | merge( |
| 106 | ...collections: Array<Partial<CommitInfoExtProps> | Iterable<[string, unknown]>> |
| 107 | ): DagCommitInfo { |
| 108 | return this.withMutations(m => m.merge(...collections)); |
| 109 | } |
| 110 | |
| 111 | // Getters |
| 112 | |
| 113 | public get title(): string { |
| 114 | return this.inner.title; |
| 115 | } |
| 116 | |
| 117 | public get hash(): Hash { |
| 118 | return this.inner.hash; |
| 119 | } |
| 120 | |
| 121 | public get parents(): ReadonlyArray<Hash> { |
| 122 | return this.inner.parents; |
| 123 | } |
| 124 | |
| 125 | public get grandparents(): ReadonlyArray<Hash> { |
| 126 | return this.inner.grandparents; |
| 127 | } |
| 128 | |
| 129 | get phase(): CommitPhaseType { |
| 130 | return this.inner.phase; |
| 131 | } |
| 132 | |
| 133 | get isDot(): boolean { |
| 134 | return this.inner.isDot; |
| 135 | } |
| 136 | |
| 137 | get author(): string { |
| 138 | return this.inner.author; |
| 139 | } |
| 140 | |
| 141 | get date(): Date { |
| 142 | return this.inner.date; |
| 143 | } |
| 144 | |
| 145 | get description(): string { |
| 146 | return this.inner.description; |
| 147 | } |
| 148 | |
| 149 | get bookmarks(): ReadonlyArray<string> { |
| 150 | return this.inner.bookmarks; |
| 151 | } |
| 152 | |
| 153 | get remoteBookmarks(): ReadonlyArray<string> { |
| 154 | return this.inner.remoteBookmarks; |
| 155 | } |
| 156 | |
| 157 | get successorInfo(): Readonly<SuccessorInfo> | undefined { |
| 158 | return this.inner.successorInfo; |
| 159 | } |
| 160 | |
| 161 | get closestPredecessors(): ReadonlyArray<Hash> | undefined { |
| 162 | return this.inner.closestPredecessors; |
| 163 | } |
| 164 | |
| 165 | get optimisticRevset(): string | undefined { |
| 166 | return this.inner.optimisticRevset; |
| 167 | } |
| 168 | |
| 169 | get filePathsSample(): ReadonlyArray<RepoRelativePath> { |
| 170 | return this.inner.filePathsSample; |
| 171 | } |
| 172 | |
| 173 | get totalFileCount(): number { |
| 174 | return this.inner.totalFileCount; |
| 175 | } |
| 176 | |
| 177 | get diffId(): string | undefined { |
| 178 | return this.inner.diffId; |
| 179 | } |
| 180 | |
| 181 | get isFollower(): boolean | undefined { |
| 182 | return this.inner.isFollower; |
| 183 | } |
| 184 | |
| 185 | get stableCommitMetadata(): ReadonlyArray<StableCommitMetadata> | undefined { |
| 186 | return this.inner.stableCommitMetadata; |
| 187 | } |
| 188 | |
| 189 | get fullRepoBranch(): InternalTypes['FullRepoBranch'] | undefined { |
| 190 | return this.inner.fullRepoBranch; |
| 191 | } |
| 192 | |
| 193 | get previewType(): CommitPreview | undefined { |
| 194 | return this.inner.previewType; |
| 195 | } |
| 196 | |
| 197 | get ancestors(): List<Hash> | undefined { |
| 198 | return this.inner.ancestors; |
| 199 | } |
| 200 | |
| 201 | get seqNumber(): number | undefined { |
| 202 | return this.inner.seqNumber; |
| 203 | } |
| 204 | |
| 205 | get isYouAreHere(): boolean | undefined { |
| 206 | return this.inner.isYouAreHere; |
| 207 | } |
| 208 | |
| 209 | get stackRev(): CommitRev | undefined { |
| 210 | return this.inner.stackRev; |
| 211 | } |
| 212 | |
| 213 | get maxCommonPathPrefix(): string { |
| 214 | return this.inner.maxCommonPathPrefix; |
| 215 | } |
| 216 | } |
| 217 | |