Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

checkout-new-task-branch.ts

116 lines4.1 KB

1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2import type { VcsRef } from "@t3tools/client-runtime/state/vcs";
3import {
4 type AtomCommandResult,
5 mapAtomCommandResult,
6} from "@t3tools/client-runtime/state/runtime";
7import type {
8 VcsListRefsInput,
9 VcsListRefsResult,
10 VcsSwitchRefInput,
11 VcsSwitchRefResult,
12} from "@t3tools/contracts";
13import { deriveLocalBranchNameFromRemoteRef } from "@t3tools/shared/git";
14import * as Cause from "effect/Cause";
15import { AsyncResult } from "effect/reactivity";
16import { msg } from "gt-react-native";
17​
18import { m } from "../../i18n/messages";
19​
20const VCS_REF_QUERY_MAX_LENGTH = 256;
21​
22export 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 }));
111​
112function localBranchName(branch: VcsRef): string {
113 return branch.isRemote
114 ? deriveLocalBranchNameFromRemoteRef(branch.name, branch.remoteName)
115 : branch.name;