The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 2 | import type { VcsRef } from "@t3tools/client-runtime/state/vcs"; |
| 3 | import { |
| 4 | type AtomCommandResult, |
| 5 | mapAtomCommandResult, |
| 6 | } from "@t3tools/client-runtime/state/runtime"; |
| 7 | import type { |
| 8 | VcsListRefsInput, |
| 9 | VcsListRefsResult, |
| 10 | VcsSwitchRefInput, |
| 11 | VcsSwitchRefResult, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { deriveLocalBranchNameFromRemoteRef } from "@t3tools/shared/git"; |
| 14 | import * as Cause from "effect/Cause"; |
| 15 | import { AsyncResult } from "effect/reactivity"; |
| 16 | import { msg } from "gt-react-native"; |
| 17 | |
| 18 | import { m } from "../../i18n/messages"; |
| 19 | |
| 20 | const VCS_REF_QUERY_MAX_LENGTH = 256; |
| 21 | |
| 22 | export async function checkoutNewTaskBranch<E>(input: { |
| 23 | readonly branch: VcsRef; |
| 24 | readonly project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot"> | null; |
| 25 | readonly workspaceMode: "local" | "worktree"; |
| 26 | readonly readRefs: (request: { |
| 27 | readonly environmentId: EnvironmentProject["environmentId"]; |
| 28 | readonly input: VcsListRefsInput; |
| 29 | }) => Promise<AtomCommandResult<VcsListRefsResult, E>>; |
| 30 | readonly switchRef: (request: { |
| 31 | readonly environmentId: EnvironmentProject["environmentId"]; |
| 32 | readonly input: VcsSwitchRefInput; |
| 33 | }) => Promise<AtomCommandResult<VcsSwitchRefResult, E>>; |
| 34 | }): Promise<AtomCommandResult<VcsRef, E | Error>> { |
| 35 | if (!input.project) { |
| 36 | return AsyncResult.failure( |
| 37 | Cause.fail( |
| 38 | new Error(m(msg("The selected project is unavailable. Reconnect and try again."))), |
| 39 | ), |
| 40 | ); |
| 41 | } |
| 42 | if (input.workspaceMode === "worktree") return AsyncResult.success(input.branch); |
| 43 | |
| 44 | // The picked ref's current flag, remote refs' missing worktrees and historical |
| 45 | // thread metadata can all be stale, so Git is asked again before choosing. |
| 46 | const localName = localBranchName(input.branch); |
| 47 | let cursor: number | undefined; |
| 48 | for (;;) { |
| 49 | const refs = await input.readRefs({ |
| 50 | environmentId: input.project.environmentId, |
| 51 | input: { |
| 52 | cwd: input.project.workspaceRoot, |
| 53 | // Bound the substring query; compare the full name across every page. |
| 54 | query: localName.slice(0, VCS_REF_QUERY_MAX_LENGTH), |
| 55 | refKind: "local", |
| 56 | ...(cursor === undefined ? { refresh: true } : { cursor }), |
| 57 | }, |
| 58 | }); |
| 59 | if (refs._tag === "Failure") return AsyncResult.failure(refs.cause); |
| 60 | const local = refs.value.refs.find((ref) => ref.name === localName); |
| 61 | if ( |
| 62 | local && |
| 63 | (!input.branch.isRemote || local.upstreamRef === `refs/remotes/${input.branch.name}`) && |
| 64 | (local.current || local.worktreePath) |
| 65 | ) { |
| 66 | if (local.current) return AsyncResult.success({ ...local, worktreePath: null }); |
| 67 | if (refs.value.isAtRepositoryRoot === undefined) { |
| 68 | return AsyncResult.failure( |
| 69 | Cause.fail( |
| 70 | new Error( |
| 71 | m( |
| 72 | msg( |
| 73 | "The server did not report whether this project is at the repository root, so this worktree cannot be reused safely. Update the server or choose another branch.", |
| 74 | ), |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | ); |
| 79 | } |
| 80 | if (!refs.value.isAtRepositoryRoot) { |
| 81 | return AsyncResult.failure( |
| 82 | Cause.fail( |
| 83 | new Error( |
| 84 | m( |
| 85 | msg( |
| 86 | "This project is a repository subdirectory. Choose another branch or add the repository root as a project to reuse this worktree.", |
| 87 | ), |
| 88 | ), |
| 89 | ), |
| 90 | ), |
| 91 | ); |
| 92 | } |
| 93 | return AsyncResult.success(local); |
| 94 | } |
| 95 | if (local || refs.value.nextCursor === null) break; |
| 96 | cursor = refs.value.nextCursor; |
| 97 | } |
| 98 | |
| 99 | const result = await input.switchRef({ |
| 100 | environmentId: input.project.environmentId, |
| 101 | input: { cwd: input.project.workspaceRoot, refName: input.branch.name }, |
| 102 | }); |
| 103 | return mapAtomCommandResult(result, (value) => ({ |
| 104 | ...input.branch, |
| 105 | current: true, |
| 106 | isRemote: false, |
| 107 | name: value.refName ?? input.branch.name, |
| 108 | worktreePath: null, |
| 109 | })); |
| 110 | } |
| 111 | |
| 112 | // A remote name can itself contain slashes; prefer the authoritative prefix. |
| 113 | function localBranchName(branch: VcsRef): string { |
| 114 | if (!branch.isRemote) return branch.name; |
| 115 | const prefix = branch.remoteName ? `${branch.remoteName}/` : null; |
| 116 | return prefix && branch.name.startsWith(prefix) |
| 117 | ? branch.name.slice(prefix.length) |
| 118 | : deriveLocalBranchNameFromRemoteRef(branch.name); |
| 119 | } |