Skip to content
Merged
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
39 changes: 26 additions & 13 deletions apps/staged/src/lib/features/projects/ProjectHome.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
import { projectRunActionsStore } from '../../stores/projectRunActions.svelte';
import { repoBadgeStore } from '../../stores/repoBadges.svelte';

/**
* Merge incoming branches with existing ones, preserving worktreePath when
* a stale async response would overwrite an already-populated value with null.
*/
function mergeBranchesPreservingWorktree(existing: Branch[], incoming: Branch[]): Branch[] {
return incoming.map((newBranch) => {
const prev = existing.find((b) => b.id === newBranch.id);
if (prev?.worktreePath && !newBranch.worktreePath) {
return { ...newBranch, worktreePath: prev.worktreePath };
}
return newBranch;
});
}

interface Props {
selectedProjectId?: string | null;
}
Expand Down Expand Up @@ -118,19 +132,13 @@
]);
setProjects(projectsList);
projects = projectsList;
// Merge branches carefully: don't let a stale async response overwrite
// worktreePath with null when we already have it set.
const existingBranches = branchesByProject.get(projectId) || [];
const mergedBranches = branches.map((newBranch) => {
const existing = existingBranches.find((b) => b.id === newBranch.id);
if (existing?.worktreePath && !newBranch.worktreePath) {
return { ...newBranch, worktreePath: existing.worktreePath };
}
return newBranch;
});
const mergedBranches = mergeBranchesPreservingWorktree(
branchesByProject.get(projectId) || [],
branches
);
branchesByProject = new Map(branchesByProject).set(projectId, mergedBranches);
commands.invalidateProjectBranchTimelines(mergedBranches.map((b) => b.id));
workspaceLifecycle.enqueueInitialSetup(projectId, branches);
workspaceLifecycle.enqueueInitialSetup(projectId, mergedBranches);
replaceProjectRepos(projectId, repos);
void repoBadgeStore.ensureForRepos(
repos.map((r) => ({ githubRepo: r.githubRepo, subpath: r.subpath }))
Expand Down Expand Up @@ -558,8 +566,13 @@
]);
setProjects(projectsList);
projects = projectsList;
branchesByProject = new Map(branchesByProject).set(projectId, branches);
workspaceLifecycle.enqueueInitialSetup(projectId, branches);
const mergedBranches = mergeBranchesPreservingWorktree(
branchesByProject.get(projectId) || [],
branches
);
branchesByProject = new Map(branchesByProject).set(projectId, mergedBranches);
commands.invalidateProjectBranchTimelines(mergedBranches.map((b) => b.id));
workspaceLifecycle.enqueueInitialSetup(projectId, mergedBranches);
replaceProjectRepos(projectId, repos);
void repoBadgeStore.ensureForRepos(
repos.map((r) => ({ githubRepo: r.githubRepo, subpath: r.subpath }))
Expand Down