| 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 {Hash, RepoPath} from 'shared/types/common'; |
| b69ab31 | | | 9 | import type {ExportFile, ImportCommit} from 'shared/types/stack'; |
| b69ab31 | | | 10 | import type {RepoRelativePath} from './types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import Immutable from 'immutable'; |
| b69ab31 | | | 13 | import {atom, useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 14 | import {RateLimiter} from 'shared/RateLimiter'; |
| b69ab31 | | | 15 | import {SelfUpdate} from 'shared/immutableExt'; |
| b69ab31 | | | 16 | import clientToServerAPI from './ClientToServerAPI'; |
| b69ab31 | | | 17 | import {t} from './i18n'; |
| b69ab31 | | | 18 | import {dagWithPreviews, uncommittedChangesWithPreviews} from './previews'; |
| b69ab31 | | | 19 | import {atomResetOnCwdChange} from './repositoryData'; |
| b69ab31 | | | 20 | import {latestUncommittedChangesTimestamp} from './serverAPIState'; |
| b69ab31 | | | 21 | import {ChunkSelectState} from './stackEdit/chunkSelectState'; |
| b69ab31 | | | 22 | import {assert} from './utils'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | type SingleFileSelection = |
| b69ab31 | | | 25 | | false /* not selected */ |
| b69ab31 | | | 26 | | true /* selected, default */ |
| b69ab31 | | | 27 | | ChunkSelectState /* maybe partially selected */; |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | type PartialSelectionProps = { |
| b69ab31 | | | 30 | /** Explicitly set selection. */ |
| b69ab31 | | | 31 | fileMap: Immutable.Map<RepoRelativePath, SingleFileSelection>; |
| b69ab31 | | | 32 | /** For files not in fileMap, whether they are selected or not. */ |
| b69ab31 | | | 33 | selectByDefault: boolean; |
| b69ab31 | | | 34 | expanded: Immutable.Set<RepoRelativePath>; |
| b69ab31 | | | 35 | }; |
| b69ab31 | | | 36 | const PartialSelectionRecord = Immutable.Record<PartialSelectionProps>({ |
| b69ab31 | | | 37 | fileMap: Immutable.Map(), |
| b69ab31 | | | 38 | selectByDefault: true, |
| b69ab31 | | | 39 | expanded: Immutable.Set(), |
| b69ab31 | | | 40 | }); |
| b69ab31 | | | 41 | type PartialSelectionRecord = Immutable.RecordOf<PartialSelectionProps>; |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | /** |
| b69ab31 | | | 44 | * Selection of partial changes made by a commit. |
| b69ab31 | | | 45 | * |
| b69ab31 | | | 46 | * Intended to be useful for both concrete commits and the `wdir()` virtual commit. |
| b69ab31 | | | 47 | * This class does not handle the differences between `wdir()` and concrete commits, |
| b69ab31 | | | 48 | * like how to load the file content, and how to get the list of changed files. |
| b69ab31 | | | 49 | * Those differences are handled at a higher level. |
| b69ab31 | | | 50 | */ |
| b69ab31 | | | 51 | export class PartialSelection extends SelfUpdate<PartialSelectionRecord> { |
| b69ab31 | | | 52 | constructor(record: PartialSelectionRecord) { |
| b69ab31 | | | 53 | super(record); |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | set<K extends keyof PartialSelectionProps>( |
| b69ab31 | | | 57 | key: K, |
| b69ab31 | | | 58 | value: PartialSelectionProps[K], |
| b69ab31 | | | 59 | ): PartialSelection { |
| b69ab31 | | | 60 | return new PartialSelection(this.inner.set(key, value)); |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | /** Empty selection. */ |
| b69ab31 | | | 64 | static empty(props: {selectByDefault?: boolean}): PartialSelection { |
| b69ab31 | | | 65 | return new PartialSelection(PartialSelectionRecord(props)); |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | /** Explicitly select a file. */ |
| b69ab31 | | | 69 | select(path: RepoRelativePath): PartialSelection { |
| b69ab31 | | | 70 | return new PartialSelection(this.inner.setIn(['fileMap', path], true)); |
| b69ab31 | | | 71 | } |
| b69ab31 | | | 72 | |
| b69ab31 | | | 73 | /** Explicitly deselect a file. */ |
| b69ab31 | | | 74 | deselect(path: RepoRelativePath): PartialSelection { |
| b69ab31 | | | 75 | return new PartialSelection(this.inner.setIn(['fileMap', path], false)).toggleExpand( |
| b69ab31 | | | 76 | path, |
| b69ab31 | | | 77 | false, |
| b69ab31 | | | 78 | ); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | /** Reset to the "default" state. Useful for commit/amend. */ |
| b69ab31 | | | 82 | clear(): PartialSelection { |
| b69ab31 | | | 83 | return this.set('fileMap', Immutable.Map()).set('expanded', Immutable.Set()); |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | /** Toggle expansion of a file. */ |
| b69ab31 | | | 87 | toggleExpand(path: RepoRelativePath, select?: boolean): PartialSelection { |
| b69ab31 | | | 88 | const expanded = this.inner.expanded; |
| b69ab31 | | | 89 | const newExpanded = |
| b69ab31 | | | 90 | (select ?? !expanded.has(path)) ? expanded.add(path) : expanded.remove(path); |
| b69ab31 | | | 91 | return this.set('expanded', newExpanded); |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | /** Test if a file was expanded. */ |
| b69ab31 | | | 95 | isExpanded(path: RepoRelativePath): boolean { |
| b69ab31 | | | 96 | return this.inner.expanded.has(path); |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | /** Drop "chunk selection" states. */ |
| b69ab31 | | | 100 | discardPartialSelections() { |
| b69ab31 | | | 101 | const newFileMap = this.inner.fileMap.filter( |
| b69ab31 | | | 102 | fileSelection => !(fileSelection instanceof ChunkSelectState), |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | return new PartialSelection(this.inner.merge({fileMap: newFileMap, expanded: Immutable.Set()})); |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | /** Start chunk selection for the given file. */ |
| b69ab31 | | | 108 | startChunkSelect( |
| b69ab31 | | | 109 | path: RepoRelativePath, |
| b69ab31 | | | 110 | a: string, |
| b69ab31 | | | 111 | b: string, |
| b69ab31 | | | 112 | selected: boolean | string, |
| b69ab31 | | | 113 | normalize = false, |
| b69ab31 | | | 114 | ): PartialSelection { |
| b69ab31 | | | 115 | const chunkState = ChunkSelectState.fromText(a, b, selected, normalize); |
| b69ab31 | | | 116 | return new PartialSelection(this.inner.setIn(['fileMap', path], chunkState)); |
| b69ab31 | | | 117 | } |
| b69ab31 | | | 118 | |
| b69ab31 | | | 119 | /** Edit chunk selection for a file. */ |
| b69ab31 | | | 120 | editChunkSelect( |
| b69ab31 | | | 121 | path: RepoRelativePath, |
| b69ab31 | | | 122 | newValue: ((chunkState: ChunkSelectState) => ChunkSelectState) | ChunkSelectState, |
| b69ab31 | | | 123 | ): PartialSelection { |
| b69ab31 | | | 124 | const chunkState = this.inner.fileMap.get(path); |
| b69ab31 | | | 125 | assert( |
| b69ab31 | | | 126 | chunkState instanceof ChunkSelectState, |
| b69ab31 | | | 127 | 'PartialSelection.editChunkSelect() called without startChunkEdit', |
| b69ab31 | | | 128 | ); |
| b69ab31 | | | 129 | const newChunkState = typeof newValue === 'function' ? newValue(chunkState) : newValue; |
| b69ab31 | | | 130 | return new PartialSelection(this.inner.setIn(['fileMap', path], newChunkState)); |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | getSelection(path: RepoRelativePath): SingleFileSelection { |
| b69ab31 | | | 134 | const record = this.inner; |
| b69ab31 | | | 135 | return record.fileMap.get(path) ?? record.selectByDefault; |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | /** |
| b69ab31 | | | 139 | * Return true if a file is selected, false if deselected, |
| b69ab31 | | | 140 | * or a string with the edited content. |
| b69ab31 | | | 141 | * Even if the file is being chunk edited, this function might |
| b69ab31 | | | 142 | * still return true or false. |
| b69ab31 | | | 143 | */ |
| b69ab31 | | | 144 | getSimplifiedSelection(path: RepoRelativePath): boolean | string { |
| b69ab31 | | | 145 | const selected = this.getSelection(path); |
| b69ab31 | | | 146 | if (selected === true || selected === false) { |
| b69ab31 | | | 147 | return selected; |
| b69ab31 | | | 148 | } |
| b69ab31 | | | 149 | const chunkState: ChunkSelectState = selected; |
| b69ab31 | | | 150 | const text = chunkState.getSelectedText(); |
| b69ab31 | | | 151 | if (text === chunkState.a) { |
| b69ab31 | | | 152 | return false; |
| b69ab31 | | | 153 | } |
| b69ab31 | | | 154 | if (text === chunkState.b) { |
| b69ab31 | | | 155 | return true; |
| b69ab31 | | | 156 | } |
| b69ab31 | | | 157 | return text; |
| b69ab31 | | | 158 | } |
| b69ab31 | | | 159 | |
| b69ab31 | | | 160 | isFullyOrPartiallySelected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 161 | return this.getSimplifiedSelection(path) !== false; |
| b69ab31 | | | 162 | } |
| b69ab31 | | | 163 | |
| b69ab31 | | | 164 | isPartiallySelected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 165 | return typeof this.getSimplifiedSelection(path) !== 'boolean'; |
| b69ab31 | | | 166 | } |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | isFullySelected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 169 | return this.getSimplifiedSelection(path) === true; |
| b69ab31 | | | 170 | } |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | isDeselected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 173 | return this.getSimplifiedSelection(path) === false; |
| b69ab31 | | | 174 | } |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | isEverythingSelected(getAllPaths: () => Array<RepoRelativePath>): boolean { |
| b69ab31 | | | 177 | const record = this.inner; |
| b69ab31 | | | 178 | const paths = record.selectByDefault ? record.fileMap.keySeq() : getAllPaths(); |
| b69ab31 | | | 179 | return paths.every(p => this.getSimplifiedSelection(p) === true); |
| b69ab31 | | | 180 | } |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | isNothingSelected(getAllPaths: () => Array<RepoRelativePath>): boolean { |
| b69ab31 | | | 183 | const record = this.inner; |
| b69ab31 | | | 184 | const paths = record.selectByDefault ? getAllPaths() : record.fileMap.keySeq(); |
| b69ab31 | | | 185 | return paths.every(p => this.getSimplifiedSelection(p) === false); |
| b69ab31 | | | 186 | } |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | /** |
| b69ab31 | | | 189 | * Produce a `ImportStack['files']` useful for the `debugimportstack` command |
| b69ab31 | | | 190 | * to create commits. |
| b69ab31 | | | 191 | * |
| b69ab31 | | | 192 | * `allPaths` provides extra file paths to be considered. This is useful |
| b69ab31 | | | 193 | * when we only track "deselected files". |
| b69ab31 | | | 194 | */ |
| b69ab31 | | | 195 | calculateImportStackFiles( |
| b69ab31 | | | 196 | allPaths: Array<RepoRelativePath>, |
| b69ab31 | | | 197 | inverse = false, |
| b69ab31 | | | 198 | ): ImportCommit['files'] { |
| b69ab31 | | | 199 | const files: ImportCommit['files'] = {}; |
| b69ab31 | | | 200 | // Process files in the fileMap. Note: this map might only contain the "deselected" |
| b69ab31 | | | 201 | // files, depending on selectByDefault. |
| b69ab31 | | | 202 | const fileMap = this.inner.fileMap; |
| b69ab31 | | | 203 | fileMap.forEach((fileSelection, path) => { |
| b69ab31 | | | 204 | if (fileSelection instanceof ChunkSelectState) { |
| b69ab31 | | | 205 | const text = inverse ? fileSelection.getInverseText() : fileSelection.getSelectedText(); |
| b69ab31 | | | 206 | if (inverse || text !== fileSelection.a) { |
| b69ab31 | | | 207 | // The file is edited. Use the changed content. |
| b69ab31 | | | 208 | files[path] = {data: text, copyFrom: '.', flags: '.'}; |
| b69ab31 | | | 209 | } |
| b69ab31 | | | 210 | } else if (fileSelection === true) { |
| b69ab31 | | | 211 | // '.' can be used for both inverse = true and false. |
| b69ab31 | | | 212 | // - For inverse = true, '.' is used with the 'write' debugimportstack command. |
| b69ab31 | | | 213 | // The 'write' command treats '.' as "working parent" to "revert" changes. |
| b69ab31 | | | 214 | // - For inverse = false, '.' is used with the 'commit' or 'amend' debugimportstack |
| b69ab31 | | | 215 | // commands. They treat '.' as "working copy" to "commit/amend" changes. |
| b69ab31 | | | 216 | files[path] = '.'; |
| b69ab31 | | | 217 | } |
| b69ab31 | | | 218 | }); |
| b69ab31 | | | 219 | // Process files outside the fileMap. |
| b69ab31 | | | 220 | allPaths.forEach(path => { |
| b69ab31 | | | 221 | if (!fileMap.has(path) && this.getSimplifiedSelection(path) !== false) { |
| b69ab31 | | | 222 | files[path] = '.'; |
| b69ab31 | | | 223 | } |
| b69ab31 | | | 224 | }); |
| b69ab31 | | | 225 | return files; |
| b69ab31 | | | 226 | } |
| b69ab31 | | | 227 | |
| b69ab31 | | | 228 | /** If any file is partially selected. */ |
| b69ab31 | | | 229 | hasChunkSelection(): boolean { |
| b69ab31 | | | 230 | return this.inner.fileMap |
| b69ab31 | | | 231 | .keySeq() |
| b69ab31 | | | 232 | .some(p => typeof this.getSimplifiedSelection(p) !== 'boolean'); |
| b69ab31 | | | 233 | } |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | /** Get all paths with chunk selections (regardless of partial or not). */ |
| b69ab31 | | | 236 | chunkSelectionPaths(): Array<RepoRelativePath> { |
| b69ab31 | | | 237 | return this.inner.fileMap |
| b69ab31 | | | 238 | .filter((v, _path) => v instanceof ChunkSelectState) |
| b69ab31 | | | 239 | .keySeq() |
| b69ab31 | | | 240 | .toArray(); |
| b69ab31 | | | 241 | } |
| b69ab31 | | | 242 | } |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | /** Default: select all files. */ |
| b69ab31 | | | 245 | const defaultUncommittedPartialSelection = PartialSelection.empty({ |
| b69ab31 | | | 246 | selectByDefault: true, |
| b69ab31 | | | 247 | }); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | /** PartialSelection for `wdir()`. See `UseUncommittedSelection` for the public API. */ |
| b69ab31 | | | 250 | export const uncommittedSelection = atomResetOnCwdChange<PartialSelection>( |
| b69ab31 | | | 251 | defaultUncommittedPartialSelection, |
| b69ab31 | | | 252 | ); |
| b69ab31 | | | 253 | |
| b69ab31 | | | 254 | const wdirRev = 'wdir()'; |
| b69ab31 | | | 255 | |
| b69ab31 | | | 256 | /** PartialSelection for `wdir()` that handles loading file contents. */ |
| b69ab31 | | | 257 | export class UseUncommittedSelection { |
| b69ab31 | | | 258 | // Persist across `UseUncommittedSelection` life cycles. |
| b69ab31 | | | 259 | // Not an atom so updating the cache does not trigger re-render. |
| b69ab31 | | | 260 | static fileContentCache: { |
| b69ab31 | | | 261 | wdirHash: Hash; |
| b69ab31 | | | 262 | files: Map<RepoPath, ExportFile | null>; |
| b69ab31 | | | 263 | parentFiles: Map<RepoPath, ExportFile | null>; |
| b69ab31 | | | 264 | asyncLoadingLock: RateLimiter; |
| b69ab31 | | | 265 | epoch: number; |
| b69ab31 | | | 266 | } = { |
| b69ab31 | | | 267 | wdirHash: '', |
| b69ab31 | | | 268 | files: new Map(), |
| b69ab31 | | | 269 | parentFiles: new Map(), |
| b69ab31 | | | 270 | asyncLoadingLock: new RateLimiter(1), |
| b69ab31 | | | 271 | epoch: 0, |
| b69ab31 | | | 272 | }; |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | constructor( |
| b69ab31 | | | 275 | public selection: PartialSelection, |
| b69ab31 | | | 276 | private setSelection: (_v: PartialSelection) => void, |
| b69ab31 | | | 277 | wdirHash: Hash, |
| b69ab31 | | | 278 | private getPaths: () => Array<RepoRelativePath>, |
| b69ab31 | | | 279 | epoch: number, |
| b69ab31 | | | 280 | ) { |
| b69ab31 | | | 281 | const cache = UseUncommittedSelection.fileContentCache; |
| b69ab31 | | | 282 | if (wdirHash !== cache.wdirHash || epoch !== cache.epoch) { |
| b69ab31 | | | 283 | // Invalidate existing cache when `.` or epoch changes. |
| b69ab31 | | | 284 | cache.files.clear(); |
| b69ab31 | | | 285 | cache.parentFiles.clear(); |
| b69ab31 | | | 286 | cache.wdirHash = wdirHash; |
| b69ab31 | | | 287 | cache.epoch = epoch; |
| b69ab31 | | | 288 | } |
| b69ab31 | | | 289 | } |
| b69ab31 | | | 290 | |
| b69ab31 | | | 291 | /** Explicitly select a file. */ |
| b69ab31 | | | 292 | select(...paths: Array<RepoRelativePath>) { |
| b69ab31 | | | 293 | let newSelection = this.selection; |
| b69ab31 | | | 294 | for (const path of paths) { |
| b69ab31 | | | 295 | newSelection = newSelection.select(path); |
| b69ab31 | | | 296 | } |
| b69ab31 | | | 297 | this.setSelection(newSelection); |
| b69ab31 | | | 298 | } |
| b69ab31 | | | 299 | |
| b69ab31 | | | 300 | selectAll() { |
| b69ab31 | | | 301 | const newSelection = defaultUncommittedPartialSelection; |
| b69ab31 | | | 302 | this.setSelection(newSelection); |
| b69ab31 | | | 303 | } |
| b69ab31 | | | 304 | |
| b69ab31 | | | 305 | /** Explicitly deselect a file. Also drops the related file content cache. */ |
| b69ab31 | | | 306 | deselect(...paths: Array<RepoRelativePath>) { |
| b69ab31 | | | 307 | let newSelection = this.selection; |
| b69ab31 | | | 308 | const cache = UseUncommittedSelection.fileContentCache; |
| b69ab31 | | | 309 | for (const path of paths) { |
| b69ab31 | | | 310 | cache.files.delete(path); |
| b69ab31 | | | 311 | newSelection = newSelection.deselect(path); |
| b69ab31 | | | 312 | } |
| b69ab31 | | | 313 | this.setSelection(newSelection); |
| b69ab31 | | | 314 | } |
| b69ab31 | | | 315 | |
| b69ab31 | | | 316 | deselectAll() { |
| b69ab31 | | | 317 | let newSelection = this.selection; |
| b69ab31 | | | 318 | this.getPaths().forEach(path => (newSelection = newSelection.deselect(path))); |
| b69ab31 | | | 319 | this.setSelection(newSelection); |
| b69ab31 | | | 320 | } |
| b69ab31 | | | 321 | |
| b69ab31 | | | 322 | /** Toggle a file expansion. */ |
| b69ab31 | | | 323 | toggleExpand(path: RepoRelativePath, select?: boolean) { |
| b69ab31 | | | 324 | this.setSelection(this.selection.toggleExpand(path, select)); |
| b69ab31 | | | 325 | } |
| b69ab31 | | | 326 | |
| b69ab31 | | | 327 | /** Test if a path is marked as expanded. */ |
| b69ab31 | | | 328 | isExpanded(path: RepoRelativePath): boolean { |
| b69ab31 | | | 329 | return this.selection.isExpanded(path); |
| b69ab31 | | | 330 | } |
| b69ab31 | | | 331 | |
| b69ab31 | | | 332 | /** Drop "chunk selection" states. Useful to clear states after an wdir-changing operation. */ |
| b69ab31 | | | 333 | discardPartialSelections() { |
| b69ab31 | | | 334 | return this.setSelection(this.selection.discardPartialSelections()); |
| b69ab31 | | | 335 | } |
| b69ab31 | | | 336 | |
| b69ab31 | | | 337 | /** Restore to the default selection (select all). */ |
| b69ab31 | | | 338 | clear() { |
| b69ab31 | | | 339 | const newSelection = this.selection.clear(); |
| b69ab31 | | | 340 | this.setSelection(newSelection); |
| b69ab31 | | | 341 | } |
| b69ab31 | | | 342 | |
| b69ab31 | | | 343 | /** |
| b69ab31 | | | 344 | * Get the chunk select state for the given path. |
| b69ab31 | | | 345 | * The file content will be loaded on demand. |
| b69ab31 | | | 346 | * |
| b69ab31 | | | 347 | * `epoch` is used to invalidate existing caches. |
| b69ab31 | | | 348 | */ |
| b69ab31 | | | 349 | getChunkSelect(path: RepoRelativePath): ChunkSelectState | Promise<ChunkSelectState> { |
| b69ab31 | | | 350 | const fileSelection = this.selection.inner.fileMap.get(path); |
| b69ab31 | | | 351 | const cache = UseUncommittedSelection.fileContentCache; |
| b69ab31 | | | 352 | |
| b69ab31 | | | 353 | let maybeStaleResult = undefined; |
| b69ab31 | | | 354 | if (fileSelection instanceof ChunkSelectState) { |
| b69ab31 | | | 355 | maybeStaleResult = fileSelection; |
| b69ab31 | | | 356 | if (cache.files.has(path)) { |
| b69ab31 | | | 357 | // Up to date. |
| b69ab31 | | | 358 | return maybeStaleResult; |
| b69ab31 | | | 359 | } else { |
| b69ab31 | | | 360 | // Cache invalidated by constructor. |
| b69ab31 | | | 361 | // Trigger a new fetch below. |
| b69ab31 | | | 362 | // Still return `maybeStaleResult` to avoid flakiness. |
| b69ab31 | | | 363 | } |
| b69ab31 | | | 364 | } |
| b69ab31 | | | 365 | |
| b69ab31 | | | 366 | const maybeReadFromCache = (): ChunkSelectState | null => { |
| b69ab31 | | | 367 | const file = cache.files.get(path); |
| b69ab31 | | | 368 | if (file === undefined) { |
| b69ab31 | | | 369 | return null; |
| b69ab31 | | | 370 | } |
| b69ab31 | | | 371 | const parentPath = file?.copyFrom ?? path; |
| b69ab31 | | | 372 | const parentFile = cache.parentFiles.get(parentPath); |
| b69ab31 | | | 373 | if (parentFile?.dataBase85 || file?.dataBase85) { |
| b69ab31 | | | 374 | throw new Error(t('Cannot edit non-utf8 file')); |
| b69ab31 | | | 375 | } |
| b69ab31 | | | 376 | const a = parentFile?.data ?? ''; |
| b69ab31 | | | 377 | const b = file?.data ?? ''; |
| b69ab31 | | | 378 | const existing = this.getSelection(path); |
| b69ab31 | | | 379 | let selected: string | boolean; |
| b69ab31 | | | 380 | if (existing instanceof ChunkSelectState) { |
| b69ab31 | | | 381 | if (existing.a === a && existing.b === b) { |
| b69ab31 | | | 382 | return existing; |
| b69ab31 | | | 383 | } |
| b69ab31 | | | 384 | selected = existing.getSelectedText(); |
| b69ab31 | | | 385 | } else { |
| b69ab31 | | | 386 | selected = existing; |
| b69ab31 | | | 387 | } |
| b69ab31 | | | 388 | const newSelection = this.selection.startChunkSelect(path, a, b, selected, true); |
| b69ab31 | | | 389 | this.setSelection(newSelection); |
| b69ab31 | | | 390 | const newSelected = newSelection.getSelection(path); |
| b69ab31 | | | 391 | assert( |
| b69ab31 | | | 392 | newSelected instanceof ChunkSelectState, |
| b69ab31 | | | 393 | 'startChunkSelect() should provide ChunkSelectState', |
| b69ab31 | | | 394 | ); |
| b69ab31 | | | 395 | return newSelected; |
| b69ab31 | | | 396 | }; |
| b69ab31 | | | 397 | |
| b69ab31 | | | 398 | const promise = cache.asyncLoadingLock.enqueueRun(async () => { |
| b69ab31 | | | 399 | const chunkState = maybeReadFromCache(); |
| b69ab31 | | | 400 | if (chunkState !== null) { |
| b69ab31 | | | 401 | return chunkState; |
| b69ab31 | | | 402 | } |
| b69ab31 | | | 403 | |
| b69ab31 | | | 404 | // Not found in cache. Need to (re)load the file via the server. |
| b69ab31 | | | 405 | |
| b69ab31 | | | 406 | const revs = wdirRev; |
| b69ab31 | | | 407 | // Setup event listener before sending the request. |
| b69ab31 | | | 408 | const iter = clientToServerAPI.iterateMessageOfType('exportedStack'); |
| b69ab31 | | | 409 | // Explicitly ask for the file via assumeTracked. Note this also provides contents |
| b69ab31 | | | 410 | // of other tracked files. |
| b69ab31 | | | 411 | clientToServerAPI.postMessage({type: 'exportStack', revs, assumeTracked: [path]}); |
| b69ab31 | | | 412 | for await (const event of iter) { |
| b69ab31 | | | 413 | if (event.revs !== revs) { |
| b69ab31 | | | 414 | // Ignore unrelated response. |
| b69ab31 | | | 415 | continue; |
| b69ab31 | | | 416 | } |
| b69ab31 | | | 417 | if (event.error) { |
| b69ab31 | | | 418 | throw new Error(event.error); |
| b69ab31 | | | 419 | } |
| b69ab31 | | | 420 | if (event.stack.some(c => !c.requested && c.node !== cache.wdirHash)) { |
| b69ab31 | | | 421 | // The wdirHash has changed. Fail the load. |
| b69ab31 | | | 422 | // The exported stack usually has a non-requested commit that is the parent of |
| b69ab31 | | | 423 | // the requested "wdir()", which is the "." commit that should match `wdirHash`. |
| b69ab31 | | | 424 | // Note: for an empty repo there is no such non-requested commit exported so |
| b69ab31 | | | 425 | // we skip the check in that case. |
| b69ab31 | | | 426 | throw new Error(t('Working copy has changed')); |
| b69ab31 | | | 427 | } |
| b69ab31 | | | 428 | |
| b69ab31 | | | 429 | // Update cache. |
| b69ab31 | | | 430 | event.stack.forEach(commit => { |
| b69ab31 | | | 431 | if (commit.requested) { |
| b69ab31 | | | 432 | mergeObjectToMap(commit.files, cache.files); |
| b69ab31 | | | 433 | } else { |
| b69ab31 | | | 434 | mergeObjectToMap(commit.relevantFiles, cache.parentFiles); |
| b69ab31 | | | 435 | } |
| b69ab31 | | | 436 | }); |
| b69ab31 | | | 437 | |
| b69ab31 | | | 438 | // Try read from cache again. |
| b69ab31 | | | 439 | const chunkState = maybeReadFromCache(); |
| b69ab31 | | | 440 | if (chunkState === null) { |
| b69ab31 | | | 441 | if (event.assumeTracked.includes(path)) { |
| b69ab31 | | | 442 | // We explicitly requested the file, but the server does not provide |
| b69ab31 | | | 443 | // it somehow. |
| b69ab31 | | | 444 | break; |
| b69ab31 | | | 445 | } else { |
| b69ab31 | | | 446 | // It's possible that there are multiple export requests. |
| b69ab31 | | | 447 | // This one does not provide the file we want, continue checking other responses. |
| b69ab31 | | | 448 | continue; |
| b69ab31 | | | 449 | } |
| b69ab31 | | | 450 | } else { |
| b69ab31 | | | 451 | return chunkState; |
| b69ab31 | | | 452 | } |
| b69ab31 | | | 453 | } |
| b69ab31 | | | 454 | |
| b69ab31 | | | 455 | // Handles the `break` above. Tells tsc that we don't return undefined. |
| b69ab31 | | | 456 | throw new Error(t('Unable to get file content unexpectedly')); |
| b69ab31 | | | 457 | }); |
| b69ab31 | | | 458 | |
| b69ab31 | | | 459 | return maybeStaleResult ?? promise; |
| b69ab31 | | | 460 | } |
| b69ab31 | | | 461 | |
| b69ab31 | | | 462 | /** Edit chunk selection for a file. */ |
| b69ab31 | | | 463 | editChunkSelect( |
| b69ab31 | | | 464 | path: RepoRelativePath, |
| b69ab31 | | | 465 | newValue: ((chunkState: ChunkSelectState) => ChunkSelectState) | ChunkSelectState, |
| b69ab31 | | | 466 | ) { |
| b69ab31 | | | 467 | const newSelection = this.selection.editChunkSelect(path, newValue); |
| b69ab31 | | | 468 | this.setSelection(newSelection); |
| b69ab31 | | | 469 | } |
| b69ab31 | | | 470 | |
| b69ab31 | | | 471 | // ---------- Read-only methods below ---------- |
| b69ab31 | | | 472 | |
| b69ab31 | | | 473 | /** |
| b69ab31 | | | 474 | * Return true if a file is selected (default), false if deselected, |
| b69ab31 | | | 475 | * or a string with the edited content. |
| b69ab31 | | | 476 | */ |
| b69ab31 | | | 477 | getSelection(path: RepoRelativePath): SingleFileSelection { |
| b69ab31 | | | 478 | return this.selection.getSelection(path); |
| b69ab31 | | | 479 | } |
| b69ab31 | | | 480 | |
| b69ab31 | | | 481 | isFullyOrPartiallySelected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 482 | return this.selection.isFullyOrPartiallySelected(path); |
| b69ab31 | | | 483 | } |
| b69ab31 | | | 484 | |
| b69ab31 | | | 485 | isPartiallySelected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 486 | return this.selection.isPartiallySelected(path); |
| b69ab31 | | | 487 | } |
| b69ab31 | | | 488 | |
| b69ab31 | | | 489 | isFullySelected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 490 | return this.selection.isFullySelected(path); |
| b69ab31 | | | 491 | } |
| b69ab31 | | | 492 | |
| b69ab31 | | | 493 | isDeselected(path: RepoRelativePath): boolean { |
| b69ab31 | | | 494 | return this.selection.isDeselected(path); |
| b69ab31 | | | 495 | } |
| b69ab31 | | | 496 | |
| b69ab31 | | | 497 | isEverythingSelected(): boolean { |
| b69ab31 | | | 498 | return this.selection.isEverythingSelected(this.getPaths); |
| b69ab31 | | | 499 | } |
| b69ab31 | | | 500 | |
| b69ab31 | | | 501 | isNothingSelected(): boolean { |
| b69ab31 | | | 502 | return this.selection.isNothingSelected(this.getPaths); |
| b69ab31 | | | 503 | } |
| b69ab31 | | | 504 | |
| b69ab31 | | | 505 | hasChunkSelection(): boolean { |
| b69ab31 | | | 506 | return this.selection.hasChunkSelection(); |
| b69ab31 | | | 507 | } |
| b69ab31 | | | 508 | } |
| b69ab31 | | | 509 | |
| b69ab31 | | | 510 | export const isFullyOrPartiallySelected = atom(get => { |
| b69ab31 | | | 511 | const sel = get(uncommittedSelection); |
| b69ab31 | | | 512 | const uncommittedChanges = get(uncommittedChangesWithPreviews); |
| b69ab31 | | | 513 | const epoch = get(latestUncommittedChangesTimestamp); |
| b69ab31 | | | 514 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 515 | const wdirHash = dag.resolve('.')?.hash ?? ''; |
| b69ab31 | | | 516 | const getPaths = () => uncommittedChanges.map(c => c.path); |
| b69ab31 | | | 517 | const emptyFunction = () => {}; |
| b69ab31 | | | 518 | // NOTE: Usually UseUncommittedSelection is only used in React, but this is |
| b69ab31 | | | 519 | // a private shortcut path to get the logic outside React. |
| b69ab31 | | | 520 | const selection = new UseUncommittedSelection(sel, emptyFunction, wdirHash, getPaths, epoch); |
| b69ab31 | | | 521 | return selection.isFullyOrPartiallySelected.bind(selection); |
| b69ab31 | | | 522 | }); |
| b69ab31 | | | 523 | |
| b69ab31 | | | 524 | /** react hook to get the uncommitted selection state. */ |
| b69ab31 | | | 525 | export function useUncommittedSelection() { |
| b69ab31 | | | 526 | const [selection, setSelection] = useAtom(uncommittedSelection); |
| b69ab31 | | | 527 | const uncommittedChanges = useAtomValue(uncommittedChangesWithPreviews); |
| b69ab31 | | | 528 | const epoch = useAtomValue(latestUncommittedChangesTimestamp); |
| b69ab31 | | | 529 | const dag = useAtomValue(dagWithPreviews); |
| b69ab31 | | | 530 | const wdirHash = dag.resolve('.')?.hash ?? ''; |
| b69ab31 | | | 531 | const getPaths = () => uncommittedChanges.map(c => c.path); |
| b69ab31 | | | 532 | |
| b69ab31 | | | 533 | return new UseUncommittedSelection(selection, setSelection, wdirHash, getPaths, epoch); |
| b69ab31 | | | 534 | } |
| b69ab31 | | | 535 | |
| b69ab31 | | | 536 | function mergeObjectToMap<V>(obj: {[path: string]: V} | undefined, map: Map<string, V>) { |
| b69ab31 | | | 537 | if (obj === undefined) { |
| b69ab31 | | | 538 | return; |
| b69ab31 | | | 539 | } |
| b69ab31 | | | 540 | for (const k in obj) { |
| b69ab31 | | | 541 | const v = obj[k]; |
| b69ab31 | | | 542 | map.set(k, v); |
| b69ab31 | | | 543 | } |
| b69ab31 | | | 544 | } |