From b1b66e46adc697e50f1bfc4c48e948464081dfbc Mon Sep 17 00:00:00 2001 From: Kiet Ho Date: Thu, 29 Jan 2026 14:17:05 -0800 Subject: [PATCH] fix(desktop): handle workspace creation for local-only repos - Fix worktree creation failing for repos without a remote - Skip checking origin/* refs when no remote is configured - Add fallback to common branch names (main, master, develop, trunk) when the requested base branch doesn't exist locally - Only use fallback for auto-derived base branches, not explicit ones - Update workspace metadata when fallback branch is used - Show user-friendly progress messages when using fallback - Make error text selectable in workspace init failure view --- .../workspaces/utils/workspace-init.ts | 159 ++++++++++++++++-- .../WorkspaceInitializingView.tsx | 2 +- 2 files changed, 142 insertions(+), 19 deletions(-) diff --git a/apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts b/apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts index b1bd7fe00ea..e819e7844bc 100644 --- a/apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts +++ b/apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts @@ -184,25 +184,80 @@ export async function initializeWorkspaceWorktree({ ); const hasRemote = await hasOriginRemote(mainRepoPath); + type LocalStartPointResult = { + ref: string; + fallbackBranch?: string; + } | null; + const resolveLocalStartPoint = async ( reason: string, - ): Promise => { - const originRef = `origin/${effectiveBaseBranch}`; - if (await refExistsLocally(mainRepoPath, originRef)) { - console.log( - `[workspace-init] ${reason}. Using local tracking ref: ${originRef}`, - ); - return originRef; + checkOriginRefs: boolean, + ): Promise => { + // Try origin tracking ref first (only if remote exists) + if (checkOriginRefs) { + const originRef = `origin/${effectiveBaseBranch}`; + if (await refExistsLocally(mainRepoPath, originRef)) { + console.log( + `[workspace-init] ${reason}. Using local tracking ref: ${originRef}`, + ); + return { ref: originRef }; + } } + + // Try local branch if (await refExistsLocally(mainRepoPath, effectiveBaseBranch)) { console.log( `[workspace-init] ${reason}. Using local branch: ${effectiveBaseBranch}`, ); - return effectiveBaseBranch; + return { ref: effectiveBaseBranch }; + } + + // Only try fallback branches if the base branch was auto-derived + if (baseBranchWasExplicit) { + console.log( + `[workspace-init] ${reason}. Base branch "${effectiveBaseBranch}" was explicitly set, not using fallback.`, + ); + return null; } + + // Fallback: try common default branch names + const commonBranches = ["main", "master", "develop", "trunk"]; + for (const branch of commonBranches) { + if (branch === effectiveBaseBranch) continue; // Already tried + // Only check origin refs if remote exists + if (checkOriginRefs) { + const fallbackOriginRef = `origin/${branch}`; + if (await refExistsLocally(mainRepoPath, fallbackOriginRef)) { + console.log( + `[workspace-init] ${reason}. Using fallback tracking ref: ${fallbackOriginRef}`, + ); + return { ref: fallbackOriginRef, fallbackBranch: branch }; + } + } + if (await refExistsLocally(mainRepoPath, branch)) { + console.log( + `[workspace-init] ${reason}. Using fallback local branch: ${branch}`, + ); + return { ref: branch, fallbackBranch: branch }; + } + } + return null; }; + // Helper to update baseBranch when fallback is used + const applyFallbackBranch = (fallbackBranch: string) => { + console.log( + `[workspace-init] Updating baseBranch from "${effectiveBaseBranch}" to "${fallbackBranch}" for workspace ${workspaceId}`, + ); + effectiveBaseBranch = fallbackBranch; + localDb + .update(worktrees) + .set({ baseBranch: fallbackBranch }) + .where(eq(worktrees.id, worktreeId)) + .run(); + }; + let startPoint: string; if (hasRemote) { const branchCheck = await branchExistsOnRemote( @@ -223,17 +278,31 @@ export async function initializeWorkspaceWorktree({ sanitizedError, ); - const localRef = await resolveLocalStartPoint("Remote unavailable"); - if (!localRef) { + const localResult = await resolveLocalStartPoint( + "Remote unavailable", + true, + ); + if (!localResult) { manager.updateProgress( workspaceId, "failed", "No local reference available", - `Cannot reach remote and no local ref for "${effectiveBaseBranch}" exists. Please check your network connection and try again.`, + baseBranchWasExplicit + ? `Cannot reach remote and branch "${effectiveBaseBranch}" doesn't exist locally. Please check your network connection and try again.` + : `Cannot reach remote and no local ref for "${effectiveBaseBranch}" exists. Please check your network connection and try again.`, ); return; } - startPoint = localRef; + if (localResult.fallbackBranch) { + applyFallbackBranch(localResult.fallbackBranch); + manager.updateProgress( + workspaceId, + "verifying", + `Using "${localResult.fallbackBranch}" branch`, + `Branch "${baseBranch}" not found locally. Using "${localResult.fallbackBranch}" instead.`, + ); + } + startPoint = localResult.ref; } else if (branchCheck.status === "not_found") { manager.updateProgress( workspaceId, @@ -246,17 +315,31 @@ export async function initializeWorkspaceWorktree({ startPoint = `origin/${effectiveBaseBranch}`; } } else { - const localRef = await resolveLocalStartPoint("No remote configured"); - if (!localRef) { + const localResult = await resolveLocalStartPoint( + "No remote configured", + false, + ); + if (!localResult) { manager.updateProgress( workspaceId, "failed", "No local reference available", - `No remote configured and no local ref for "${effectiveBaseBranch}" exists.`, + baseBranchWasExplicit + ? `No remote configured and branch "${effectiveBaseBranch}" doesn't exist locally.` + : `No remote configured and no local ref for "${effectiveBaseBranch}" exists.`, ); return; } - startPoint = localRef; + if (localResult.fallbackBranch) { + applyFallbackBranch(localResult.fallbackBranch); + manager.updateProgress( + workspaceId, + "verifying", + `Using "${localResult.fallbackBranch}" branch`, + `Branch "${baseBranch}" not found locally. Using "${localResult.fallbackBranch}" instead.`, + ); + } + startPoint = localResult.ref; } if (manager.isCancellationRequested(workspaceId)) { @@ -271,8 +354,48 @@ export async function initializeWorkspaceWorktree({ if (hasRemote) { try { await fetchDefaultBranch(mainRepoPath, effectiveBaseBranch); - } catch { - // Silently continue - branch exists on remote, just couldn't fetch + } catch (fetchError) { + // Fetch failed - verify local tracking ref exists before proceeding + const originRef = `origin/${effectiveBaseBranch}`; + if (!(await refExistsLocally(mainRepoPath, originRef))) { + console.warn( + `[workspace-init] Fetch failed and local ref "${originRef}" doesn't exist. Attempting local fallback.`, + ); + const localResult = await resolveLocalStartPoint( + "Fetch failed and remote tracking ref unavailable", + true, + ); + if (!localResult) { + const sanitizedError = sanitizeGitError( + fetchError instanceof Error + ? fetchError.message + : String(fetchError), + ); + manager.updateProgress( + workspaceId, + "failed", + "Cannot fetch branch", + baseBranchWasExplicit + ? `Failed to fetch "${effectiveBaseBranch}" and it doesn't exist locally. ` + + `Please check your network connection or try running "git fetch origin ${effectiveBaseBranch}" manually. ` + + `Error: ${sanitizedError}` + : `Failed to fetch "${effectiveBaseBranch}" and no local reference exists. ` + + `Please check your network connection or try running "git fetch origin ${effectiveBaseBranch}" manually. ` + + `Error: ${sanitizedError}`, + ); + return; + } + if (localResult.fallbackBranch) { + applyFallbackBranch(localResult.fallbackBranch); + manager.updateProgress( + workspaceId, + "fetching", + `Using "${localResult.fallbackBranch}" branch`, + `Could not fetch "${baseBranch}". Using local "${localResult.fallbackBranch}" branch instead.`, + ); + } + startPoint = localResult.ref; + } } } diff --git a/apps/desktop/src/renderer/screens/main/components/WorkspaceView/WorkspaceInitializingView/WorkspaceInitializingView.tsx b/apps/desktop/src/renderer/screens/main/components/WorkspaceView/WorkspaceInitializingView/WorkspaceInitializingView.tsx index 7d96bfb1744..180deff65fb 100644 --- a/apps/desktop/src/renderer/screens/main/components/WorkspaceView/WorkspaceInitializingView/WorkspaceInitializingView.tsx +++ b/apps/desktop/src/renderer/screens/main/components/WorkspaceView/WorkspaceInitializingView/WorkspaceInitializingView.tsx @@ -194,7 +194,7 @@ export function WorkspaceInitializingView({

{workspaceName}

{progress?.error && ( -

+

{progress.error}

)}