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

new-task-context-presentation.ts

95 lines2.7 KB

1import { sanitizeNewRefName } from "@t3tools/shared/git";
2​
3type WorkspaceMode = "local" | "worktree";
4​
5export function resolveNewTaskWorkspaceLabel(input: {
6 readonly workspaceMode: WorkspaceMode;
7 readonly worktreePath: string | null;
8}): "Current checkout" | "Current worktree" | "New worktree" {
9 if (input.workspaceMode === "worktree") {
10 return "New worktree";
11 }
12 return input.worktreePath ? "Current worktree" : "Current checkout";
14​
15export function resolveNewTaskBranchWorktreePath(input: {
16 readonly workspaceMode: WorkspaceMode;
17 readonly projectCwd: string;
18 readonly branchWorktreePath: string | null | undefined;
19}): string | null {
20 if (
21 input.workspaceMode === "worktree" ||
22 !input.branchWorktreePath ||
23 input.branchWorktreePath === input.projectCwd
24 ) {
25 return null;
26 }
27 return input.branchWorktreePath;
29​
30export function resolveNewTaskLocalWorkspaceSelection(input: {
31 readonly branches: ReadonlyArray<{
32 readonly name: string;
33 readonly current: boolean;
34 readonly worktreePath?: string | null;
35 }>;
36 readonly projectCwd: string;
37}): {
38 readonly branch: string | null;
39 readonly worktreePath: string | null;
40 readonly awaitsCurrentBranch: boolean;
41} {
42 const currentBranch = input.branches.find((branch) => branch.current) ?? null;
43 if (!currentBranch) {
44 return {
45 branch: null,
46 worktreePath: null,
47 awaitsCurrentBranch: true,
48 };
49 }
50​
51 return {
52 branch: currentBranch.name,
53 worktreePath: resolveNewTaskBranchWorktreePath({
54 workspaceMode: "local",
55 projectCwd: input.projectCwd,
56 branchWorktreePath: currentBranch.worktreePath,
57 }),
58 awaitsCurrentBranch: false,
59 };
61​
62export function resolveNewTaskBranchLabel(input: {
63 readonly branchName: string | null;
64 readonly startFromOrigin: boolean;
65 readonly workspaceMode: WorkspaceMode;
66}): string {
67 if (!input.branchName) {
68 return "Choose branch";
69 }
70​
71 if (input.workspaceMode === "local") {
72 return input.branchName;
73 }
74​
75 const baseRef = input.startFromOrigin ? `origin/${input.branchName}` : input.branchName;
76 return `From ${baseRef}`;
78​
79export function shouldCheckoutNewTaskBranch(input: {
80 readonly branchIsCurrent: boolean;
81 readonly branchWorktreePath: string | null | undefined;
82 readonly workspaceMode: WorkspaceMode;
83}): boolean {
84 return input.workspaceMode === "local" && !input.branchIsCurrent && !input.branchWorktreePath;
86​
87export function filterNewTaskBranches<T extends { readonly name: string }>(
88 branches: ReadonlyArray<T>,
89 rawQuery: string,
90): ReadonlyArray<T> {
91 const query = sanitizeNewRefName(rawQuery).toLowerCase();
92 return query.length === 0
93 ? branches
94 : branches.filter((branch) => branch.name.toLowerCase().includes(query));