| 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 {CommitInfo} from './types'; |
| 9 | |
| 10 | import {readAtom} from './jotaiUtils'; |
| 11 | import {AmendToOperation} from './operations/AmendToOperation'; |
| 12 | import {uncommittedSelection} from './partialSelection'; |
| 13 | import {dagWithPreviews, uncommittedChangesWithPreviews} from './previews'; |
| 14 | import {latestSuccessorUnlessExplicitlyObsolete} from './successionUtils'; |
| 15 | |
| 16 | /** |
| 17 | * Amend --to allows amending to a parent commit other than head. |
| 18 | * Only allowed on a commit that is a parent of head, and when |
| 19 | * your current selection is not a partial selection. |
| 20 | */ |
| 21 | export function isAmendToAllowedForCommit(commit: CommitInfo): boolean { |
| 22 | if (commit.isDot || commit.phase === 'public' || commit.successorInfo != null) { |
| 23 | // no point, just amend normally |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | const uncommittedChanges = readAtom(uncommittedChangesWithPreviews); |
| 28 | if (uncommittedChanges == null || uncommittedChanges.length === 0) { |
| 29 | // nothing to amend |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | // amend --to doesn't handle partial chunk selections, only entire files |
| 34 | const selection = readAtom(uncommittedSelection); |
| 35 | const hasPartialSelection = selection.hasChunkSelection(); |
| 36 | |
| 37 | if (hasPartialSelection) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | const dag = readAtom(dagWithPreviews); |
| 42 | const head = dag?.resolve('.'); |
| 43 | if (dag == null || head == null || !dag.has(commit.hash)) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return dag.isAncestor(commit.hash, head.hash); |
| 48 | } |
| 49 | |
| 50 | export function getAmendToOperation(commit: CommitInfo): AmendToOperation { |
| 51 | const selection = readAtom(uncommittedSelection); |
| 52 | const uncommittedChanges = readAtom(uncommittedChangesWithPreviews); |
| 53 | |
| 54 | const paths = uncommittedChanges |
| 55 | .filter(change => selection.isFullySelected(change.path)) |
| 56 | .map(change => change.path); |
| 57 | return new AmendToOperation(latestSuccessorUnlessExplicitlyObsolete(commit), paths); |
| 58 | } |
| 59 | |