|
1 | 1 | <script lang="ts"> |
| 2 | + import { goto } from "$app/navigation"; |
2 | 3 | import ReduxResult from "$components/ReduxResult.svelte"; |
| 4 | + import { showError, showToast } from "$lib/notifications/toasts"; |
| 5 | + import { branchesPath } from "$lib/routes/routes.svelte"; |
| 6 | + import { handleCreateBranchFromBranchOutcome } from "$lib/stacks/stack"; |
3 | 7 | import { DEPENDENCY_SERVICE } from "$lib/dependencies/dependencyService.svelte"; |
4 | 8 | import { SETTINGS } from "$lib/settings/userSettings"; |
5 | 9 | import { type RejectionReason } from "$lib/stacks/stackService.svelte"; |
6 | 10 | import { STACK_SERVICE } from "$lib/stacks/stackService.svelte"; |
7 | 11 | import { inject } from "@gitbutler/core/context"; |
8 | | - import { FileName, HunkDiff, Icon, Tooltip } from "@gitbutler/ui"; |
| 12 | + import { AsyncButton, Button, FileName, HunkDiff, Icon, Tooltip } from "@gitbutler/ui"; |
9 | 13 |
|
10 | 14 | type Props = { |
11 | 15 | path: string; |
|
20 | 24 | const dependencyService = inject(DEPENDENCY_SERVICE); |
21 | 25 |
|
22 | 26 | let isFolded = $state(true); |
| 27 | + let applyingStackId = $state<string | null>(null); |
| 28 | + let locatingCommitId = $state<string | null>(null); |
| 29 | + let ignoredLocks = $state<Record<string, true>>({}); |
23 | 30 |
|
24 | 31 | function reasonRelatedToDependencyInfo(reason: RejectionReason): boolean { |
25 | | - return reason === "cherryPickMergeConflict" || reason === "workspaceMergeConflict"; |
| 32 | + return ( |
| 33 | + reason === "cherryPickMergeConflict" || |
| 34 | + reason === "workspaceMergeConflict" || |
| 35 | + reason === "workspaceMergeConflictOfUnrelatedFile" |
| 36 | + ); |
| 37 | + } |
| 38 | +
|
| 39 | + function lockKey(lock: { |
| 40 | + commitId: string; |
| 41 | + target: { type: string; subject?: string }; |
| 42 | + }): string { |
| 43 | + return `${lock.commitId}:${lock.target.type}:${lock.target.subject ?? ""}`; |
| 44 | + } |
| 45 | +
|
| 46 | + function ignoreLock(lock: { |
| 47 | + commitId: string; |
| 48 | + target: { type: string; subject?: string }; |
| 49 | + }) { |
| 50 | + ignoredLocks = { |
| 51 | + ...ignoredLocks, |
| 52 | + [lockKey(lock)]: true, |
| 53 | + }; |
| 54 | + } |
| 55 | +
|
| 56 | + function isIgnoredLock(lock: { |
| 57 | + commitId: string; |
| 58 | + target: { type: string; subject?: string }; |
| 59 | + }): boolean { |
| 60 | + return ignoredLocks[lockKey(lock)] === true; |
| 61 | + } |
| 62 | +
|
| 63 | + async function applyKnownButUnappliedStack(stackId: string, branchName: string) { |
| 64 | + applyingStackId = stackId; |
| 65 | + try { |
| 66 | + const outcome = await stackService.createVirtualBranchFromBranch({ |
| 67 | + projectId, |
| 68 | + branch: `refs/heads/${branchName}`, |
| 69 | + }); |
| 70 | + handleCreateBranchFromBranchOutcome(outcome); |
| 71 | + showToast({ |
| 72 | + title: "Stack applied to workspace", |
| 73 | + message: `Applied stack '${branchName}'. Retry the commit after conflicts are resolved.`, |
| 74 | + style: "info", |
| 75 | + }); |
| 76 | + } catch (error) { |
| 77 | + showError("Failed to apply stack", error); |
| 78 | + } finally { |
| 79 | + applyingStackId = null; |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | + async function recoverStackByCommit(commitId: string) { |
| 84 | + locatingCommitId = commitId; |
| 85 | + try { |
| 86 | + const allStacks = await stackService.fetchAllStacks(projectId); |
| 87 | + if (!allStacks || allStacks.length === 0) { |
| 88 | + showToast({ |
| 89 | + title: "No stacks available to recover", |
| 90 | + message: "No stacks were found. Open Branches to create/apply a stack and retry.", |
| 91 | + style: "warning", |
| 92 | + }); |
| 93 | + return; |
| 94 | + } |
| 95 | +
|
| 96 | + for (const stack of allStacks) { |
| 97 | + if (!stack.id) continue; |
| 98 | + const branches = await stackService.fetchBranches(projectId, stack.id); |
| 99 | + const commitFound = branches?.some((branch) => |
| 100 | + branch.commits.some((commit) => commit.id === commitId), |
| 101 | + ); |
| 102 | + if (!commitFound) continue; |
| 103 | +
|
| 104 | + const stackHead = stack.heads.at(0)?.name; |
| 105 | + if (!stackHead) { |
| 106 | + showToast({ |
| 107 | + title: "Stack found but no branch head", |
| 108 | + message: `Commit ${commitId.substring(0, 7)} belongs to a stack without a visible head branch. |
| 109 | +Open Branches to inspect and recover manually.`, |
| 110 | + style: "warning", |
| 111 | + }); |
| 112 | + return; |
| 113 | + } |
| 114 | +
|
| 115 | + await applyKnownButUnappliedStack(stack.id, stackHead); |
| 116 | + return; |
| 117 | + } |
| 118 | +
|
| 119 | + showToast({ |
| 120 | + title: "Commit could not be mapped to a stack", |
| 121 | + message: `Commit ${commitId.substring(0, 7)} was not found in known stacks. |
| 122 | +It may be orphaned history. Open Branches to create a recovery stack/branch.`, |
| 123 | + style: "warning", |
| 124 | + }); |
| 125 | + } catch (error) { |
| 126 | + showError("Failed to recover unknown stack", error); |
| 127 | + } finally { |
| 128 | + locatingCommitId = null; |
| 129 | + } |
26 | 130 | } |
27 | 131 | </script> |
28 | 132 |
|
|
77 | 181 | </div> |
78 | 182 | <div class="commit-failed__file-entry__dependency-locks__content"> |
79 | 183 | {#each dependency.locks as lock} |
80 | | - {#if lock.target.type === "stack"} |
81 | | - {@const branchesQuery = stackService.branches(projectId, lock.target.subject)} |
82 | | - {@const branch = branchesQuery.response} |
83 | | - {@const commitBranch = branch?.find((b) => |
84 | | - b.commits.some((c) => c.id === lock.commitId), |
85 | | - )} |
86 | | - {@const branchName = commitBranch?.name || "Unknown branch"} |
87 | | - {@const commitMessage = commitBranch?.commits.find( |
88 | | - (c) => c.id === lock.commitId, |
89 | | - )} |
90 | | - {@const commitTitle = |
91 | | - commitMessage?.message.split("\n")[0] || "No commit message provided"} |
92 | | - <p class="text-body commit-failed__file-entry-dependency-lock"> |
93 | | - <i class="commit-failed__text-icon"><Icon name="branch-small" /></i> |
94 | | - <span class="text-semibold">{branchName}</span> |
95 | | - <i class="clr-text-2">in commit</i> |
96 | | - <i class="commit-failed__text-icon"><Icon name="commit" /></i> |
97 | | - <Tooltip text={commitTitle}> |
98 | | - <span class="commit-failed__tooltip-text text-semibold h-dotted-underline" |
99 | | - >{lock.commitId.substring(0, 7)}</span |
| 184 | + {#if !isIgnoredLock(lock)} |
| 185 | + {#if lock.target.type === "stack"} |
| 186 | + {@const stackTarget = lock.target as { type: "stack"; subject: string }} |
| 187 | + {@const branchesQuery = stackService.branches(projectId, stackTarget.subject)} |
| 188 | + {@const branch = branchesQuery.response} |
| 189 | + {@const commitBranch = branch?.find((b) => |
| 190 | + b.commits.some((c) => c.id === lock.commitId), |
| 191 | + )} |
| 192 | + {@const knownStack = stackService.allStackById(projectId, stackTarget.subject).response} |
| 193 | + {@const stackHeadBranch = knownStack?.heads.at(0)?.name} |
| 194 | + {@const branchName = commitBranch?.name || knownStack?.heads.at(0)?.name || "Unknown stack"} |
| 195 | + {@const commitMessage = commitBranch?.commits.find( |
| 196 | + (c) => c.id === lock.commitId, |
| 197 | + )} |
| 198 | + {@const commitTitle = |
| 199 | + commitMessage?.message.split("\n")[0] || "No commit message provided"} |
| 200 | + <p class="text-body commit-failed__file-entry-dependency-lock"> |
| 201 | + <i class="commit-failed__text-icon"><Icon name="branch-small" /></i> |
| 202 | + <span class="text-semibold">{branchName}</span> |
| 203 | + <i class="clr-text-2">in commit</i> |
| 204 | + <i class="commit-failed__text-icon"><Icon name="commit" /></i> |
| 205 | + <Tooltip text={commitTitle}> |
| 206 | + <span class="commit-failed__tooltip-text text-semibold h-dotted-underline">{lock.commitId.substring(0, 7)}</span> |
| 207 | + </Tooltip> |
| 208 | + </p> |
| 209 | + |
| 210 | + {#if !commitBranch && knownStack && stackHeadBranch} |
| 211 | + <p class="text-12 clr-text-2"> |
| 212 | + This stack exists but is not currently applied in your workspace. |
| 213 | + </p> |
| 214 | + <div class="commit-failed__lock-actions"> |
| 215 | + <AsyncButton |
| 216 | + kind="outline" |
| 217 | + loading={applyingStackId === stackTarget.subject} |
| 218 | + action={async () => await applyKnownButUnappliedStack(stackTarget.subject, stackHeadBranch)} |
| 219 | + > |
| 220 | + Apply stack to workspace |
| 221 | + </AsyncButton> |
| 222 | + <Button kind="outline" onclick={() => goto(branchesPath(projectId))}>Open Branches</Button> |
| 223 | + <Button kind="ghost" onclick={() => ignoreLock(lock)}>Ignore for now</Button> |
| 224 | + </div> |
| 225 | + {:else if !commitBranch && !knownStack} |
| 226 | + <p class="text-12 clr-text-2"> |
| 227 | + The stack id is no longer known. This can happen when the source stack was removed or rewritten. |
| 228 | + </p> |
| 229 | + <div class="commit-failed__lock-actions"> |
| 230 | + <AsyncButton |
| 231 | + kind="outline" |
| 232 | + loading={locatingCommitId === lock.commitId} |
| 233 | + action={async () => await recoverStackByCommit(lock.commitId)} |
| 234 | + > |
| 235 | + Try recover by commit id |
| 236 | + </AsyncButton> |
| 237 | + <Button kind="outline" onclick={() => goto(branchesPath(projectId))}>Open Branches</Button> |
| 238 | + <Button kind="ghost" onclick={() => ignoreLock(lock)}>Ignore for now</Button> |
| 239 | + </div> |
| 240 | + {/if} |
| 241 | + {:else} |
| 242 | + <p class="text-body commit-failed__file-entry-dependency-lock"> |
| 243 | + <i class="commit-failed__text-icon"><Icon name="branch-small" /></i> |
| 244 | + <span class="text-semibold">Unknown stack</span> |
| 245 | + <i class="clr-text-2">in commit</i> |
| 246 | + <i class="commit-failed__text-icon"><Icon name="commit" /></i> |
| 247 | + <span class="text-semibold">{lock.commitId.substring(0, 7)}</span> |
| 248 | + </p> |
| 249 | + <p class="text-12 clr-text-2"> |
| 250 | + The dependency solver could not map this lock to a stack id in the current workspace. |
| 251 | + </p> |
| 252 | + <div class="commit-failed__lock-actions"> |
| 253 | + <AsyncButton |
| 254 | + kind="outline" |
| 255 | + loading={locatingCommitId === lock.commitId} |
| 256 | + action={async () => await recoverStackByCommit(lock.commitId)} |
100 | 257 | > |
101 | | - </Tooltip> |
102 | | - </p> |
103 | | - {:else} |
104 | | - <p class="text-body commit-failed__file-entry-dependency-lock"> |
105 | | - <i class="commit-failed__text-icon"><Icon name="branch-small" /></i> |
106 | | - <span class="text-semibold">Unknown stack</span> |
107 | | - <i class="clr-text-2">in commit</i> |
108 | | - <i class="commit-failed__text-icon"><Icon name="commit" /></i> |
109 | | - <span class="text-semibold">{lock.commitId.substring(0, 7)}</span> |
110 | | - </p> |
| 258 | + Try recover by commit id |
| 259 | + </AsyncButton> |
| 260 | + <Button kind="outline" onclick={() => goto(branchesPath(projectId))}>Open Branches</Button> |
| 261 | + <Button kind="ghost" onclick={() => ignoreLock(lock)}>Ignore for now</Button> |
| 262 | + </div> |
| 263 | + {/if} |
111 | 264 | {/if} |
112 | 265 | {/each} |
113 | 266 | </div> |
|
207 | 360 | gap: 4px; |
208 | 361 | } |
209 | 362 |
|
| 363 | + .commit-failed__lock-actions { |
| 364 | + display: flex; |
| 365 | + flex-wrap: wrap; |
| 366 | + gap: 6px; |
| 367 | + padding-top: 2px; |
| 368 | + } |
| 369 | +
|
210 | 370 | /* MODIFIERS */ |
211 | 371 | .clickable { |
212 | 372 | cursor: pointer; |
|
0 commit comments