Skip to content

Commit bd6fa59

Browse files
Yuruzuuclaude
andcommitted
fix(web): keep the branch label through a worktree-to-root checkout
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 84f7b59 commit bd6fa59

3 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎apps/web/src/components/BranchToolbar.logic.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ describe("shouldIncludeBranchPickerItem", () => {
311311

312312
describe("isBranchHoldCurrent", () => {
313313
const environmentId = EnvironmentId.make("env-a");
314-
const hold = { branch: "feature", replaces: "main", environmentId, cwd: "/repo" };
314+
const hold = {
315+
branch: "feature",
316+
environmentId,
317+
checkouts: [{ cwd: "/repo", replaces: "main" }],
318+
};
315319
const current = { canonicalBranch: "main", environmentId, cwd: "/repo" };
316320

317321
it("keeps showing the checked-out branch while status still reports the old one", () => {
@@ -328,6 +332,30 @@ describe("isBranchHoldCurrent", () => {
328332
expect(isBranchHoldCurrent(hold, { ...current, cwd: "/other" })).toBe(false);
329333
});
330334

335+
it("follows a thread moving from its worktree to the project root", () => {
336+
const moveHold = {
337+
branch: "main",
338+
environmentId,
339+
checkouts: [
340+
{ cwd: "/repo-feature", replaces: "feature" },
341+
{ cwd: "/repo", replaces: "dev" },
342+
],
343+
};
344+
expect(
345+
isBranchHoldCurrent(moveHold, {
346+
environmentId,
347+
cwd: "/repo-feature",
348+
canonicalBranch: "feature",
349+
}),
350+
).toBe(true);
351+
expect(
352+
isBranchHoldCurrent(moveHold, { environmentId, cwd: "/repo", canonicalBranch: "dev" }),
353+
).toBe(true);
354+
expect(
355+
isBranchHoldCurrent(moveHold, { environmentId, cwd: "/repo", canonicalBranch: "main" }),
356+
).toBe(false);
357+
});
358+
331359
it("does not apply to the same path on another environment", () => {
332360
expect(
333361
isBranchHoldCurrent(hold, { ...current, environmentId: EnvironmentId.make("env-b") }),

‎apps/web/src/components/BranchToolbar.logic.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ export function resolveBranchToolbarValue(input: {
201201

202202
export interface BranchHold {
203203
readonly branch: string | null;
204-
readonly replaces: string | null;
205204
readonly environmentId: EnvironmentId;
206-
readonly cwd: string;
205+
/**
206+
* Each checkout the thread can show before it catches up, with the branch its status reported
207+
* before the switch. A move from a worktree to the project root lists both.
208+
*/
209+
readonly checkouts: ReadonlyArray<{ readonly cwd: string; readonly replaces: string | null }>;
207210
}
208211

209212
/**
@@ -220,8 +223,9 @@ export function isBranchHoldCurrent(
220223
): boolean {
221224
return (
222225
hold.environmentId === current.environmentId &&
223-
hold.cwd === current.cwd &&
224-
hold.replaces === current.canonicalBranch
226+
hold.checkouts.some(
227+
(checkout) => checkout.cwd === current.cwd && checkout.replaces === current.canonicalBranch,
228+
)
225229
);
226230
}
227231

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,19 @@ export function BranchToolbarBranchSelector({
491491
if (applied) {
492492
setBranchHold({
493493
branch: nextBranchName,
494-
replaces: canonicalActiveBranch,
495494
environmentId,
496-
cwd: selectionTarget.checkoutCwd,
495+
// The thread keeps its old checkout until the metadata update moves it to the new one.
496+
checkouts: [
497+
{ cwd: branchCwd, replaces: canonicalActiveBranch },
498+
...(selectionTarget.checkoutCwd === branchCwd
499+
? []
500+
: [
501+
{
502+
cwd: selectionTarget.checkoutCwd,
503+
replaces: projectStatusQuery.data?.refName ?? null,
504+
},
505+
]),
506+
],
497507
});
498508
}
499509
setOptimisticBranch(applied ? nextBranchName : previousBranch);
@@ -537,9 +547,8 @@ export function BranchToolbarBranchSelector({
537547
if (applied) {
538548
setBranchHold({
539549
branch: nextBranchName,
540-
replaces: canonicalActiveBranch,
541550
environmentId,
542-
cwd: branchCwd,
551+
checkouts: [{ cwd: branchCwd, replaces: canonicalActiveBranch }],
543552
});
544553
}
545554
setOptimisticBranch(applied ? nextBranchName : previousBranch);

0 commit comments

Comments
 (0)