Skip to content

Commit ea68f2a

Browse files
saphidclaudemaria-rcks
authored
fix(web): picking a branch in a subfolder project keeps the thread in the project folder (#29908)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Maria <[email protected]>
1 parent 4a09502 commit ea68f2a

3 files changed

Lines changed: 53 additions & 15 deletions

File tree

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ describe("dedupeRemoteBranchesWithLocalMatches", () => {
160160

161161
describe("resolveBranchSelectionTarget", () => {
162162
it("reuses an existing secondary worktree for the selected ref", () => {
163+
expect(
164+
resolveBranchSelectionTarget({
165+
activeProjectCwd: "/repo/nested-checkout/packages/app",
166+
activeProjectCheckoutRoot: "/repo/nested-checkout",
167+
activeWorktreePath: null,
168+
refName: { isDefault: true, worktreePath: "/repo" },
169+
}),
170+
).toEqual({
171+
checkoutCwd: "/repo",
172+
nextWorktreePath: "/repo",
173+
reuseExistingWorktree: true,
174+
});
163175
expect(
164176
resolveBranchSelectionTarget({
165177
activeProjectCwd: "/repo",
@@ -177,20 +189,31 @@ describe("resolveBranchSelectionTarget", () => {
177189
});
178190

179191
it("switches back to the main repo when the ref already lives there", () => {
180-
expect(
181-
resolveBranchSelectionTarget({
182-
activeProjectCwd: "/repo",
183-
activeWorktreePath: "/repo/.t3/worktrees/feature-a",
184-
refName: {
185-
isDefault: true,
186-
worktreePath: "/repo",
187-
},
188-
}),
189-
).toEqual({
190-
checkoutCwd: "/repo",
191-
nextWorktreePath: null,
192-
reuseExistingWorktree: true,
193-
});
192+
// A subdirectory project's own checkout is reported at the repository root.
193+
// The checkout root is undefined until the project's status loads.
194+
for (const [activeProjectCwd, activeWorktreePath, activeProjectCheckoutRoot] of [
195+
["/repo", "/repo/.t3/worktrees/feature-a", "/repo"],
196+
["/repo/packages/app", "/repo/.t3/worktrees/feature-a", "/repo"],
197+
["/repo/packages/app", null, "/repo"],
198+
["/repo/packages/app", "/repo/.t3/worktrees/feature-a", undefined],
199+
["/repo/packages/app", null, undefined],
200+
] as const) {
201+
expect(
202+
resolveBranchSelectionTarget({
203+
activeProjectCwd,
204+
activeProjectCheckoutRoot,
205+
activeWorktreePath,
206+
refName: {
207+
isDefault: true,
208+
worktreePath: "/repo",
209+
},
210+
}),
211+
).toEqual({
212+
checkoutCwd: "/repo",
213+
nextWorktreePath: null,
214+
reuseExistingWorktree: true,
215+
});
216+
}
194217
});
195218

196219
it("keeps checkout in the current worktree for non-default refs", () => {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
} from "@t3tools/contracts";
88
import * as Schema from "effect/Schema";
99
import { sanitizeNewRefName } from "@t3tools/shared/git";
10+
import { workspaceRelativeFilePath } from "@t3tools/shared/path";
1011
import { toSortableTimestamp } from "@t3tools/client-runtime/state/thread-sort";
1112
export { deriveLocalBranchNameFromRemoteRef, sanitizeNewRefName } from "@t3tools/shared/git";
1213

@@ -247,6 +248,7 @@ export function resolveLocalCheckoutBranchMismatch(input: {
247248

248249
export function resolveBranchSelectionTarget(input: {
249250
activeProjectCwd: string;
251+
activeProjectCheckoutRoot?: string | undefined;
250252
activeWorktreePath: string | null;
251253
refName: Pick<VcsRef, "isDefault" | "worktreePath">;
252254
}): {
@@ -257,9 +259,14 @@ export function resolveBranchSelectionTarget(input: {
257259
const { activeProjectCwd, activeWorktreePath, refName } = input;
258260

259261
if (refName.worktreePath) {
262+
// Until the project's checkout root loads, a checkout containing the project counts as its own.
263+
const isProjectCheckout =
264+
input.activeProjectCheckoutRoot === undefined
265+
? workspaceRelativeFilePath(activeProjectCwd, refName.worktreePath) !== null
266+
: workspaceRelativeFilePath(input.activeProjectCheckoutRoot, refName.worktreePath) === ".";
260267
return {
261268
checkoutCwd: refName.worktreePath,
262-
nextWorktreePath: refName.worktreePath === activeProjectCwd ? null : refName.worktreePath,
269+
nextWorktreePath: isProjectCheckout ? null : refName.worktreePath,
263270
reuseExistingWorktree: true,
264271
};
265272
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ export function BranchToolbarBranchSelector({
224224
input: { cwd: branchCwd },
225225
}),
226226
);
227+
const projectStatusQuery = useEnvironmentQuery(
228+
activeProjectCwd === null
229+
? null
230+
: vcsEnvironment.status({ environmentId, input: { cwd: activeProjectCwd } }),
231+
);
232+
const activeProjectCheckoutRoot = projectStatusQuery.data?.workingTree.rootPath;
227233
const trimmedBranchQuery = branchQuery.trim();
228234
const branchRefState = usePaginatedBranches({
229235
environmentId,
@@ -389,6 +395,7 @@ export function BranchToolbarBranchSelector({
389395

390396
const selectionTarget = resolveBranchSelectionTarget({
391397
activeProjectCwd,
398+
activeProjectCheckoutRoot,
392399
activeWorktreePath,
393400
refName,
394401
});
@@ -631,6 +638,7 @@ export function BranchToolbarBranchSelector({
631638
(!activeProjectCwd ||
632639
!resolveBranchSelectionTarget({
633640
activeProjectCwd,
641+
activeProjectCheckoutRoot,
634642
activeWorktreePath,
635643
refName,
636644
}).reuseExistingWorktree))

0 commit comments

Comments
 (0)