addons/isl/src/operationUtils.tsxblame
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 {CommitInfo} from './types';
b69ab319
b69ab3110import {readAtom} from './jotaiUtils';
b69ab3111import {AmendToOperation} from './operations/AmendToOperation';
b69ab3112import {uncommittedSelection} from './partialSelection';
b69ab3113import {dagWithPreviews, uncommittedChangesWithPreviews} from './previews';
b69ab3114import {latestSuccessorUnlessExplicitlyObsolete} from './successionUtils';
b69ab3115
b69ab3116/**
b69ab3117 * Amend --to allows amending to a parent commit other than head.
b69ab3118 * Only allowed on a commit that is a parent of head, and when
b69ab3119 * your current selection is not a partial selection.
b69ab3120 */
b69ab3121export function isAmendToAllowedForCommit(commit: CommitInfo): boolean {
b69ab3122 if (commit.isDot || commit.phase === 'public' || commit.successorInfo != null) {
b69ab3123 // no point, just amend normally
b69ab3124 return false;
b69ab3125 }
b69ab3126
b69ab3127 const uncommittedChanges = readAtom(uncommittedChangesWithPreviews);
b69ab3128 if (uncommittedChanges == null || uncommittedChanges.length === 0) {
b69ab3129 // nothing to amend
b69ab3130 return false;
b69ab3131 }
b69ab3132
b69ab3133 // amend --to doesn't handle partial chunk selections, only entire files
b69ab3134 const selection = readAtom(uncommittedSelection);
b69ab3135 const hasPartialSelection = selection.hasChunkSelection();
b69ab3136
b69ab3137 if (hasPartialSelection) {
b69ab3138 return false;
b69ab3139 }
b69ab3140
b69ab3141 const dag = readAtom(dagWithPreviews);
b69ab3142 const head = dag?.resolve('.');
b69ab3143 if (dag == null || head == null || !dag.has(commit.hash)) {
b69ab3144 return false;
b69ab3145 }
b69ab3146
b69ab3147 return dag.isAncestor(commit.hash, head.hash);
b69ab3148}
b69ab3149
b69ab3150export function getAmendToOperation(commit: CommitInfo): AmendToOperation {
b69ab3151 const selection = readAtom(uncommittedSelection);
b69ab3152 const uncommittedChanges = readAtom(uncommittedChangesWithPreviews);
b69ab3153
b69ab3154 const paths = uncommittedChanges
b69ab3155 .filter(change => selection.isFullySelected(change.path))
b69ab3156 .map(change => change.path);
b69ab3157 return new AmendToOperation(latestSuccessorUnlessExplicitlyObsolete(commit), paths);
b69ab3158}