MCPcopy
hub / github.com/21st-dev/1code / createBranchesRouter

Function createBranchesRouter

src/main/lib/git/branches.ts:18–267  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16const INVALID_BRANCH_PATTERNS = [/^-/, /\.\./, /\.$/, /^\./, /@\{/, /\\/, /\s/];
17
18export 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);

Callers 1

createGitRouterFunction · 0.90

Calls 13

assertRegisteredWorktreeFunction · 0.90
createGitFunction · 0.90
getRegisteredChatFunction · 0.90
gitSwitchBranchFunction · 0.90
getDatabaseFunction · 0.90
withGitLockFunction · 0.90
withLockRetryFunction · 0.90
createGitForNetworkFunction · 0.90
getCheckedOutBranchesFunction · 0.85
getDefaultBranchFunction · 0.70
setMethod · 0.45

Tested by

no test coverage detected