()
| 16 | const INVALID_BRANCH_PATTERNS = [/^-/, /\.\./, /\.$/, /^\./, /@\{/, /\\/, /\s/]; |
| 17 | |
| 18 | export const createBranchesRouter = () => { |
| 19 | return router({ |
| 20 | getBranches: publicProcedure |
| 21 | .input(z.object({ worktreePath: z.string() })) |
| 22 | .query( |
| 23 | async ({ |
| 24 | input, |
| 25 | }): Promise<{ |
| 26 | current: string; |
| 27 | local: Array<{ branch: string; lastCommitDate: number }>; |
| 28 | remote: string[]; |
| 29 | defaultBranch: string; |
| 30 | checkedOutBranches: Record<string, string>; |
| 31 | }> => { |
| 32 | assertRegisteredWorktree(input.worktreePath); |
| 33 | const git = createGit(input.worktreePath); |
| 34 | const branchSummary = await git.branch(["-a"]); |
| 35 | |
| 36 | const localBranches: string[] = []; |
| 37 | const remote: string[] = []; |
| 38 | |
| 39 | for (const name of Object.keys(branchSummary.branches)) { |
| 40 | if (name.startsWith("remotes/origin/")) { |
| 41 | if (name === "remotes/origin/HEAD") continue; |
| 42 | const remoteName = name.replace("remotes/origin/", ""); |
| 43 | remote.push(remoteName); |
| 44 | } else { |
| 45 | localBranches.push(name); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const local = await getLocalBranchesWithDates(git, localBranches); |
| 50 | const defaultBranch = await getDefaultBranch(git, remote); |
| 51 | const checkedOutBranches = await getCheckedOutBranches( |
| 52 | git, |
| 53 | input.worktreePath, |
| 54 | ); |
| 55 | |
| 56 | return { |
| 57 | current: branchSummary.current, |
| 58 | local, |
| 59 | remote: remote.sort(), |
| 60 | defaultBranch, |
| 61 | checkedOutBranches, |
| 62 | }; |
| 63 | }, |
| 64 | ), |
| 65 | |
| 66 | switchBranch: publicProcedure |
| 67 | .input( |
| 68 | z.object({ |
| 69 | worktreePath: z.string(), |
| 70 | branch: z.string(), |
| 71 | }), |
| 72 | ) |
| 73 | .mutation(async ({ input }): Promise<{ success: boolean }> => { |
| 74 | const chat = getRegisteredChat(input.worktreePath); |
| 75 | await gitSwitchBranch(input.worktreePath, input.branch); |
no test coverage detected