|
| 1 | +import type { Engine } from "../engine"; |
| 2 | +import { |
| 3 | + edit, |
| 4 | + list, |
| 5 | + resolveChange, |
| 6 | + runJJWithMutableConfigVoid, |
| 7 | + status, |
| 8 | +} from "../jj"; |
| 9 | +import type { Changeset } from "../parser"; |
| 10 | +import { ok, type Result } from "../result"; |
| 11 | +import type { Command } from "./types"; |
| 12 | + |
| 13 | +interface DeleteResult { |
| 14 | + movedTo: string | null; |
| 15 | + untrackedBookmarks: string[]; |
| 16 | + /** The change that was deleted (for CLI display) */ |
| 17 | + change: Changeset; |
| 18 | +} |
| 19 | + |
| 20 | +interface DeleteOptions { |
| 21 | + /** Change ID, bookmark name, or search query (required) */ |
| 22 | + id: string; |
| 23 | + engine: Engine; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Delete a change, discarding its work. |
| 28 | + * If the change has children, they are rebased onto the parent. |
| 29 | + * If deleting the current change, moves to parent. |
| 30 | + * Untracks any bookmarks on the deleted change from the engine. |
| 31 | + */ |
| 32 | +export async function deleteChange( |
| 33 | + options: DeleteOptions, |
| 34 | +): Promise<Result<DeleteResult>> { |
| 35 | + const { id, engine } = options; |
| 36 | + |
| 37 | + const statusBefore = await status(); |
| 38 | + if (!statusBefore.ok) return statusBefore; |
| 39 | + |
| 40 | + // Resolve the change |
| 41 | + const changeResult = await resolveChange(id, { includeBookmarks: true }); |
| 42 | + if (!changeResult.ok) return changeResult; |
| 43 | + const change = changeResult.value; |
| 44 | + |
| 45 | + const wasOnChange = |
| 46 | + statusBefore.value.workingCopy.changeId === change.changeId; |
| 47 | + const parentId = change.parents[0]; |
| 48 | + |
| 49 | + const childrenResult = await list({ |
| 50 | + revset: `children(${change.changeId})`, |
| 51 | + }); |
| 52 | + const hasChildren = childrenResult.ok && childrenResult.value.length > 0; |
| 53 | + |
| 54 | + // Use mutable config for operations on potentially pushed commits |
| 55 | + if (hasChildren) { |
| 56 | + const rebaseResult = await runJJWithMutableConfigVoid([ |
| 57 | + "rebase", |
| 58 | + "-s", |
| 59 | + `children(${change.changeId})`, |
| 60 | + "-d", |
| 61 | + parentId || "trunk()", |
| 62 | + ]); |
| 63 | + if (!rebaseResult.ok) return rebaseResult; |
| 64 | + } |
| 65 | + |
| 66 | + // Discard work by restoring |
| 67 | + const restoreResult = await runJJWithMutableConfigVoid([ |
| 68 | + "restore", |
| 69 | + "--changes-in", |
| 70 | + change.changeId, |
| 71 | + ]); |
| 72 | + if (!restoreResult.ok) return restoreResult; |
| 73 | + |
| 74 | + const abandonResult = await runJJWithMutableConfigVoid([ |
| 75 | + "abandon", |
| 76 | + change.changeId, |
| 77 | + ]); |
| 78 | + if (!abandonResult.ok) return abandonResult; |
| 79 | + |
| 80 | + // Untrack any bookmarks on the deleted change |
| 81 | + const untrackedBookmarks: string[] = []; |
| 82 | + for (const bookmark of change.bookmarks) { |
| 83 | + if (engine.isTracked(bookmark)) { |
| 84 | + engine.untrack(bookmark); |
| 85 | + untrackedBookmarks.push(bookmark); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + let movedTo: string | null = null; |
| 90 | + if (wasOnChange && parentId) { |
| 91 | + const editResult = await edit(parentId); |
| 92 | + if (editResult.ok) { |
| 93 | + movedTo = parentId; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return ok({ movedTo, untrackedBookmarks, change }); |
| 98 | +} |
| 99 | + |
| 100 | +export const deleteCommand: Command<DeleteResult, [DeleteOptions]> = { |
| 101 | + meta: { |
| 102 | + name: "delete", |
| 103 | + args: "<id>", |
| 104 | + description: |
| 105 | + "Delete a change, discarding its work. Children restack onto parent.", |
| 106 | + aliases: ["dl"], |
| 107 | + category: "management", |
| 108 | + }, |
| 109 | + run: deleteChange, |
| 110 | +}; |
0 commit comments