| 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 {Dag} from '../previews'; |
| 9 | import type {ExactRevset, OptimisticRevset, SucceedableRevset} from '../types'; |
| 10 | |
| 11 | import {CommitPreview} from '../previews'; |
| 12 | import {Operation} from './Operation'; |
| 13 | |
| 14 | export class HideOperation extends Operation { |
| 15 | constructor(private source: SucceedableRevset | ExactRevset | OptimisticRevset) { |
| 16 | super('HideOperation'); |
| 17 | } |
| 18 | |
| 19 | static opName = 'Hide'; |
| 20 | |
| 21 | getArgs() { |
| 22 | return ['hide', '--rev', this.source]; |
| 23 | } |
| 24 | |
| 25 | private hash() { |
| 26 | return this.source.type === 'optimistic-revset' ? this.source.fake : this.source.revset; |
| 27 | } |
| 28 | |
| 29 | previewDag(dag: Dag): Dag { |
| 30 | const hash = this.hash(); |
| 31 | const toHide = dag.descendants(hash); |
| 32 | return dag.replaceWith(toHide, (h, c) => { |
| 33 | const previewType = h === hash ? CommitPreview.HIDDEN_ROOT : CommitPreview.HIDDEN_DESCENDANT; |
| 34 | return c?.merge({previewType}); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | optimisticDag(dag: Dag): Dag { |
| 39 | const hash = this.hash(); |
| 40 | const toHide = dag.descendants(hash); |
| 41 | const toCleanup = dag.parents(hash); |
| 42 | // If the head is being hidden, we need to move the head to the parent. |
| 43 | const newHead = []; |
| 44 | if (toHide.toHashes().some(h => dag.get(h)?.isDot == true)) { |
| 45 | const parent = dag.get(hash)?.parents?.at(0); |
| 46 | if (parent && dag.has(parent)) { |
| 47 | newHead.push(parent); |
| 48 | } |
| 49 | } |
| 50 | return dag |
| 51 | .remove(toHide) |
| 52 | .replaceWith(newHead, (_h, c) => { |
| 53 | return c?.merge({isDot: true, previewType: CommitPreview.GOTO_DESTINATION}); |
| 54 | }) |
| 55 | .cleanup(toCleanup); |
| 56 | } |
| 57 | } |
| 58 | |