|
| 1 | +<script lang="ts" module> |
| 2 | + export type RebaseStackModalProps = { |
| 3 | + projectId: string; |
| 4 | + stackId: string | undefined; |
| 5 | + }; |
| 6 | +</script> |
| 7 | + |
| 8 | +<script lang="ts"> |
| 9 | + import { parseQueryError } from "$lib/error/error"; |
| 10 | + import { STACK_SERVICE } from "$lib/stacks/stackService.svelte"; |
| 11 | + import { inject } from "@gitbutler/core/context"; |
| 12 | + import { Button, Modal } from "@gitbutler/ui"; |
| 13 | + import { tick } from "svelte"; |
| 14 | + import type { BottomUpdate, WorkspaceState } from "@gitbutler/but-sdk"; |
| 15 | +
|
| 16 | + const { projectId, stackId }: RebaseStackModalProps = $props(); |
| 17 | +
|
| 18 | + const stackService = inject(STACK_SERVICE); |
| 19 | + const [integrateUpstream, integrateOp] = stackService.workspaceIntegrateUpstream; |
| 20 | +
|
| 21 | + let modal = $state<Modal>(); |
| 22 | + let dryRunResult = $state<WorkspaceState | undefined>(); |
| 23 | + let dryRunError = $state<string | undefined>(); |
| 24 | + let bottomUpdates = $state<BottomUpdate[]>([]); |
| 25 | +
|
| 26 | + export async function show() { |
| 27 | + dryRunResult = undefined; |
| 28 | + dryRunError = undefined; |
| 29 | + bottomUpdates = []; |
| 30 | +
|
| 31 | + try { |
| 32 | + const branches = await stackService.fetchBranches(projectId, stackId!); |
| 33 | + if (!branches || branches.length === 0) { |
| 34 | + dryRunError = "No branches found in this stack."; |
| 35 | + await tick(); |
| 36 | + modal?.show(); |
| 37 | + return; |
| 38 | + } |
| 39 | +
|
| 40 | + // The bottom branch is the last in the array (ordered top to bottom). |
| 41 | + // Its bottom-most commit is the last in its commits array. |
| 42 | + const bottomBranch = branches[branches.length - 1]!; |
| 43 | + const bottomCommit = bottomBranch.commits[bottomBranch.commits.length - 1]; |
| 44 | +
|
| 45 | + if (bottomCommit) { |
| 46 | + bottomUpdates = [ |
| 47 | + { |
| 48 | + kind: "rebase", |
| 49 | + selector: { type: "commit", subject: bottomCommit.id }, |
| 50 | + }, |
| 51 | + ]; |
| 52 | + } else { |
| 53 | + // Empty branch — use the branch reference as selector |
| 54 | + bottomUpdates = [ |
| 55 | + { |
| 56 | + kind: "rebase", |
| 57 | + selector: { |
| 58 | + type: "reference", |
| 59 | + subject: bottomBranch.reference, |
| 60 | + }, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + } |
| 64 | +
|
| 65 | + const result = await integrateUpstream({ |
| 66 | + projectId, |
| 67 | + updates: bottomUpdates, |
| 68 | + dryRun: true, |
| 69 | + }); |
| 70 | +
|
| 71 | + dryRunResult = result; |
| 72 | + } catch (err: unknown) { |
| 73 | + dryRunError = parseQueryError(err).message; |
| 74 | + } |
| 75 | +
|
| 76 | + await tick(); |
| 77 | + modal?.show(); |
| 78 | + } |
| 79 | +
|
| 80 | + const replacedCount = $derived( |
| 81 | + dryRunResult ? Object.keys(dryRunResult.replacedCommits).length : 0, |
| 82 | + ); |
| 83 | +</script> |
| 84 | + |
| 85 | +<Modal |
| 86 | + bind:this={modal} |
| 87 | + width="small" |
| 88 | + title="Rebase stack" |
| 89 | + onSubmit={async (close) => { |
| 90 | + await integrateUpstream({ |
| 91 | + projectId, |
| 92 | + updates: bottomUpdates, |
| 93 | + dryRun: false, |
| 94 | + }); |
| 95 | + close(); |
| 96 | + }} |
| 97 | +> |
| 98 | + {#if dryRunError} |
| 99 | + <p class="text-13 text-body">Failed to preview rebase:</p> |
| 100 | + <p class="text-13 text-body text-error" style="user-select: text">{dryRunError}</p> |
| 101 | + {:else if dryRunResult} |
| 102 | + <p class="text-13 text-body">This will rebase your stack onto the latest target branch.</p> |
| 103 | + {#if replacedCount > 0} |
| 104 | + <p class="text-13 text-body details"> |
| 105 | + {replacedCount} |
| 106 | + {replacedCount === 1 ? "commit" : "commits"} will be rewritten. |
| 107 | + </p> |
| 108 | + {:else} |
| 109 | + <p class="text-13 text-body details"> |
| 110 | + Your stack is already up to date. No commits will be rewritten. |
| 111 | + </p> |
| 112 | + {/if} |
| 113 | + {:else} |
| 114 | + <p class="text-13 text-body">Loading preview...</p> |
| 115 | + {/if} |
| 116 | + |
| 117 | + {#snippet controls(close)} |
| 118 | + <Button kind="outline" onclick={close}>Cancel</Button> |
| 119 | + <Button |
| 120 | + style="pop" |
| 121 | + type="submit" |
| 122 | + disabled={!!dryRunError || !dryRunResult} |
| 123 | + loading={integrateOp.current.isLoading} |
| 124 | + > |
| 125 | + Rebase |
| 126 | + </Button> |
| 127 | + {/snippet} |
| 128 | +</Modal> |
| 129 | + |
| 130 | +<style lang="postcss"> |
| 131 | + .details { |
| 132 | + margin-top: 8px; |
| 133 | + color: var(--text-2); |
| 134 | + } |
| 135 | +
|
| 136 | + .text-error { |
| 137 | + color: var(--text-error); |
| 138 | + word-break: break-word; |
| 139 | + } |
| 140 | +</style> |
0 commit comments