Open History to see the last commit
1import type { GitBranch } from "@t3tools/contracts";
2import { Schema } from "effect";
3
4export const EnvMode = Schema.Literals(["local", "worktree"]);
5export type EnvMode = typeof EnvMode.Type;
6
7export function resolveEffectiveEnvMode(input: {
8 activeWorktreePath: string | null;
9 hasServerThread: boolean;
10 draftThreadEnvMode: EnvMode | undefined;
11}): EnvMode {
12 const { activeWorktreePath, hasServerThread, draftThreadEnvMode } = input;
13 return activeWorktreePath || (!hasServerThread && draftThreadEnvMode === "worktree")
14 ? "worktree"
15 : "local";
16}
17
18export function resolveDraftEnvModeAfterBranchChange(input: {
19 nextWorktreePath: string | null;
20 currentWorktreePath: string | null;
21 effectiveEnvMode: EnvMode;
22}): EnvMode {
23 const { nextWorktreePath, currentWorktreePath, effectiveEnvMode } = input;
24 if (nextWorktreePath) {
25 return "worktree";
26 }
27 if (effectiveEnvMode === "worktree" && !currentWorktreePath) {
28 return "worktree";
29 }
30 return "local";
31}
32
33export function resolveBranchToolbarValue(input: {
34 envMode: EnvMode;
35 activeWorktreePath: string | null;
36 activeThreadBranch: string | null;
37 currentGitBranch: string | null;
38}): string | null {
39 const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
40 if (envMode === "worktree" && !activeWorktreePath) {
41 return activeThreadBranch ?? currentGitBranch;
42 }
43 return currentGitBranch ?? activeThreadBranch;
44}
45
46export function deriveLocalBranchNameFromRemoteRef(branchName: string): string {
47 const firstSeparatorIndex = branchName.indexOf("/");
48 if (firstSeparatorIndex <= 0 || firstSeparatorIndex === branchName.length - 1) {
49 return branchName;
50 }
51 return branchName.slice(firstSeparatorIndex + 1);
52}
53
54function deriveLocalBranchNameCandidatesFromRemoteRef(
55 branchName: string,
56 remoteName?: string,
57): ReadonlyArray<string> {
58 const candidates = new Set<string>();
59 const firstSlashCandidate = deriveLocalBranchNameFromRemoteRef(branchName);
60 if (firstSlashCandidate.length > 0) {
61 candidates.add(firstSlashCandidate);
62 }
63
64 if (remoteName) {
65 const remotePrefix = `${remoteName}/`;
66 if (branchName.startsWith(remotePrefix) && branchName.length > remotePrefix.length) {
67 candidates.add(branchName.slice(remotePrefix.length));
68 }
69 }
70
71 return [...candidates];
72}
73
74export function dedupeRemoteBranchesWithLocalMatches(
75 branches: ReadonlyArray<GitBranch>,
76): ReadonlyArray<GitBranch> {
77 const localBranchNames = new Set(
78 branches.filter((branch) => !branch.isRemote).map((branch) => branch.name),
79 );
80
81 return branches.filter((branch) => {
82 if (!branch.isRemote) {
83 return true;
84 }
85
86 if (branch.remoteName !== "origin") {
87 return true;
88 }
89
90 const localBranchCandidates = deriveLocalBranchNameCandidatesFromRemoteRef(
91 branch.name,
92 branch.remoteName,
93 );
94 return !localBranchCandidates.some((candidate) => localBranchNames.has(candidate));
95 });
96}
97
98export function resolveBranchSelectionTarget(input: {
99 activeProjectCwd: string;
100 activeWorktreePath: string | null;
101 branch: Pick<GitBranch, "isDefault" | "worktreePath">;
102}): {
103 checkoutCwd: string;
104 nextWorktreePath: string | null;
105 reuseExistingWorktree: boolean;
106} {
107 const { activeProjectCwd, activeWorktreePath, branch } = input;
108
109 if (branch.worktreePath) {
110 return {
111 checkoutCwd: branch.worktreePath,
112 nextWorktreePath: branch.worktreePath === activeProjectCwd ? null : branch.worktreePath,
113 reuseExistingWorktree: true,
114 };
115 }
116
117 const nextWorktreePath =
118 activeWorktreePath !== null && branch.isDefault ? null : activeWorktreePath;
119
120 return {
121 checkoutCwd: nextWorktreePath ?? activeProjectCwd,
122 nextWorktreePath,
123 reuseExistingWorktree: false,
124 };
125}
126
127export function shouldIncludeBranchPickerItem(input: {
128 itemValue: string;
129 normalizedQuery: string;
130 createBranchItemValue: string | null;
131 checkoutPullRequestItemValue: string | null;
132}): boolean {
133 const { itemValue, normalizedQuery, createBranchItemValue, checkoutPullRequestItemValue } = input;
134
135 if (normalizedQuery.length === 0) {
136 return true;
137 }
138
139 if (createBranchItemValue && itemValue === createBranchItemValue) {
140 return true;
141 }
142
143 if (checkoutPullRequestItemValue && itemValue === checkoutPullRequestItemValue) {
144 return true;
145 }
146
147 return itemValue.toLowerCase().includes(normalizedQuery);
148}