Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 165 additions & 1 deletion apps/desktop/src/lib/trpc/routers/workspaces/procedures/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
sanitizeBranchName,
worktreeExists,
} from "../utils/git";
import { loadSetupConfig } from "../utils/setup";
import { copySupersetConfigToWorktree, loadSetupConfig } from "../utils/setup";
import { initializeWorkspaceWorktree } from "../utils/workspace-init";

interface CreateWorkspaceFromWorktreeParams {
Expand Down Expand Up @@ -615,6 +615,170 @@ export const createCreateProcedures = () => {
};
}),

openExternalWorktree: publicProcedure
.input(
z.object({
projectId: z.string(),
worktreePath: z.string(),
branch: z.string(),
}),
)
.mutation(async ({ input }) => {
const project = getProject(input.projectId);
if (!project) {
throw new Error(`Project ${input.projectId} not found`);
}

const exists = await worktreeExists(
project.mainRepoPath,
input.worktreePath,
);
if (!exists) {
throw new Error("Worktree no longer exists on disk");
}

const existingWorktree = localDb
.select()
.from(worktrees)
.where(
and(
eq(worktrees.projectId, input.projectId),
eq(worktrees.path, input.worktreePath),
),
)
.get();
Comment thread
Kitenite marked this conversation as resolved.

if (existingWorktree) {
// Failed init can leave gitStatus null, which shows "Setup incomplete" UI
if (!existingWorktree.gitStatus) {
localDb
.update(worktrees)
.set({
gitStatus: {
branch: existingWorktree.branch,
needsRebase: false,
lastRefreshed: Date.now(),
},
})
.where(eq(worktrees.id, existingWorktree.id))
.run();
}

const existingWorkspace = localDb
.select()
.from(workspaces)
.where(
and(
eq(workspaces.worktreeId, existingWorktree.id),
isNull(workspaces.deletingAt),
),
)
.get();

if (existingWorkspace) {
touchWorkspace(existingWorkspace.id);
setLastActiveWorkspace(existingWorkspace.id);
return {
workspace: existingWorkspace,
initialCommands: null,
worktreePath: existingWorktree.path,
projectId: project.id,
wasExisting: true,
};
}

const maxTabOrder = getMaxWorkspaceTabOrder(input.projectId);
const workspace = localDb
.insert(workspaces)
.values({
projectId: input.projectId,
worktreeId: existingWorktree.id,
type: "worktree",
branch: existingWorktree.branch,
name: existingWorktree.branch,
tabOrder: maxTabOrder + 1,
})
.returning()
.get();

setLastActiveWorkspace(workspace.id);
activateProject(project);

copySupersetConfigToWorktree(
project.mainRepoPath,
existingWorktree.path,
);
const setupConfig = loadSetupConfig(project.mainRepoPath);

track("workspace_opened", {
workspace_id: workspace.id,
project_id: project.id,
type: "worktree",
source: "external_import",
});

return {
workspace,
initialCommands: setupConfig?.setup || null,
worktreePath: existingWorktree.path,
projectId: project.id,
wasExisting: false,
};
}

const defaultBranch = project.defaultBranch || "main";
const worktree = localDb
.insert(worktrees)
.values({
projectId: input.projectId,
path: input.worktreePath,
branch: input.branch,
baseBranch: defaultBranch,
gitStatus: {
branch: input.branch,
needsRebase: false,
lastRefreshed: Date.now(),
},
})
.returning()
.get();

const maxTabOrder = getMaxWorkspaceTabOrder(input.projectId);
const workspace = localDb
.insert(workspaces)
.values({
projectId: input.projectId,
worktreeId: worktree.id,
type: "worktree",
branch: input.branch,
name: input.branch,
tabOrder: maxTabOrder + 1,
})
.returning()
.get();

setLastActiveWorkspace(workspace.id);
activateProject(project);

copySupersetConfigToWorktree(project.mainRepoPath, input.worktreePath);
const setupConfig = loadSetupConfig(project.mainRepoPath);

track("workspace_created", {
workspace_id: workspace.id,
project_id: project.id,
branch: input.branch,
source: "external_import",
});

return {
workspace,
initialCommands: setupConfig?.setup || null,
worktreePath: input.worktreePath,
projectId: project.id,
wasExisting: false,
};
}),

createFromPr: publicProcedure
.input(
z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
checkNeedsRebase,
fetchDefaultBranch,
getDefaultBranch,
listExternalWorktrees,
refreshDefaultBranch,
} from "../utils/git";
import { fetchGitHubPRStatus } from "../utils/github";
Expand Down Expand Up @@ -166,5 +167,38 @@ export const createGitStatusProcedures = () => {
};
});
}),

getExternalWorktrees: publicProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input }) => {
const project = getProject(input.projectId);
if (!project) {
return [];
Comment on lines +171 to +176

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cd /tmp/repo && find . -name "git-status.ts" -path "*/trpc/routers/workspaces/procedures/*" 2>/dev/null

Repository: superset-sh/superset

Length of output: 123


🏁 Script executed:

cd /tmp/repo && git ls-files | grep -E "git-status\.ts$"

Repository: superset-sh/superset

Length of output: 123


🏁 Script executed:

cd /tmp/repo && find . -path "*/apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts" 2>/dev/null

Repository: superset-sh/superset

Length of output: 123


🏁 Script executed:

git ls-files | grep "git-status.ts"

Repository: superset-sh/superset

Length of output: 133


🏁 Script executed:

find . -type f -name "git-status.ts" 2>/dev/null | head -20

Repository: superset-sh/superset

Length of output: 135


🏁 Script executed:

fd "git-status.ts" 2>/dev/null

Repository: superset-sh/superset

Length of output: 133


🏁 Script executed:

wc -l apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 137


🏁 Script executed:

sed -n '1,30p' apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 933


🏁 Script executed:

sed -n '165,185p' apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 628


🏁 Script executed:

sed -n '1,50p' apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 1519


🏁 Script executed:

rg "throw new" apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts -A 2

Repository: superset-sh/superset

Length of output: 308


🏁 Script executed:

rg "TRPCError" apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 46


🏁 Script executed:

rg "TRPCError" apps/desktop/src/lib/trpc/ --type ts --type tsx -l

Repository: superset-sh/superset

Length of output: 91


🏁 Script executed:

head -5 apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts && echo "---" && sed -n '20,50p' apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 1220


🏁 Script executed:

find apps/desktop/src/lib/trpc/routers -name "*.ts" -o -name "*.tsx" | head -5 | xargs grep -l "TRPCError"

Repository: superset-sh/superset

Length of output: 48


🏁 Script executed:

grep -n "import.*TRPCError" apps/desktop/src/lib/trpc/**/*.ts 2>/dev/null | head -10

Repository: superset-sh/superset

Length of output: 46


🏁 Script executed:

find apps/desktop/src/lib/trpc -name "*.ts" | head -3 | xargs cat

Repository: superset-sh/superset

Length of output: 4707


🏁 Script executed:

sed -n '1,10p' apps/desktop/src/lib/trpc/index.ts 2>/dev/null || sed -n '1,10p' apps/desktop/src/lib/trpc.ts 2>/dev/null || echo "Checking directory structure..."

Repository: superset-sh/superset

Length of output: 377


🏁 Script executed:

ls -la apps/desktop/src/lib/trpc/

Repository: superset-sh/superset

Length of output: 293


🏁 Script executed:

cat apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts

Repository: superset-sh/superset

Length of output: 5431


Use TRPCError.NOT_FOUND for missing projects.

Returning an empty list for an invalid projectId hides an error path and bypasses standard tRPC handling. Throw NOT_FOUND so callers can surface the issue properly.

🐛 Suggested fix
+import { TRPCError } from "@trpc/server";
 import { z } from "zod";
-				if (!project) {
-					return [];
-				}
+				if (!project) {
+					throw new TRPCError({
+						code: "NOT_FOUND",
+						message: `Project ${input.projectId} not found`,
+					});
+				}

Per coding guidelines for apps/*/src/**/trpc/**/*.{ts,tsx}, use TRPCError with code NOT_FOUND when a resource doesn't exist.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
getExternalWorktrees: publicProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input }) => {
const project = getProject(input.projectId);
if (!project) {
return [];
getExternalWorktrees: publicProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input }) => {
const project = getProject(input.projectId);
if (!project) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Project ${input.projectId} not found`,
});
}
🤖 Prompt for AI Agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/procedures/git-status.ts` around
lines 171 - 176, The getExternalWorktrees procedure currently returns an empty
array when getProject(input.projectId) yields no project; instead throw a
TRPCError with code NOT_FOUND so callers can handle the missing resource. In the
getExternalWorktrees publicProcedure, after calling getProject(input.projectId)
(and before returning), replace the empty-array return with throwing new
TRPCError({ code: 'NOT_FOUND', message: `Project ${input.projectId} not found`
}) (import TRPCError if needed) so the procedure surfaces the missing project
correctly.

}

const allWorktrees = await listExternalWorktrees(project.mainRepoPath);

const trackedWorktrees = localDb
.select({ path: worktrees.path })
.from(worktrees)
.where(eq(worktrees.projectId, input.projectId))
.all();
const trackedPaths = new Set(trackedWorktrees.map((wt) => wt.path));

return allWorktrees
.filter((wt) => {
if (wt.path === project.mainRepoPath) return false;
if (wt.isBare) return false;
if (wt.isDetached) return false;
if (!wt.branch) return false;
if (trackedPaths.has(wt.path)) return false;
return true;
})
.map((wt) => ({
path: wt.path,
// biome-ignore lint/style/noNonNullAssertion: filtered above
branch: wt.branch!,
}));
}),
});
};
53 changes: 53 additions & 0 deletions apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,59 @@ export async function worktreeExists(
}
}

export interface ExternalWorktree {
path: string;
branch: string | null;
isDetached: boolean;
isBare: boolean;
}

export async function listExternalWorktrees(
mainRepoPath: string,
): Promise<ExternalWorktree[]> {
try {
const git = simpleGit(mainRepoPath);
const output = await git.raw(["worktree", "list", "--porcelain"]);

const result: ExternalWorktree[] = [];
let current: Partial<ExternalWorktree> = {};

for (const line of output.split("\n")) {
if (line.startsWith("worktree ")) {
if (current.path) {
result.push({
path: current.path,
branch: current.branch ?? null,
isDetached: current.isDetached ?? false,
isBare: current.isBare ?? false,
});
}
current = { path: line.slice("worktree ".length) };
} else if (line.startsWith("branch refs/heads/")) {
current.branch = line.slice("branch refs/heads/".length);
} else if (line === "detached") {
current.isDetached = true;
} else if (line === "bare") {
current.isBare = true;
}
}

if (current.path) {
result.push({
path: current.path,
branch: current.branch ?? null,
isDetached: current.isDetached ?? false,
isBare: current.isBare ?? false,
});
}

return result;
} catch (error) {
console.error(`Failed to list external worktrees: ${error}`);
throw error;
}
}

/**
* Checks if a branch is already checked out in a worktree.
* @param mainRepoPath - Path to the main repository
Expand Down
Loading
Loading