addons/isl/src/operations/HideOperation.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {Dag} from '../previews';
b69ab319import type {ExactRevset, OptimisticRevset, SucceedableRevset} from '../types';
b69ab3110
b69ab3111import {CommitPreview} from '../previews';
b69ab3112import {Operation} from './Operation';
b69ab3113
b69ab3114export class HideOperation extends Operation {
b69ab3115 constructor(private source: SucceedableRevset | ExactRevset | OptimisticRevset) {
b69ab3116 super('HideOperation');
b69ab3117 }
b69ab3118
b69ab3119 static opName = 'Hide';
b69ab3120
b69ab3121 getArgs() {
b69ab3122 return ['hide', '--rev', this.source];
b69ab3123 }
b69ab3124
b69ab3125 private hash() {
b69ab3126 return this.source.type === 'optimistic-revset' ? this.source.fake : this.source.revset;
b69ab3127 }
b69ab3128
b69ab3129 previewDag(dag: Dag): Dag {
b69ab3130 const hash = this.hash();
b69ab3131 const toHide = dag.descendants(hash);
b69ab3132 return dag.replaceWith(toHide, (h, c) => {
b69ab3133 const previewType = h === hash ? CommitPreview.HIDDEN_ROOT : CommitPreview.HIDDEN_DESCENDANT;
b69ab3134 return c?.merge({previewType});
b69ab3135 });
b69ab3136 }
b69ab3137
b69ab3138 optimisticDag(dag: Dag): Dag {
b69ab3139 const hash = this.hash();
b69ab3140 const toHide = dag.descendants(hash);
b69ab3141 const toCleanup = dag.parents(hash);
b69ab3142 // If the head is being hidden, we need to move the head to the parent.
b69ab3143 const newHead = [];
b69ab3144 if (toHide.toHashes().some(h => dag.get(h)?.isDot == true)) {
b69ab3145 const parent = dag.get(hash)?.parents?.at(0);
b69ab3146 if (parent && dag.has(parent)) {
b69ab3147 newHead.push(parent);
b69ab3148 }
b69ab3149 }
b69ab3150 return dag
b69ab3151 .remove(toHide)
b69ab3152 .replaceWith(newHead, (_h, c) => {
b69ab3153 return c?.merge({isDot: true, previewType: CommitPreview.GOTO_DESTINATION});
b69ab3154 })
b69ab3155 .cleanup(toCleanup);
b69ab3156 }
b69ab3157}