From cae7b7964fad52e270c0aa4e24700cb89a9e1b7c Mon Sep 17 00:00:00 2001 From: Kiet Ho Date: Sat, 29 Nov 2025 15:14:03 -0800 Subject: [PATCH 1/2] load in existing worktrees if path exists --- .../src/lib/trpc/routers/projects/projects.ts | 90 ++++++++++++++++++- .../lib/trpc/routers/workspaces/utils/git.ts | 47 ++++++++++ 2 files changed, 135 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/lib/trpc/routers/projects/projects.ts b/apps/desktop/src/lib/trpc/routers/projects/projects.ts index d8a51bbe3c8..17d2df47535 100644 --- a/apps/desktop/src/lib/trpc/routers/projects/projects.ts +++ b/apps/desktop/src/lib/trpc/routers/projects/projects.ts @@ -1,13 +1,16 @@ -import { basename } from "node:path"; +import { existsSync } from "node:fs"; +import { homedir } from "node:os"; +import { basename, join } from "node:path"; import type { BrowserWindow } from "electron"; import { dialog } from "electron"; import { db } from "main/lib/db"; import type { Project } from "main/lib/db/schemas"; import { nanoid } from "nanoid"; +import { SUPERSET_DIR_NAME, WORKTREES_DIR_NAME } from "shared/constants"; import { PROJECT_COLOR_VALUES } from "shared/constants/project-colors"; import { z } from "zod"; import { publicProcedure, router } from "../.."; -import { getGitRoot } from "../workspaces/utils/git"; +import { getGitRoot, listWorktrees } from "../workspaces/utils/git"; import { assignRandomColor } from "./utils/colors"; export const createProjectsRouter = (window: BrowserWindow) => { @@ -65,6 +68,89 @@ export const createProjectsRouter = (window: BrowserWindow) => { // biome-ignore lint/style/noNonNullAssertion: project is assigned above, TypeScript can't see it inside callback data.projects.push(project!); }); + + // Import existing worktrees for this new project + const supersetWorktreesDir = join( + homedir(), + SUPERSET_DIR_NAME, + WORKTREES_DIR_NAME, + ); + + const existingWorktrees = await listWorktrees(mainRepoPath); + + // Filter to only worktrees in the superset worktrees directory that exist on disk + const supersetWorktrees = existingWorktrees.filter( + (wt) => + wt.path.startsWith(supersetWorktreesDir) && existsSync(wt.path), + ); + + if (supersetWorktrees.length > 0) { + await db.update((data) => { + for (const wt of supersetWorktrees) { + // Check if worktree already exists in db + const existingWorktree = data.worktrees.find( + (w) => w.path === wt.path, + ); + if (existingWorktree) { + continue; + } + + const worktreeId = nanoid(); + const worktree = { + id: worktreeId, + // biome-ignore lint/style/noNonNullAssertion: project is assigned above + projectId: project!.id, + path: wt.path, + branch: wt.branch, + createdAt: Date.now(), + gitStatus: { + branch: wt.branch, + needsRebase: false, + lastRefreshed: Date.now(), + }, + }; + data.worktrees.push(worktree); + + // Calculate next tab order for workspace + const projectWorkspaces = data.workspaces.filter( + // biome-ignore lint/style/noNonNullAssertion: project is assigned above + (w) => w.projectId === project!.id, + ); + const maxTabOrder = + projectWorkspaces.length > 0 + ? Math.max(...projectWorkspaces.map((w) => w.tabOrder)) + : -1; + + const workspace = { + id: nanoid(), + // biome-ignore lint/style/noNonNullAssertion: project is assigned above + projectId: project!.id, + worktreeId, + name: wt.branch, + tabOrder: maxTabOrder + 1, + createdAt: Date.now(), + updatedAt: Date.now(), + lastOpenedAt: Date.now(), + }; + data.workspaces.push(workspace); + + // Mark project as active since we found workspaces + // biome-ignore lint/style/noNonNullAssertion: project is assigned above + const p = data.projects.find((proj) => proj.id === project!.id); + if (p && p.tabOrder === null) { + const activeProjects = data.projects.filter( + (proj) => proj.tabOrder !== null, + ); + const maxProjectTabOrder = + activeProjects.length > 0 + ? // biome-ignore lint/style/noNonNullAssertion: filter guarantees tabOrder is not null + Math.max(...activeProjects.map((proj) => proj.tabOrder!)) + : -1; + p.tabOrder = maxProjectTabOrder + 1; + } + } + }); + } } return { diff --git a/apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts b/apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts index 9f73c42ec97..82bf157c302 100644 --- a/apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts +++ b/apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts @@ -144,3 +144,50 @@ export async function checkNeedsRebase(worktreePath: string): Promise { ]); return Number.parseInt(behindCount.trim(), 10) > 0; } + +export interface WorktreeInfo { + path: string; + branch: string; +} + +/** + * Lists all worktrees for a repository + * @param mainRepoPath - Path to the main repository + * @returns Array of worktree information (path and branch) + */ +export async function listWorktrees( + mainRepoPath: string, +): Promise { + const git = simpleGit(mainRepoPath); + const output = await git.raw(["worktree", "list", "--porcelain"]); + + const worktrees: WorktreeInfo[] = []; + const lines = output.split("\n"); + + let currentWorktree: Partial = {}; + + for (const line of lines) { + const trimmed = line.trim(); + + if (trimmed.startsWith("worktree ")) { + // Start of a new worktree entry + if (currentWorktree.path && currentWorktree.branch) { + worktrees.push(currentWorktree as WorktreeInfo); + } + currentWorktree = { path: trimmed.slice("worktree ".length) }; + } else if (trimmed.startsWith("branch ")) { + // Branch reference (e.g., "branch refs/heads/branch-name") + const branchRef = trimmed.slice("branch ".length); + // Extract branch name from refs/heads/branch-name + const branchName = branchRef.replace("refs/heads/", ""); + currentWorktree.branch = branchName; + } + } + + // Don't forget the last worktree + if (currentWorktree.path && currentWorktree.branch) { + worktrees.push(currentWorktree as WorktreeInfo); + } + + return worktrees; +} From 2476930391650f42a679e4ddabc38be5b3b587ff Mon Sep 17 00:00:00 2001 From: Kiet <31864905+Kitenite@users.noreply.github.com> Date: Sun, 30 Nov 2025 19:44:59 -0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- apps/desktop/src/lib/trpc/routers/projects/projects.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/desktop/src/lib/trpc/routers/projects/projects.ts b/apps/desktop/src/lib/trpc/routers/projects/projects.ts index 17d2df47535..4ceb8a2fe23 100644 --- a/apps/desktop/src/lib/trpc/routers/projects/projects.ts +++ b/apps/desktop/src/lib/trpc/routers/projects/projects.ts @@ -81,7 +81,7 @@ export const createProjectsRouter = (window: BrowserWindow) => { // Filter to only worktrees in the superset worktrees directory that exist on disk const supersetWorktrees = existingWorktrees.filter( (wt) => - wt.path.startsWith(supersetWorktreesDir) && existsSync(wt.path), + wt.path.startsWith(`${supersetWorktreesDir}/`) && existsSync(wt.path), ); if (supersetWorktrees.length > 0) {