The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { sanitizeNewRefName } from "@t3tools/shared/git"; |
| 2 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import { changeRequestTermKind } from "../../lib/changeRequestTerm"; |
| 4 | import { resolveSendEnvMode } from "@t3tools/shared/projectSettings"; |
| 5 | import { msg } from "gt-react-native"; |
| 6 | |
| 7 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 8 | import type { ComposerDraftWorkspaceSelection } from "../../state/use-composer-drafts"; |
| 9 | |
| 10 | type WorkspaceMode = "local" | "worktree"; |
| 11 | |
| 12 | export function resolveNewTaskWorkspaceSelection(input: { |
| 13 | readonly selection: ComposerDraftWorkspaceSelection | undefined; |
| 14 | readonly fallback: ComposerDraftWorkspaceSelection; |
| 15 | readonly isGitRepo: boolean; |
| 16 | }): ComposerDraftWorkspaceSelection { |
| 17 | const selection = input.selection ?? input.fallback; |
| 18 | const mode = resolveSendEnvMode({ requestedEnvMode: selection.mode, isGitRepo: input.isGitRepo }); |
| 19 | return input.isGitRepo ? selection : { mode, branch: null, worktreePath: null }; |
| 20 | } |
| 21 | |
| 22 | export function resolveNewTaskWorkspaceLabel( |
| 23 | input: { |
| 24 | readonly workspaceMode: WorkspaceMode; |
| 25 | readonly worktreePath: string | null; |
| 26 | }, |
| 27 | resolve: MessageResolver = m, |
| 28 | ): string { |
| 29 | if (input.workspaceMode === "worktree") { |
| 30 | return resolve(msg("New worktree")); |
| 31 | } |
| 32 | return input.worktreePath ? resolve(msg("Current worktree")) : resolve(msg("Current checkout")); |
| 33 | } |
| 34 | |
| 35 | export function resolveNewTaskBranchWorktreePath(input: { |
| 36 | readonly workspaceMode: WorkspaceMode; |
| 37 | readonly projectCwd: string; |
| 38 | readonly branchWorktreePath: string | null | undefined; |
| 39 | }): string | null { |
| 40 | if ( |
| 41 | input.workspaceMode === "worktree" || |
| 42 | !input.branchWorktreePath || |
| 43 | input.branchWorktreePath === input.projectCwd |
| 44 | ) { |
| 45 | return null; |
| 46 | } |
| 47 | return input.branchWorktreePath; |
| 48 | } |
| 49 | |
| 50 | export function resolveNewTaskLocalWorkspaceSelection(input: { |
| 51 | readonly branches: ReadonlyArray<{ |
| 52 | readonly name: string; |
| 53 | readonly current: boolean; |
| 54 | readonly worktreePath?: string | null; |
| 55 | }>; |
| 56 | readonly projectCwd: string; |
| 57 | }): { |
| 58 | readonly branch: string | null; |
| 59 | readonly worktreePath: string | null; |
| 60 | readonly awaitsCurrentBranch: boolean; |
| 61 | } { |
| 62 | const currentBranch = input.branches.find((branch) => branch.current) ?? null; |
| 63 | if (!currentBranch) { |
| 64 | return { |
| 65 | branch: null, |
| 66 | worktreePath: null, |
| 67 | awaitsCurrentBranch: true, |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | return { |
| 72 | branch: currentBranch.name, |
| 73 | worktreePath: resolveNewTaskBranchWorktreePath({ |
| 74 | workspaceMode: "local", |
| 75 | projectCwd: input.projectCwd, |
| 76 | branchWorktreePath: currentBranch.worktreePath, |
| 77 | }), |
| 78 | awaitsCurrentBranch: false, |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | export function resolveNewTaskBranchLabel( |
| 83 | input: { |
| 84 | readonly branchName: string | null; |
| 85 | readonly startFromOrigin: boolean; |
| 86 | readonly workspaceMode: WorkspaceMode; |
| 87 | readonly pullRequestReference?: string; |
| 88 | }, |
| 89 | resolve: MessageResolver = m, |
| 90 | ): string { |
| 91 | if (input.workspaceMode === "worktree" && input.pullRequestReference) { |
| 92 | const parsed = sourceControlClients.parseChangeRequestUrl(input.pullRequestReference); |
| 93 | if (parsed) |
| 94 | return resolve( |
| 95 | msg( |
| 96 | "{term, select, mr {From MR #{number}} cr {From change request #{number}} other {From PR #{number}}}", |
| 97 | { |
| 98 | term: changeRequestTermKind(parsed.definition.changeRequest), |
| 99 | number: parsed.link.number, |
| 100 | }, |
| 101 | ), |
| 102 | ); |
| 103 | } |
| 104 | if (!input.branchName) { |
| 105 | return resolve(msg("Choose branch")); |
| 106 | } |
| 107 | |
| 108 | if (input.workspaceMode === "local") { |
| 109 | return input.branchName; |
| 110 | } |
| 111 | |
| 112 | // Only the server can determine a local branch's configured remote. |
| 113 | return input.startFromOrigin |
| 114 | ? resolve(msg("From {branchName} (remote)", { branchName: input.branchName })) |
| 115 | : resolve(msg("From {branchName}", { branchName: input.branchName })); |
| 116 | } |
| 117 | |
| 118 | export function shouldCheckoutNewTaskBranch(input: { |
| 119 | readonly branchIsCurrent: boolean; |
| 120 | readonly branchWorktreePath: string | null | undefined; |
| 121 | readonly workspaceMode: WorkspaceMode; |
| 122 | }): boolean { |
| 123 | return input.workspaceMode === "local" && !input.branchIsCurrent && !input.branchWorktreePath; |
| 124 | } |
| 125 | |
| 126 | export function filterNewTaskBranches<T extends { readonly name: string }>( |
| 127 | branches: ReadonlyArray<T>, |
| 128 | rawQuery: string, |
| 129 | ): ReadonlyArray<T> { |
| 130 | const query = sanitizeNewRefName(rawQuery).toLowerCase(); |
| 131 | return query.length === 0 |
| 132 | ? branches |
| 133 | : branches.filter((branch) => branch.name.toLowerCase().includes(query)); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * A worktree with no chosen base starts from the default branch, and a local |
| 138 | * selection the user just switched to follows the current checkout until refs |
| 139 | * load. Both are derived, never written back, so a late ref list or t3.json |
| 140 | * default still wins. |
| 141 | */ |
| 142 | export function fillNewTaskWorkspaceBranch(input: { |
| 143 | readonly selection: ComposerDraftWorkspaceSelection; |
| 144 | readonly branches: ReadonlyArray<{ |
| 145 | readonly name: string; |
| 146 | readonly current: boolean; |
| 147 | readonly isDefault: boolean; |
| 148 | readonly isRemote?: boolean; |
| 149 | readonly worktreePath?: string | null; |
| 150 | }>; |
| 151 | readonly projectCwd: string | null; |
| 152 | readonly worktreeDefaultSettled: boolean; |
| 153 | }): ComposerDraftWorkspaceSelection { |
| 154 | const { selection } = input; |
| 155 | if (selection.branch !== null) return selection; |
| 156 | if (selection.mode === "worktree") { |
| 157 | // The default may only exist as origin/<default>, so search remote refs too. |
| 158 | const base = |
| 159 | input.branches.find((branch) => branch.isDefault) ?? |
| 160 | input.branches.find((branch) => !branch.isRemote && branch.current) ?? |
| 161 | null; |
| 162 | return input.worktreeDefaultSettled && base |
| 163 | ? { ...selection, branch: base.name, worktreePath: null } |
| 164 | : selection; |
| 165 | } |
| 166 | if (!selection.followsCurrentCheckout || input.projectCwd === null) return selection; |
| 167 | const local = resolveNewTaskLocalWorkspaceSelection({ |
| 168 | branches: input.branches.filter((branch) => !branch.isRemote), |
| 169 | projectCwd: input.projectCwd, |
| 170 | }); |
| 171 | return local.awaitsCurrentBranch |
| 172 | ? selection |
| 173 | : { ...selection, branch: local.branch, worktreePath: local.worktreePath }; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * The workspace a new-task draft starts in: its own selection, else the |
| 178 | * project's default mode, with the branch filled in as above. The new-task |
| 179 | * flow, its send path and the home pending-drafts list all resolve through |
| 180 | * this, so what each shows is what is sent. |
| 181 | */ |
| 182 | export function resolveNewTaskDraftWorkspace(input: { |
| 183 | readonly selection: ComposerDraftWorkspaceSelection | undefined; |
| 184 | readonly canChooseWorkspace: boolean; |
| 185 | readonly defaultMode: WorkspaceMode; |
| 186 | readonly defaultModeSettled: boolean; |
| 187 | readonly isGitRepo: boolean; |
| 188 | readonly branches: Parameters<typeof fillNewTaskWorkspaceBranch>[0]["branches"]; |
| 189 | readonly projectCwd: string | null; |
| 190 | }): ComposerDraftWorkspaceSelection { |
| 191 | const selection = input.canChooseWorkspace ? input.selection : undefined; |
| 192 | return fillNewTaskWorkspaceBranch({ |
| 193 | selection: resolveNewTaskWorkspaceSelection({ |
| 194 | selection, |
| 195 | fallback: { mode: input.defaultMode, branch: null, worktreePath: null }, |
| 196 | isGitRepo: input.isGitRepo, |
| 197 | }), |
| 198 | branches: input.branches, |
| 199 | projectCwd: input.projectCwd, |
| 200 | worktreeDefaultSettled: selection?.mode !== undefined || input.defaultModeSettled, |
| 201 | }); |
| 202 | } |