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